Implement PR suggestions

This commit is contained in:
zandercymatics 2024-01-25 13:45:45 -07:00
parent dc5e0293f6
commit c75c4bb7a8
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
5 changed files with 47 additions and 16 deletions

View file

@ -994,7 +994,7 @@ class DomainAdmin(ListHeaderAdmin):
fieldsets = ( fieldsets = (
( (
None, None,
{"fields": ["name", "state", "expiration_date", "first_ready", "deleted", "domain_notes"]}, {"fields": ["name", "state", "expiration_date", "first_ready", "deleted", "notes"]},
), ),
) )

View file

@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2024-01-25 16:18 # Generated by Django 4.2.7 on 2024-01-25 20:43
from django.db import migrations, models from django.db import migrations, models
@ -11,7 +11,7 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.AddField( migrations.AddField(
model_name="domain", model_name="domain",
name="domain_notes", name="notes",
field=models.TextField(blank=True, help_text="Notes about this domain", null=True), field=models.TextField(blank=True, help_text="Notes about this domain", null=True),
), ),
migrations.AddField( migrations.AddField(

View file

@ -992,7 +992,7 @@ class Domain(TimeStampedModel, DomainHelper):
help_text="The last time this domain moved into the READY state", help_text="The last time this domain moved into the READY state",
) )
domain_notes = models.TextField( notes = models.TextField(
null=True, null=True,
blank=True, blank=True,
help_text="Notes about this domain", help_text="Notes about this domain",

View file

@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
from django.db import transaction from django.db import transaction
from registrar.models.utility.domain_helper import DomainHelper
from .domain_application import DomainApplication from .domain_application import DomainApplication
from .utility.time_stamped_model import TimeStampedModel from .utility.time_stamped_model import TimeStampedModel
@ -236,15 +238,11 @@ class DomainInformation(TimeStampedModel):
if existing_domain_info: if existing_domain_info:
return existing_domain_info return existing_domain_info
# Get a list of the existing fields on DomainApplication and DomainInformation # Get the fields that exist on both DomainApplication and DomainInformation
domain_app_fields = set(field.name for field in DomainApplication._meta.get_fields() if field != "id") common_fields = DomainHelper.get_common_fields(DomainApplication, DomainInformation)
domain_info_fields = set(field.name for field in DomainInformation._meta.get_fields() if field != "id")
# Get a list of all many_to_many relations on DomainInformation (needs to be saved differently) # Get a list of all many_to_many relations on DomainInformation (needs to be saved differently)
info_many_to_many_fields = {field.name for field in DomainInformation._meta.many_to_many} # type: ignore info_many_to_many_fields = DomainInformation._get_many_to_many_fields()
# Get the fields that exist on both DomainApplication and DomainInformation
common_fields = domain_app_fields & domain_info_fields
# Create a dictionary with only the common fields, and create a DomainInformation from it # Create a dictionary with only the common fields, and create a DomainInformation from it
da_dict = {} da_dict = {}
@ -253,9 +251,10 @@ class DomainInformation(TimeStampedModel):
# If the field isn't many_to_many, populate the da_dict. # If the field isn't many_to_many, populate the da_dict.
# If it is, populate da_many_to_many_dict as we need to save this later. # If it is, populate da_many_to_many_dict as we need to save this later.
if hasattr(domain_application, field) and field not in info_many_to_many_fields: if hasattr(domain_application, field) and field not in info_many_to_many_fields:
da_dict[field] = getattr(domain_application, field) if field not in info_many_to_many_fields:
elif hasattr(domain_application, field): da_dict[field] = getattr(domain_application, field)
da_many_to_many_dict[field] = getattr(domain_application, field).all() else:
da_many_to_many_dict[field] = getattr(domain_application, field).all()
# Create a placeholder DomainInformation object # Create a placeholder DomainInformation object
domain_info = DomainInformation(**da_dict) domain_info = DomainInformation(**da_dict)
@ -275,5 +274,10 @@ class DomainInformation(TimeStampedModel):
return domain_info return domain_info
@staticmethod
def _get_many_to_many_fields():
"""Returns a set of each field.name that has the many to many relation"""
return {field.name for field in DomainInformation._meta.many_to_many} # type: ignore
class Meta: class Meta:
verbose_name_plural = "Domain information" verbose_name_plural = "Domain information"

View file

@ -1,5 +1,6 @@
import re import re
from typing import Type
from django.db import models
from django import forms from django import forms
from django.http import JsonResponse from django.http import JsonResponse
@ -29,7 +30,7 @@ class DomainHelper:
@classmethod @classmethod
def validate(cls, domain: str, blank_ok=False) -> str: def validate(cls, domain: str, blank_ok=False) -> str:
"""Attempt to determine if a domain name could be requested.""" """Attempt to determine if a domain name could be requested."""
return domain
# Split into pieces for the linter # Split into pieces for the linter
domain = cls._validate_domain_string(domain, blank_ok) domain = cls._validate_domain_string(domain, blank_ok)
@ -158,3 +159,29 @@ class DomainHelper:
"""Get the top level domain. Example: `gsa.gov` -> `gov`.""" """Get the top level domain. Example: `gsa.gov` -> `gov`."""
parts = domain.rsplit(".") parts = domain.rsplit(".")
return parts[-1] if len(parts) > 1 else "" return parts[-1] if len(parts) > 1 else ""
@staticmethod
def get_common_fields(model_1: Type[models.Model], model_2: Type[models.Model]):
"""
Returns a set of field names that two Django models have in common, excluding the 'id' field.
Args:
model_1 (Type[models.Model]): The first Django model class.
model_2 (Type[models.Model]): The second Django model class.
Returns:
Set[str]: A set of field names that both models share.
Example:
If model_1 has fields {"id", "name", "color"} and model_2 has fields {"id", "color"},
the function will return {"color"}.
"""
# Get a list of the existing fields on model_1 and model_2
model_1_fields = set(field.name for field in model_1._meta.get_fields() if field != "id")
model_2_fields = set(field.name for field in model_2._meta.get_fields() if field != "id")
# Get the fields that exist on both DomainApplication and DomainInformation
common_fields = model_1_fields & model_2_fields
return common_fields