From acd804b548fc5ee416cb7469751d46acb954d0d9 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Tue, 10 Oct 2023 08:53:24 -0600 Subject: [PATCH] Fix bug with the DELETED state --- src/registrar/models/domain.py | 45 ++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/registrar/models/domain.py b/src/registrar/models/domain.py index 91f4299e5..3fd0b0997 100644 --- a/src/registrar/models/domain.py +++ b/src/registrar/models/domain.py @@ -880,8 +880,8 @@ class Domain(TimeStampedModel, DomainHelper): return self._handle_registrant_contact(desired_contact) - _registry_id: str - if contact_type in contacts: + _registry_id: str = "" + if contacts is not None and contact_type in contacts: _registry_id = contacts.get(contact_type) desired = PublicContact.objects.filter( @@ -1293,6 +1293,47 @@ class Domain(TimeStampedModel, DomainHelper): except RegistryError as e: logger.error(e) + except TransitionNotAllowed: + # Fixes a bug with _fetch_cache trying to create + # a deleted domain, as cache gets cleared on delete. + # Instead, serve what we have locally. + if self.state == self.State.DELETED: + logger.warning("Attempted to create a deleted domain") + data = self._cache + choices = PublicContact.ContactTypeChoices + contacts_dict = { + choices.ADMINISTRATIVE: None, + choices.SECURITY: None, + choices.TECHNICAL: None, + } + registrant_id = ... + existing_contacts = PublicContact.objects.filter( + domain=self + ) + if existing_contacts.count() > 0: + for choice in contacts_dict: + contacts_dict[choice] = existing_contacts.get(contact_type=choice).registry_id + # Edge case for registrant + registrant = PublicContact.ContactTypeChoices.REGISTRANT + registrant_id = existing_contacts.get(contact_type=registrant).registry_id + + cache = { + "auth_info": getattr(data, "auth_info", ...), + "contacts": getattr(data, "contacts", contacts_dict), + "cr_date": getattr(data, "cr_date", ...), + "ex_date": getattr(data, "ex_date", ...), + "hosts": getattr(data, "hosts", ...), + "name": getattr(data, "name", self.name), + "registrant": getattr(data, "name", registrant_id), + "statuses": getattr(data, "statuses", ...), + "tr_date": getattr(data, "tr_date", ...), + "up_date": getattr(data, "up_date", ...), + } + cleaned = {k: v for k, v in cache.items() if v is not ...} + + self._cache = cleaned + + def _get_or_create_public_contact(self, public_contact: PublicContact): """Tries to find a PublicContact object in our DB. If it can't, it'll create it. Returns PublicContact"""