Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc Technical debt and clean up #397

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.1.7 on 2023-03-13 11:14

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("trak", "0004_additionalinfo"),
]

operations = [
migrations.AlterField(
model_name="address",
name="city",
field=models.CharField(blank=True, max_length=25, null=True),
),
migrations.AlterField(
model_name="contact",
name="email",
field=models.EmailField(blank=True, max_length=254, null=True),
),
]
8 changes: 8 additions & 0 deletions server/apps/trak/models/address_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class Address(models.Model):
)
city = models.CharField(
max_length=25,
null=True,
blank=True,
)
state = models.CharField(
max_length=3,
Expand All @@ -122,3 +124,9 @@ def __str__(self):
if self.street_number:
return f"{self.street_number} {self.address1}"
return f" {self.address1}"

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})>"
21 changes: 12 additions & 9 deletions server/apps/trak/models/contact_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ class Contact(models.Model):
null=True,
blank=True,
)
email = models.EmailField()
email = models.EmailField(
null=True,
blank=True,
)
company_name = models.CharField(
max_length=80,
null=True,
Expand All @@ -99,15 +102,15 @@ class Contact(models.Model):

def __str__(self):
try:
first = self.first_name if self.first_name else ""
middle = self.middle_initial if self.middle_initial else ""
last = self.last_name if self.last_name else ""
return (
f"contact {self.pk}: {first.capitalize()} {middle.capitalize()} "
f"{last.capitalize()}"
)
first = self.first_name or ""
middle = self.middle_initial or ""
last = self.last_name or ""
return f"{first.capitalize()} {middle.capitalize()} {last.capitalize()}"
except AttributeError:
return f"contact {self.pk}: {self.first_name} {self.middle_initial} {self.last_name}"

def __repr__(self):
return f"Contact {self.pk}"
field_values = ", ".join(
f"{field.name}={getattr(self, field.name)!r}" for field in self._meta.fields
)
return f"<{self.__class__.__name__}({field_values})>"
12 changes: 12 additions & 0 deletions server/apps/trak/models/handler_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def create_handler(self, **handler_data):
except KeyError as exc:
logger.warning(f"error while creating handler {exc}")

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})>"

def get_emergency_phone(self) -> Union[EpaPhone, None]:
"""Check if emergency phone is present and create an EpaPhone row"""
try:
Expand Down Expand Up @@ -153,6 +159,12 @@ class Handler(models.Model):
def __str__(self):
return f"{self.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})>"


class ManifestHandlerManager(models.Manager):
"""
Expand Down
9 changes: 9 additions & 0 deletions server/apps/trak/models/manifest_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,12 @@ class NewDestination(models.TextChoices):
class Meta:
verbose_name = "AdditionalInfo"
verbose_name_plural = "AdditionalInfo"

def __str__(self):
return f"{self.original_mtn or 'Unknown'}"

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})>"
6 changes: 6 additions & 0 deletions server/apps/trak/models/rcra_profile_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class RcraProfile(models.Model):
def __str__(self):
return f"{self.user.username}"

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})>"

def sync(self):
"""Launch task to sync use profile. ToDo: remove this method"""
from ..tasks import sync_user_sites
Expand Down
5 changes: 3 additions & 2 deletions server/apps/trak/models/signature_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ def __str__(self):
return f"{self.printed_name} ({self.sign_date.strftime('%Y-%m-%d %H:%M:%S')})"

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

def __hash__(self):
return hash((self.printed_name, self.sign_date))
6 changes: 6 additions & 0 deletions server/apps/trak/models/site_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ class Site(models.Model):

def __str__(self):
return f"{self.epa_site.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})>"
7 changes: 7 additions & 0 deletions server/apps/trak/models/site_permission_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class SitePermission(models.Model):
def __str__(self):
return f"{self.profile.user}: {self.site}"

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})>"

def clean(self):
if self.site_manager:
fields = ["annual_report", "biennial_report", "e_manifest", "my_rcra_id", "wiets"]
Expand Down
6 changes: 6 additions & 0 deletions server/apps/trak/models/transporter_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ class Transporter(ManifestHandler):

def __str__(self):
return f"{self.handler.epa_id}: transporter {self.order} on {self.manifest.mtn}"

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})>"
6 changes: 6 additions & 0 deletions server/apps/trak/models/waste_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ class WasteLine(models.Model):

def __str__(self):
return f"{self.manifest} line {self.line_number}"

def __repr__(self):
field_values = ", ".join(
f"{field.name}={getattr(self, field.name)!r}" for field in self._meta.fields
)
return f"<WasteLine({field_values})>"
8 changes: 7 additions & 1 deletion server/apps/trak/serializers/trak_ser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ class TrakBaseSerializer(serializers.ModelSerializer):
across trak app serializers universally.
"""

