Skip to content

Commit

Permalink
Merge pull request #48 from dpgraham4401/pull_manifest_endpoint
Browse files Browse the repository at this point in the history
Pull manifest endpoint
  • Loading branch information
dpgraham4401 authored Jun 22, 2022
2 parents 24400c2 + 283341f commit dbac8ee
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 46 deletions.
1 change: 1 addition & 0 deletions apps/api/serializers/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def to_representation(self, value) -> dict:
class AddressSerializer(TrakSerializer):
streetNumber = serializers.CharField(
source='street_number',
required=False,
)
state = LocalityField('state')
country = LocalityField('country')
Expand Down
85 changes: 58 additions & 27 deletions apps/api/serializers/manifest/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from rest_framework import serializers

from apps.trak.models import Manifest, WasteLine
from apps.trak.models import Handler, Manifest, WasteLine

from ..base import TrakSerializer
from ..handler import HandlerSerializer
Expand All @@ -19,88 +19,113 @@

class ManifestSerializer(TrakSerializer):
createdDate = serializers.DateTimeField(
source='created_date')
source='created_date',
)
updatedDate = serializers.DateTimeField(
source='update_date')
source='update_date',
)
manifestTrackingNumber = serializers.CharField(
source='mtn')
source='mtn',
)
# status
submissionType = serializers.CharField(
source='submission_type')
source='submission_type',
)
signatureStatus = serializers.BooleanField(
source='signature_status',
allow_null=True,
default=False)
default=False,
)
originType = serializers.CharField(
source='origin_type')
source='origin_type',
)
shippedDate = serializers.DateTimeField(
source='shipped_date')
source='shipped_date',
required=False,
)
potentialShipDate = serializers.CharField(
source='potential_ship_date')
source='potential_ship_date',
required=False,
)
receivedDate = serializers.DateTimeField(
source='received_date')
source='received_date',
required=False,
)
certifiedDate = serializers.DateTimeField(
source='certified_date',
allow_null=True,
default=None)
default=None,
)
certifiedBy = serializers.JSONField(
source='certified_by',
allow_null=True,
default=None)
default=None,
)
generator = HandlerSerializer()
transporters = TransporterSerializer(many=True)
designatedFacility = HandlerSerializer(
source='tsd')
source='tsd',
)
# broker
wastes = WasteLineSerializer(many=True)
# rejection
rejectionInfo = serializers.JSONField(
source='rejection_info',
allow_null=True,
default=None)
default=None,
)
# discrepancy
# residue
residueNewManifestTrackingNumbers = serializers.JSONField(
source='residue_new_mtn',
default=[])
default=[],
)
# import, see .to_representation() and .to_internal_value() methods
importInfo = serializers.JSONField(
source='import_info',
allow_null=True,
default=None)
default=None,
)
containsPreviousRejectOrResidue = serializers.BooleanField(
source='contains_residue_or_rejection')
source='contains_residue_or_rejection',
)
printedDocument = serializers.JSONField(
source='printed_document',
allow_null=True,
default=None)
default=None,
)
formDocument = serializers.JSONField(
source='form_document',
allow_null=True,
default=None)
default=None,
)
additionalInfo = serializers.JSONField(
source='additional_info',
allow_null=True,
default=None)
default=None,
)
correctionInfo = serializers.JSONField(
source='correction_info',
allow_null=True,
default=None)

default=None,
)
ppcStatus = serializers.JSONField(
source='ppc_status',
allow_null=True,
default=None)
default=None,
)
# mtnValidationInfo
# provideImageGeneratorInfo
locked = serializers.BooleanField(
allow_null=True,
default=False)
default=False,
)

lockedReason = serializers.CharField(
source='locked_reason',
allow_null=True,
default=None)
default=None,
)

def create(self, validated_data) -> Manifest:
# pop foreign table data
Expand All @@ -109,8 +134,14 @@ def create(self, validated_data) -> Manifest:
tsd_data = validated_data.pop('tsd')
gen_data = validated_data.pop('generator')
# Secondary foreign table data
gen_object = self.create_handler(**gen_data)
tsd_object = self.create_handler(**tsd_data)
if Handler.objects.filter(epa_id=gen_data['epa_id']).exists():
gen_object = Handler.objects.get(epa_id=gen_data['epa_id'])
else:
gen_object = self.create_handler(**gen_data)
if Handler.objects.filter(epa_id=tsd_data['epa_id']).exists():
tsd_object = Handler.objects.get(epa_id=tsd_data['epa_id'])
else:
tsd_object = self.create_handler(**tsd_data)

