mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-15 17:17:02 +02:00
Implement PR suggestions
This commit is contained in:
parent
dc5e0293f6
commit
c75c4bb7a8
5 changed files with 47 additions and 16 deletions
|
@ -994,7 +994,7 @@ class DomainAdmin(ListHeaderAdmin):
|
|||
fieldsets = (
|
||||
(
|
||||
None,
|
||||
{"fields": ["name", "state", "expiration_date", "first_ready", "deleted", "domain_notes"]},
|
||||
{"fields": ["name", "state", "expiration_date", "first_ready", "deleted", "notes"]},
|
||||
),
|
||||
)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
@ -11,7 +11,7 @@ class Migration(migrations.Migration):
|
|||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="domain",
|
||||
name="domain_notes",
|
||||
name="notes",
|
||||
field=models.TextField(blank=True, help_text="Notes about this domain", null=True),
|
||||
),
|
||||
migrations.AddField(
|
|
@ -992,7 +992,7 @@ class Domain(TimeStampedModel, DomainHelper):
|
|||
help_text="The last time this domain moved into the READY state",
|
||||
)
|
||||
|
||||
domain_notes = models.TextField(
|
||||
notes = models.TextField(
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Notes about this domain",
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from __future__ import annotations
|
||||
from django.db import transaction
|
||||
|
||||
from registrar.models.utility.domain_helper import DomainHelper
|
||||
from .domain_application import DomainApplication
|
||||
from .utility.time_stamped_model import TimeStampedModel
|
||||
|
||||
|
@ -236,15 +238,11 @@ class DomainInformation(TimeStampedModel):
|
|||
if existing_domain_info:
|
||||
return existing_domain_info
|
||||
|
||||
# Get a list of the existing fields on DomainApplication and DomainInformation
|
||||
domain_app_fields = set(field.name for field in DomainApplication._meta.get_fields() if field != "id")
|
||||
domain_info_fields = set(field.name for field in DomainInformation._meta.get_fields() if field != "id")
|
||||
# Get the fields that exist on both DomainApplication and DomainInformation
|
||||
common_fields = DomainHelper.get_common_fields(DomainApplication, DomainInformation)
|
||||
|
||||
# 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
|
||||
|
||||
# Get the fields that exist on both DomainApplication and DomainInformation
|
||||
common_fields = domain_app_fields & domain_info_fields
|
||||
info_many_to_many_fields = DomainInformation._get_many_to_many_fields()
|
||||
|
||||
# Create a dictionary with only the common fields, and create a DomainInformation from it
|
||||
da_dict = {}
|
||||
|
@ -253,9 +251,10 @@ class DomainInformation(TimeStampedModel):
|
|||
# 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 hasattr(domain_application, field) and field not in info_many_to_many_fields:
|
||||
da_dict[field] = getattr(domain_application, field)
|
||||
elif hasattr(domain_application, field):
|
||||
da_many_to_many_dict[field] = getattr(domain_application, field).all()
|
||||
if field not in info_many_to_many_fields:
|
||||
da_dict[field] = getattr(domain_application, field)
|
||||
else:
|
||||
da_many_to_many_dict[field] = getattr(domain_application, field).all()
|
||||
|
||||
# Create a placeholder DomainInformation object
|
||||
domain_info = DomainInformation(**da_dict)
|
||||
|
@ -275,5 +274,10 @@ class DomainInformation(TimeStampedModel):
|
|||
|
||||
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:
|
||||
verbose_name_plural = "Domain information"
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import re
|
||||
|
||||
from typing import Type
|
||||
from django.db import models
|
||||
from django import forms
|
||||
from django.http import JsonResponse
|
||||
|
||||
|
@ -29,7 +30,7 @@ class DomainHelper:
|
|||
@classmethod
|
||||
def validate(cls, domain: str, blank_ok=False) -> str:
|
||||
"""Attempt to determine if a domain name could be requested."""
|
||||
|
||||
return domain
|
||||
# Split into pieces for the linter
|
||||
domain = cls._validate_domain_string(domain, blank_ok)
|
||||
|
||||
|
@ -158,3 +159,29 @@ class DomainHelper:
|
|||
"""Get the top level domain. Example: `gsa.gov` -> `gov`."""
|
||||
parts = domain.rsplit(".")
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue