Add initial federal agency model migration

This commit is contained in:
Erin 2024-03-19 10:36:14 -07:00
parent d920c890df
commit 7b5beba813
No known key found for this signature in database
GPG key ID: 1CAD275313C62460
4 changed files with 56 additions and 31 deletions

View file

@ -15,7 +15,7 @@ from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from dateutil.relativedelta import relativedelta # type: ignore
from epplibwrapper.errors import ErrorCode, RegistryError
from registrar.models import Contact, Domain, DomainRequest, DraftDomain, User, Website
from registrar.models import Contact, Domain, DomainRequest, DraftDomain, User, Website, FederalAgency
from registrar.utility import csv_export
from registrar.utility.errors import FSMApplicationError, FSMErrorCodes
from registrar.views.utility.mixins import OrderableFieldsMixin
@ -1839,6 +1839,7 @@ admin.site.register(models.DomainInvitation, DomainInvitationAdmin)
admin.site.register(models.DomainInformation, DomainInformationAdmin)
admin.site.register(models.Domain, DomainAdmin)
admin.site.register(models.DraftDomain, DraftDomainAdmin)
admin.site.register(FederalAgency)
# Host and HostIP removed from django admin because changes in admin
# do not propagate to registry and logic not applied
admin.site.register(models.Host, MyHostAdmin)

View file

@ -0,0 +1,26 @@
# Generated by Django 4.2.10 on 2024-03-19 05:42
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("registrar", "0076_alter_domainrequest_current_websites_and_more"),
]
operations = [
migrations.CreateModel(
name="FederalAgency",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("agency", models.CharField(blank=True, help_text="Federal agency", null=True)),
],
options={
"verbose_name": "Federal agency",
"verbose_name_plural": "Federal agencies",
},
),
]

View file

@ -4,6 +4,7 @@ from .domain_request import DomainRequest
from .domain_information import DomainInformation
from .domain import Domain
from .draft_domain import DraftDomain
from .federal_agency import FederalAgency
from .host_ip import HostIP
from .host import Host
from .domain_invitation import DomainInvitation
@ -22,6 +23,7 @@ __all__ = [
"Domain",
"DraftDomain",
"DomainInvitation",
"FederalAgency",
"HostIP",
"Host",
"UserDomainRole",
@ -39,6 +41,7 @@ auditlog.register(Domain)
auditlog.register(DraftDomain)
auditlog.register(DomainInvitation)
auditlog.register(DomainInformation)
auditlog.register(FederalAgency)
auditlog.register(HostIP)
auditlog.register(Host)
auditlog.register(UserDomainRole)

View file

@ -1,15 +1,22 @@
from django.contrib.auth.models import Group
from .utility.time_stamped_model import TimeStampedModel
from django.db import models
import logging
logger = logging.getLogger(__name__)
# TODO: Update param model
class FederalAgency(Group):
class FederalAgency(TimeStampedModel):
class Meta:
verbose_name = "Federal agency"
verbose_name_plural = "Federal agencies"
agency = models.CharField(
null=True,
blank=True,
help_text="Federal agency",
)
# TODO: Update parameters to put in
def create_federal_agencies(apps, schema_editor):
"""This method gets run from a data migration."""
@ -17,6 +24,7 @@ class FederalAgency(Group):
# Hard to pass self to these methods as the calls from migrations
# are only expecting apps and schema_editor, so we'll just define
# apps, schema_editor in the local scope instead
AGENCIES = [
"Administrative Conference of the United States",
"Advisory Council on Historic Preservation",
@ -203,29 +211,16 @@ class FederalAgency(Group):
"World War I Centennial Commission",
]
# TODO: Get apps back here
FederalAgency = apps.get_model("registrar", "FederalAgency")
FederalAgency = apps.get_model("registrar", "FederalAgency")
logger.info("Creating federal agency table.")
try:
federal_agencies_list, _ = FederalAgency.objects.get_or_create(
name="cisa_analysts_group",
)
federal_agencies_list.federal_agency.clear()
# TODO: Why is AGENCIES not loading here?
for agency in AGENCIES:
# Assign the permissions to the group
federal_agencies_list.agency.add(*agency)
# TODO: Maybe remove?
# Convert the permissions QuerySet to a list of codenames
agency_list = list(agency.values_list("codename", flat=True))
logger.debug(agency + " added to group " + federal_agencies_list.name)
federal_agencies_list.save()
logger.debug("Federal agency added to table " + federal_agencies_list.name)
except Exception as e:
logger.error(f"Error creating fedearl agency: {e}")
try:
for agency in AGENCIES:
federal_agencies_list, _ = FederalAgency.objects.get_or_create(
agency=agency,
)
logger.debug(agency + " added to record " + federal_agencies_list.agency)
federal_agencies_list.save()
logger.debug("Federal agency added to table " + federal_agencies_list.agency)
except Exception as e:
logger.error(f"Error creating federal agency: {e}")