# Create model instances
manifest = Manifest.objects.create(generator=gen_object,
Expand Down
2 changes: 2 additions & 0 deletions apps/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
path('manifest/<str:mtn>', views.ManifestView.as_view()),
# site
path('handler/<str:epa_id>', views.HandlerView.as_view()),
# rcrainfo integration actions
path('rcrainfo/manifest-pull', views.PullManifest.as_view(), name='manifest-pull'),
# other
path('sync/<str:site_id>', views.SyncSiteManifest.as_view(), name='sync'),
path('tran/<int:tran_id>', views.TransporterView.as_view())
Expand Down
2 changes: 2 additions & 0 deletions apps/api/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .external import HandlerView, ManifestView, TransporterView
from .internal import PullManifest, SyncSiteManifest
21 changes: 4 additions & 17 deletions apps/api/views.py → apps/api/views/external.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import http

from django.core.exceptions import ObjectDoesNotExist
from rest_framework import status
from rest_framework.exceptions import APIException
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.api.serializers import (HandlerSerializer, ManifestSerializer,
TransporterSerializer)
from apps.trak.models import Handler, Manifest, Transporter
from lib.rcrainfo import rcrainfo

from .serializers import (HandlerSerializer, ManifestSerializer,
TransporterSerializer)


class ManifestView(APIView):
Expand All @@ -35,7 +34,7 @@ def get(self, request: Request, mtn: str = None) -> Response:

def post(self, request: Request, mtn: str = None) -> Response:
if not mtn:
return self.response(status=400)
return self.response(status=status.HTTP_400_BAD_REQUEST)
else:
serializer = ManifestSerializer(data=request.data)
valid = serializer.is_valid()
Expand All @@ -46,18 +45,6 @@ def post(self, request: Request, mtn: str = None) -> Response:
return self.response(status=500)


# trash to be fixed, can't be bothered to remove it right now
class SyncSiteManifest(APIView):
response = Response

def get(self, request: Request, epa_id: str = None) -> Response:
if epa_id:
resp = rcrainfo.get_mtns(epa_id)
return Response(data={'mtn': resp.json})
else:
return self.response(status=200)


class HandlerView(APIView):
response = Response

Expand Down
58 changes: 58 additions & 0 deletions apps/api/views/internal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os

from emanifest import client as em
from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.accounts.models import Profile
from apps.api.serializers import ManifestSerializer
from apps.trak.models import Manifest
from lib.rcrainfo import rcrainfo


# ToDo authentication, right now will work as long as user is signed
# into haztrak via the browser for demo purposes. Just Demo!
class PullManifest(APIView):
if os.getenv('RCRAINFO_ENV'):
ri_client = em.new_client(os.getenv('RCRAINFO_ENV'))
else:
ri_client = em.new_client('preprod')

def post(self, request: Request) -> Response:
try:
user_profile = Profile.objects.get(user_id=self.request.user)
if not user_profile.rcra_api_id and user_profile.rcra_api_key:
return Response(status=status.HTTP_401_UNAUTHORIZED, data={
"msg": f'user {user_profile} RCRAInfo API ID and Key '
f'no found, add them to your profile'})
self.ri_client.Auth(user_profile.rcra_api_id, user_profile.rcra_api_key)
data = {'mtn': []}
for mtn in self.request.data['mtn']:
resp = self.ri_client.GetManByMTN(mtn)
data['mtn'].append({mtn: resp.response.status_code})
if resp.response.status_code == 200:
if Manifest.objects.filter(mtn=mtn):
# ToDo update manifest if already downloaded
# see ManifestSerializer .update method
pass
else:
new_manifest = ManifestSerializer(data=resp.json)
new_manifest.is_valid()
new_manifest.save()
return Response(status=status.HTTP_200_OK, data=data)
except KeyError:
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)


# trash, not time remove it right now
class SyncSiteManifest(APIView):
response = Response

def get(self, request: Request, epa_id: str = None) -> Response:
if epa_id:
resp = rcrainfo.get_mtns(epa_id)
return Response(data={'mtn': resp.json})
else:
return self.response(status=status.HTTP_200_OK)
39 changes: 37 additions & 2 deletions apps/trak/templates/trak/site_manifests.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,45 @@
<!-- Page Heading and top level Button -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">{{ site }} Manifests</h1>
<button id="sync-manifest" data-name="{{ site }}"
<button type="button" data-toggle="modal" data-target="#pullManifestModal"
class=" d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i
class="fas fa-download fa-sm text-white-50"></i> Sync with RCRAinfo
class="fas fa-download fa-sm text-white-50"></i> Pull Manifests
</button>
</div>

<!-- Pull manifest modal -->
<div class="modal fade" id="pullManifestModal" tabindex="-1" role="dialog"
aria-labelledby="pullManifestModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header bg-light">
<h5 class="modal-title" id="pullManifestModalLabel">Pull Manifests from RCRAInfo by MTN</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<label for="pullMtnInput">Manifest Tracking Number(s):</label>
<input id="pullMtnInput" type="text" class="form-control" name="mtn">
<i class="text-muted d-flex justify-content-end">Separate MTN by spaces</i>
<div id="pullMtnRespBox" class="justify-content-start px-5 py-1 mr-5 bg-light rounded"
style="min-height: 60px">
<ul id="pullMtnRespList" class="list-unstyled">
<!-- Pull manifest Response box-->
</ul>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" onclick="pullMtn()" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
<script>
</script>

{% if gen_manifests %}
<!-- Generator Manifest Card/row -->
<div class="row">
Expand Down Expand Up @@ -136,4 +170,5 @@ <h6 class="m-0 font-weight-bold text-primary">Receiving Facility</h6>
</div>
<!-- Page level plugins -->
<script src="{% static 'js/trak/sync-db.js' %}"></script>
<script src="{% static 'js/trak/manifest-pull.js' %}"></script>
{% endblock content %}
Loading

0 comments on commit dbac8ee

Please sign in to comment.