Skip to content

Commit

Permalink
372 manager class refactor (#400)
Browse files Browse the repository at this point in the history
* replace create_x Manager methods with 'save(**kwargs)' interface method

add e_signature_factory fixture

* move Serializer create method logic to ModelManagers

* misc fixes, fixture datetime naive warnings, rename module for consistency
  • Loading branch information
dpgraham4401 authored Mar 13, 2023
1 parent 45be025 commit 5661931
Show file tree
Hide file tree
Showing 24 changed files with 249 additions and 203 deletions.
4 changes: 2 additions & 2 deletions .idea/scopes/server.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/apps/trak/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Model definitions for the trak domain
"""
from .address_model import Address, EpaCountries, EpaStates
from .address_model import Address
from .contact_model import Contact, EpaPhone
from .handler_model import Handler, ManifestHandler
from .manifest_model import Manifest
Expand Down
146 changes: 72 additions & 74 deletions server/apps/trak/models/address_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,83 @@
from django.utils.translation import gettext_lazy as _


class EpaCountries(models.TextChoices):
US = "US", _("United States")
MX = "MX", _("Mexico")
CA = "CA", _("Canada")


class EpaStates(models.TextChoices):
AK = "AK", _("Alaska")
AL = "AL", _("Alabama")
AP = "AP", _("Armed Forces Pacific")
AR = "AR", _("Arkansas")
AZ = "AZ", _("Arizona")
CA = "CA", _("California")
CO = "CO", _("Colorado")
CT = "CT", _("Connecticut")
DC = "DC", _("Washington DC")
DE = "DE", _("Delaware")
FL = "FL", _("Florida")
GA = "GA", _("Georgia")
GU = "GU", _("Guam")
HI = "HI", _("Hawaii")
IA = "IA", _("Iowa")
ID = "ID", _("Idaho")
IL = "IL", _("Illinois")
IN = "IN", _("Indiana")
KS = "KS", _("Kansas")
KY = "KY", _("Kentucky")
LA = "LA", _("Louisiana")
MA = "MA", _("Massachusetts")
MD = "MD", _("Maryland")
ME = "ME", _("Maine")
MI = "MI", _("Michigan")
MN = "MN", _("Minnesota")
MO = "MO", _("Missouri")
MS = "MS", _("Mississippi")
MT = "MT", _("Montana")
NC = "NC", _("North Carolina")
ND = "ND", _("North Dakota")
NE = "NE", _("Nebraska")
NH = "NH", _("New Hampshire")
NJ = "NJ", _("New Jersey")
NM = "NM", _("New Mexico")
NV = "NV", _("Nevada")
NY = "NY", _("New York")
OH = "OH", _("Ohio")
OK = "OK", _("Oklahoma")
OR = "OR", _("Oregon")
PA = "PA", _("Pennsylvania")
PR = "PR", _("Puerto Rico")
RI = "RI", _("Rhode Island")
SC = "SC", _("South Carolina")
SD = "SD", _("South Dakota")
TN = "TN", _("Tennessee")
TX = "TX", _("Texas")
UT = "UT", _("Utah")
VA = "VA", _("Virginia")
VI = "VI", _("Virgin Islands")
VT = "VT", _("Vermont")
WA = "WA", _("Washington")
WI = "WI", _("Wisconsin")
WV = "WV", _("West Virginia")
WY = "WY", _("Wyoming")
XA = "XA", _("REGION 01 PURVIEW")
XB = "XB", _("REGION 02 PURVIEW")
XC = "XC", _("REGION 03 PURVIEW")
XD = "XD", _("REGION 04 PURVIEW")
XE = "XE", _("REGION 05 PURVIEW")
XF = "XF", _("REGION 06 PURVIEW")
XG = "XG", _("REGION 07 PURVIEW")
XH = "XH", _("REGION 08 PURVIEW")
XI = "XI", _("REGION 09 PURVIEW")
XJ = "XJ", _("REGION 10 PURVIEW")


class Address(models.Model):
"""
Used to capture RCRAInfo address instances (mail, site).
"""

class EpaCountries(models.TextChoices):
US = "US", _("United States")
MX = "MX", _("Mexico")
CA = "CA", _("Canada")

class EpaStates(models.TextChoices):
AK = "AK", _("Alaska")
AL = "AL", _("Alabama")
AP = "AP", _("Armed Forces Pacific")
AR = "AR", _("Arkansas")
AZ = "AZ", _("Arizona")
CA = "CA", _("California")
CO = "CO", _("Colorado")
CT = "CT", _("Connecticut")
DC = "DC", _("Washington DC")
DE = "DE", _("Delaware")
FL = "FL", _("Florida")
GA = "GA", _("Georgia")
GU = "GU", _("Guam")
HI = "HI", _("Hawaii")
IA = "IA", _("Iowa")
ID = "ID", _("Idaho")
IL = "IL", _("Illinois")
IN = "IN", _("Indiana")
KS = "KS", _("Kansas")
KY = "KY", _("Kentucky")
LA = "LA", _("Louisiana")
MA = "MA", _("Massachusetts")
MD = "MD", _("Maryland")
ME = "ME", _("Maine")
MI = "MI", _("Michigan")
MN = "MN", _("Minnesota")
MO = "MO", _("Missouri")
MS = "MS", _("Mississippi")
MT = "MT", _("Montana")
NC = "NC", _("North Carolina")
ND = "ND", _("North Dakota")
NE = "NE", _("Nebraska")
NH = "NH", _("New Hampshire")
NJ = "NJ", _("New Jersey")
NM = "NM", _("New Mexico")
NV = "NV", _("Nevada")
NY = "NY", _("New York")
OH = "OH", _("Ohio")
OK = "OK", _("Oklahoma")
OR = "OR", _("Oregon")
PA = "PA", _("Pennsylvania")
PR = "PR", _("Puerto Rico")
RI = "RI", _("Rhode Island")
SC = "SC", _("South Carolina")
SD = "SD", _("South Dakota")
TN = "TN", _("Tennessee")
TX = "TX", _("Texas")
UT = "UT", _("Utah")
VA = "VA", _("Virginia")
VI = "VI", _("Virgin Islands")
VT = "VT", _("Vermont")
WA = "WA", _("Washington")
WI = "WI", _("Wisconsin")
WV = "WV", _("West Virginia")
WY = "WY", _("Wyoming")
XA = "XA", _("REGION 01 PURVIEW")
XB = "XB", _("REGION 02 PURVIEW")
XC = "XC", _("REGION 03 PURVIEW")
XD = "XD", _("REGION 04 PURVIEW")
XE = "XE", _("REGION 05 PURVIEW")
XF = "XF", _("REGION 06 PURVIEW")
XG = "XG", _("REGION 07 PURVIEW")
XH = "XH", _("REGION 08 PURVIEW")
XI = "XI", _("REGION 09 PURVIEW")
XJ = "XJ", _("REGION 10 PURVIEW")

street_number = models.CharField(
max_length=12,
null=True,
Expand Down
14 changes: 14 additions & 0 deletions server/apps/trak/models/base_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from abc import ABCMeta, abstractmethod

from django.db import models


class TrakManager(models.Manager, metaclass=ABCMeta):
"""
Abstract class that defines an interface for our model managers
"""

@abstractmethod
def save(self, **kwargs) -> models.QuerySet:
"""Save instance and its related models to the database"""
return self.create(**kwargs)
12 changes: 6 additions & 6 deletions server/apps/trak/models/contact_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from apps.trak.models.base_model import TrakManager


class EpaPhoneNumber(models.CharField):
"""
Expand Down Expand Up @@ -39,26 +41,24 @@ def __str__(self):
return f"{self.number}"


class ContactManager(models.Manager):
class ContactManager(TrakManager):
"""
Inter-model related functionality for Contact Model
"""

def create(self, contact=None, **contact_data):
def save(self, **contact_data) -> models.QuerySet:
"""
Create Contact instance in database, create related phone instance if applicable,
and return the new instance.
"""
if isinstance(contact, Contact):
return contact
if "phone" in contact_data:
phone_data = contact_data.pop("phone")
if isinstance(phone_data, EpaPhone):
phone = phone_data
else:
phone = EpaPhone.objects.create(**phone_data)
return super().create(**contact_data, phone=phone)
return super().create(**contact_data)
return self.create(**contact_data, phone=phone)
return super().save(**contact_data)


class Contact(models.Model):
Expand Down
36 changes: 16 additions & 20 deletions server/apps/trak/models/handler_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from django.db import models

from .address_model import Address
from .base_model import TrakManager
from .contact_model import Contact, EpaPhone
from .signature_model import ESignature, PaperSignature

logger = logging.getLogger(__name__)


class HandlerManager(models.Manager):
class HandlerManager(TrakManager):
"""
Inter-model related functionality for Handler Model
"""
Expand All @@ -20,7 +21,7 @@ def __init__(self):
self.handler_data = None
super().__init__()

def create_handler(self, **handler_data):
def save(self, **handler_data):
"""
Create a handler and its related fields
Expand All @@ -32,14 +33,14 @@ def create_handler(self, **handler_data):
"""
try:
epa_id = handler_data.get("epa_id")
if Handler.objects.filter(epa_id=epa_id).exists():
if self.model.objects.filter(epa_id=epa_id).exists():
return Handler.objects.get(epa_id=epa_id)
self.handler_data = handler_data
new_contact = Contact.objects.create(self.handler_data.pop("contact"))
new_contact = Contact.objects.save(**self.handler_data.pop("contact"))
emergency_phone = self.get_emergency_phone()
site_address = self.get_address("site_address")
mail_address = self.get_address("mail_address")
return super().create(
return super().save(
site_address=site_address,
mail_address=mail_address,
emergency_phone=emergency_phone,
Expand Down Expand Up @@ -166,23 +167,12 @@ def __repr__(self):
return f"<{self.__class__.__name__}({field_values})>"


class ManifestHandlerManager(models.Manager):
class ManifestHandlerManager(TrakManager):
"""
Inter-model related functionality for ManifestHandler Model
"""

def __init__(self):
self.manifest_handler_data = None
super().__init__()

@staticmethod
def create_manifest_handler(**handler_data):
"""
Create a Manifest handler and its related fields
Keyword Args:
handler (dict): handler data in (ordered)dict format
"""
def save(self, **handler_data) -> models.QuerySet:
e_signatures = []
paper_signature = None
if "e_signatures" in handler_data:
Expand All @@ -195,14 +185,14 @@ def create_manifest_handler(**handler_data):
handler = Handler.objects.get(epa_id=handler_data["handler"]["epa_id"])
logger.debug(f"using existing Handler {handler}")
else:
handler = Handler.objects.create_handler(**handler_data["handler"])
handler = Handler.objects.save(**handler_data["handler"])
logger.debug(f"Handler created {handler}")
manifest_handler = ManifestHandler.objects.create(
handler=handler, paper_signature=paper_signature
)
logger.debug(f"ManifestHandler created {manifest_handler}")
for e_signature_data in e_signatures:
e_sig = ESignature.objects.create_e_signature(
e_sig = ESignature.objects.save(
manifest_handler=manifest_handler, **e_signature_data
)
logger.debug(f"ESignature created {e_sig}")
Expand Down Expand Up @@ -237,3 +227,9 @@ class ManifestHandler(models.Model):

def __str__(self):
return f"ManifestHandler: {self.handler.epa_id}"

def __repr__(self):
field_values = ", ".join(
f"{field.name}={getattr(self, field.name)!r}" for field in self._meta.fields
)
return f"<{self.__class__.__name__}({field_values})>"
Loading

0 comments on commit 5661931

Please sign in to comment.