def __str__(self):
return f"<{self.__class__.__name__}>"

def __repr__(self):
return f"<{self.__class__.__name__}({self.data})>"

def to_representation(self, instance):
"""
Remove empty fields when serializing to JSON
Remove empty fields when serializing
"""
data = super().to_representation(instance)
for field in self.fields:
Expand Down
16 changes: 8 additions & 8 deletions server/apps/trak/services/handler_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class HandlerService:

def __init__(self, *, username: str, rcrainfo: RcrainfoService = None, logger: Logger = None):
self.username = username
if rcrainfo is not None:
self.rcrainfo = rcrainfo
else:
self.rcrainfo = RcrainfoService(api_username=self.username)
if logger:
self.logger = logger
else:
self.logger = logging.getLogger(__name__)
self.rcrainfo = rcrainfo or RcrainfoService(api_username=self.username)
self.logger = logger or logging.getLogger(__name__)

def __repr__(self):
return (
f"<{self.__class__.__name__}(api_username='{self.username}', "
f"rcrainfo='{self.rcrainfo}')>"
)

def pull_rcra_handler(self, *, site_id: str) -> Handler:
"""
Expand Down
15 changes: 7 additions & 8 deletions server/apps/trak/services/manifest_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ class ManifestService:

def __init__(self, *, username: str, rcrainfo: RcrainfoService = None, logger: Logger = None):
self.username = username
if rcrainfo is not None:
self.rcrainfo = rcrainfo
else:
self.rcrainfo = RcrainfoService(api_username=self.username)
if logger:
self.logger = logger
else:
self.logger = logging.getLogger(__name__)
self.rcrainfo = rcrainfo or RcrainfoService(api_username=self.username)
self.logger = logger or logging.getLogger(__name__)

def __repr__(self):
return (
f"<{self.__class__.__name__}(username='{self.username}', rcrainfo='{self.rcrainfo}')>"
)

def _retrieve_manifest(self, mtn: str):
response = self.rcrainfo.get_manifest(mtn)
Expand Down
15 changes: 7 additions & 8 deletions server/apps/trak/services/profile_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ class RcraProfileService:
def __init__(self, *, username: str, rcrainfo: RcrainfoService = None, logger: Logger = None):
self.username = username
self.profile, created = RcraProfile.objects.get_or_create(user__username=self.username)
if rcrainfo is not None:
self.rcrainfo = rcrainfo
else:
self.rcrainfo = RcrainfoService(api_username=self.username)
if logger:
self.logger = logger
else:
self.logger = logging.getLogger(__name__)
self.rcrainfo = rcrainfo or RcrainfoService(api_username=self.username)
self.logger = logger or logging.getLogger(__name__)

@property
def can_access_rcrainfo(self) -> bool:
Expand All @@ -47,6 +41,11 @@ def can_access_rcrainfo(self) -> bool:
return True
return False

def __repr__(self):
return (
f"<{self.__class__.__name__}(username='{self.username}', rcrainfo='{self.rcrainfo}')>"
)

def pull_rcra_profile(self, *, username: str = None):
"""
This high level function makes several requests to RCRAInfo to pull...
Expand Down
22 changes: 16 additions & 6 deletions server/apps/trak/services/rcrainfo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ def __init__(self, *, api_username: str, rcrainfo_env: str = None, **kwargs):
self.profile = None
if rcrainfo_env is None:
rcrainfo_env = os.getenv("HT_RCRAINFO_ENV", "preprod")
self.rcrainfo_env = rcrainfo_env
super().__init__(rcrainfo_env, **kwargs)

