diff --git a/src/registrar/fixtures.py b/src/registrar/fixtures.py index ee226b7cb..171fd3e22 100644 --- a/src/registrar/fixtures.py +++ b/src/registrar/fixtures.py @@ -43,6 +43,11 @@ class UserFixture: "username": "2ffe71b0-cea4-4097-8fb6-7a35b901dd70", "first_name": "Neil", "last_name": "Martinsen-Burrell", + }, + { + "username": "7185e6cd-d3c8-4adc-90a3-ceddba71d24f", + "first_name": "Jon", + "last_name": "Roberts", }, ] diff --git a/src/registrar/migrations/0018_domaininformation.py b/src/registrar/migrations/0018_domaininformation.py index c85943990..ef6a28226 100644 --- a/src/registrar/migrations/0018_domaininformation.py +++ b/src/registrar/migrations/0018_domaininformation.py @@ -1,4 +1,4 @@ -# Generated by Django 4.1.6 on 2023-05-08 12:48 +# Generated by Django 4.1.6 on 2023-05-08 15:30 from django.conf import settings from django.db import migrations, models @@ -233,7 +233,7 @@ class Migration(migrations.Migration): ), ( "domain", - models.ForeignKey( + models.OneToOneField( blank=True, help_text="Domain to which this information belongs", null=True, @@ -242,6 +242,17 @@ class Migration(migrations.Migration): to="registrar.domain", ), ), + ( + "domain_application", + models.OneToOneField( + blank=True, + help_text="Associated domain application", + null=True, + on_delete=django.db.models.deletion.PROTECT, + related_name="domainapplication_info", + to="registrar.domainapplication", + ), + ), ( "investigator", models.ForeignKey( diff --git a/src/registrar/models/domain_application.py b/src/registrar/models/domain_application.py index 5e50c88e9..bdcde15cc 100644 --- a/src/registrar/models/domain_application.py +++ b/src/registrar/models/domain_application.py @@ -527,7 +527,7 @@ class DomainApplication(TimeStampedModel): # remove PK from domainapplication as it use different PK # for domain/domaininformation - domain_info, _ = DomainInformation.create_from_da_dict(domain_info) + domain_info = DomainInformation.create_from_da(self) # create the permission for the user UserDomainRole = apps.get_model("registrar.UserDomainRole") @@ -595,7 +595,7 @@ class DomainApplication(TimeStampedModel): # import pdb; pdb.set_trace() if field.get_internal_type() in ("ForeignKey", "OneToOneField"): # get the related instance of the FK value - print(f"{field.name}: ID: {field.value_from_object(instance)}") + # print(f"{field.name}: ID: {field.value_from_object(instance)}") fk_id = field.value_from_object(instance) if fk_id: data[field.name] = field.related_model.objects.get(id=fk_id) diff --git a/src/registrar/models/domain_information.py b/src/registrar/models/domain_information.py index b6adc6c52..ab7f75247 100644 --- a/src/registrar/models/domain_information.py +++ b/src/registrar/models/domain_information.py @@ -13,7 +13,7 @@ logger = logging.getLogger(__name__) class DomainInformation(TimeStampedModel): - """A registrant's application for a new domain.""" + """A registrant's domain information for that domain, exported from DomainApplication.""" class StateTerritoryChoices(models.TextChoices): ALABAMA = "AL", "Alabama (AL)" @@ -264,6 +264,16 @@ class DomainInformation(TimeStampedModel): related_name="information_investigating", ) + domain_application = models.OneToOneField( + "registrar.DomainApplication", + on_delete=models.PROTECT, + blank=True, + null=True, + related_name="domainapplication_info", + help_text="Associated domain application", + unique=True + ) + # ##### data fields from the initial form ##### organization_type = models.CharField( max_length=255, @@ -370,7 +380,7 @@ class DomainInformation(TimeStampedModel): on_delete=models.PROTECT, ) - domain = models.ForeignKey( + domain = models.OneToOneField( "registrar.Domain", on_delete=models.PROTECT, blank=True, @@ -440,10 +450,15 @@ class DomainInformation(TimeStampedModel): return "" @classmethod - def create_from_da_dict(cls, da_dict): + def create_from_da(cls, domain_application): """Takes in a DomainApplication dict and converts it into DomainInformation""" - # we don't want to pass the id to avoid conflicts - da_dict.pop("id") + da_dict = domain_application.to_dict() + # remove the id so one can be assinged on creation + da_id = da_dict.pop("id") + # check if we have a record that corresponds with the domain application, if so short circuit the create + domain_info = cls.objects.filter(domain_application__id=da_id).first() + if domain_info: + return domain_info # the following information below is not needed in the domain information: da_dict.pop("status") da_dict.pop("current_websites") @@ -452,7 +467,7 @@ class DomainInformation(TimeStampedModel): other_contacts = da_dict.pop("other_contacts") alternative_domains = da_dict.pop("alternative_domains") #just in case domain_info = cls(**da_dict) - + domain_info.domain_application = domain_application #Save so the object now have PK (needed to process the manytomany below before first) domain_info.save()