@property
def has_api_user(self):
def has_api_user(self) -> bool:
"""returns boolean if the assigned API user has credentials"""
return self.profile.is_api_user
try:
return self.profile.is_api_user
except AttributeError:
return False

def __repr__(self):
return (
f"<{self.__class__.__name__}(api_username='{self.api_user}', "
f"rcrainfo_env='{self.rcrainfo_env}')>"
)

def retrieve_id(self, api_id=None) -> str:
"""Override RcrainfoClient method to retrieve API ID for authentication"""
Expand All @@ -44,9 +54,9 @@ def get_user_profile(self, username: str = None):
haztrak user to have their unique RCRAInfo user and API credentials in their
RcraProfile
"""
if username:
profile = RcraProfile.objects.get(user__username=username)
else:
profile = RcraProfile.objects.get(user__username=self.api_user)
profile = RcraProfile.objects.get(user__username=username or self.api_user)
response = self.search_users(userId=profile.rcra_username)
return response.json()

def __bool__(self):
return True
14 changes: 6 additions & 8 deletions server/apps/trak/services/site_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ class SiteService:

def __init__(self, *, username: str, site_id: str = None, rcrainfo: RcrainfoService = None):
self.username = username
if rcrainfo is not None:
self.rcrainfo = rcrainfo
else:
self.rcrainfo = RcrainfoService(api_username=username)
self.rcrainfo = rcrainfo or RcrainfoService(api_username=username)
if site_id:
self.site = Site.objects.get(epa_site__epa_id=site_id)

Expand All @@ -35,17 +32,20 @@ def sync_rcra_manifest(self, *, site_id: str = None) -> Dict[str, List[str]]:
logger.info(f"{self} sync rcra manifest, site ID {site_id}")
try:
manifest_service = ManifestService(username=self.username, rcrainfo=self.rcrainfo)
site = Site.objects.get(epa_site__epa_id=site_id)
site = Site.objects.get(epa_site__epa_id=site_id or self.site)
logger.info(f"site: {site}, manifest_service: {manifest_service}")
tracking_numbers: List[str] = manifest_service.search_rcra_mtn(
site_id=site_id, start_date=site.last_rcra_sync
)
# ToDo: uncomment this after we have manifest development fixtures
# limit the number of manifest to sync at a time
tracking_numbers = tracking_numbers[0:10]
logger.info(f"Pulling {tracking_numbers} from RCRAInfo")
results: Dict[str, List[str]] = manifest_service.pull_manifests(
tracking_numbers=tracking_numbers
)
# ToDo: uncomment this after we have manifest development fixtures
# Update the Rcrainfo last sync date for future sync operations
# site.last_rcra_sync = datetime.now().replace(tzinfo=timezone.utc)
site.save()
return results
Expand All @@ -57,7 +57,6 @@ def sync_rcra_manifest(self, *, site_id: str = None) -> Dict[str, List[str]]:
def create_or_update_site(self, *, handler: Handler, site_name: str = None) -> Site:
"""
Retrieve a site from the database or create.
# ToDo convert to create_or_update_site()

Keyword Args:
handler (Handler): An instance of the (hazardous waste) Handler model
Expand All @@ -66,7 +65,6 @@ def create_or_update_site(self, *, handler: Handler, site_name: str = None) -> S
if site_name is None:
site_name = handler.name
if Site.objects.filter(epa_site__epa_id=handler.epa_id).exists():
site = Site.objects.get(epa_site__epa_id=handler.epa_id)
return site
return Site.objects.get(epa_site__epa_id=handler.epa_id)
else:
return Site.objects.create(epa_site=handler, name=site_name)
1 change: 0 additions & 1 deletion server/apps/trak/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .handler_task import get_handler
from .lookup_task import pull_federal_codes
from .manifest_task import pull_manifest
from .poc_task import say_hello
from .profile_task import sync_user_sites
from .site_task import sync_site_manifests
10 changes: 0 additions & 10 deletions server/apps/trak/tasks/poc_task.py

This file was deleted.

Loading