Skip to content

Commit

Permalink
Merge pull request #16 from shibaken/compliance_mgt
Browse files Browse the repository at this point in the history
shibaken/compliance-mgt to djandwich/compliance-mgt
  • Loading branch information
Djandwich authored Feb 24, 2021
2 parents 40f352c + a01a474 commit d5ddb2f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 56 deletions.
4 changes: 4 additions & 0 deletions wildlifecompliance/components/offence/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def __str__(self):
def retrieve_penalty_amounts_by_date(self, date_of_issue):
return self.section_regulation.retrieve_penalty_amounts_by_date(date_of_issue)

@property
def act(self):
return self.section_regulation.act

@property
def dotag_offence_code(self):
return self.section_regulation.dotag_offence_code
Expand Down
1 change: 0 additions & 1 deletion wildlifecompliance/components/sanction_outcome/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from wildlifecompliance.components.sanction_outcome.pdf_infringement_notice_blue import create_infringement_notice_blue
from wildlifecompliance.components.sanction_outcome.pdf_letter_of_advice import create_letter_of_advice_pdf_bytes
from wildlifecompliance.components.sanction_outcome.pdf_remediation_notice import create_remediation_notice_pdf_bytes
from wildlifecompliance.doctopdf import create_infringement_notice_pdf_contents

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,16 @@ def _create_pdf(invoice_buffer, sanction_outcome):


def create_infringement_notice_blue(filename, sanction_outcome):
with BytesIO() as invoice_buffer:
_create_pdf(invoice_buffer, sanction_outcome)
value = create_infringement_notice_pdf_contents(filename, sanction_outcome)
content = ContentFile(value)

# Get the value of the BytesIO buffer
# value = invoice_buffer.getvalue()
value = create_infringement_notice_pdf_contents(filename)
content = ContentFile(value)
# START: Save the pdf file to the database
document = sanction_outcome.documents.create(name=filename)
path = default_storage.save('wildlifecompliance/{}/{}/documents/{}'.format(sanction_outcome._meta.model_name, sanction_outcome.id, filename), content)
document._file = path
document.save()
# END: Save

# START: Save the pdf file to the database
document = sanction_outcome.documents.create(name=filename)
# path = default_storage.save('wildlifecompliance/{}/{}/documents/{}'.format(sanction_outcome._meta.model_name, sanction_outcome.id, filename), invoice_buffer)
path = default_storage.save('wildlifecompliance/{}/{}/documents/{}'.format(sanction_outcome._meta.model_name, sanction_outcome.id, filename), content)
document._file = path
document.save()
# END: Save

invoice_buffer.close()

return document
return document


7 changes: 7 additions & 0 deletions wildlifecompliance/components/sanction_outcome/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,13 @@ def validate(self, obj):
return obj


class SanctionOutcomeNoticeSerializer(serializers.ModelSerializer):


class Meta:
model = SanctionOutcome


class SanctionOutcomeUserActionSerializer(serializers.ModelSerializer):
# who = serializers.CharField(source='who.get_full_name')
who = serializers.SerializerMethodField()
Expand Down
1 change: 1 addition & 0 deletions wildlifecompliance/components/section_regulation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Meta:
def __str__(self):
return self.name


class SectionRegulation(RevisionedMixin):
#act = models.CharField(max_length=100, blank=True)
act = models.ForeignKey(Act, related_name='section_regulations')
Expand Down
89 changes: 51 additions & 38 deletions wildlifecompliance/doctopdf.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
import logging
import os
from django.conf import settings
from docxtpl import DocxTemplate
# from disturbance.components.main.models import ApiaryGlobalSettings
from wildlifecompliance.components.main.models import SanctionOutcomeWordTemplate
from wildlifecompliance.components.sanction_outcome.models import AllegedCommittedOffence


def create_infringement_notice_pdf_contents(pdf_filename):
#import ipdb; ipdb.set_trace()
# print ("Letter File")
# confirmation_doc = None
# if booking.annual_booking_period_group.letter:
# print (booking.annual_booking_period_group.letter.path)
# confirmation_doc = booking.annual_booking_period_group.letter.path
# confirmation_doc = settings.BASE_DIR+"/mooring/templates/doc/AnnualAdmissionStickerLetter.docx"
logger = logging.getLogger(__name__)

# licence_template = ApiaryGlobalSettings.objects.get(key=ApiaryGlobalSettings.KEY_APIARY_LICENCE_TEMPLATE_FILE)

# if licence_template._file:
# path_to_template = licence_template._file.path
# else:
# Use default template file
# path_to_template = os.path.join(settings.BASE_DIR, 'disturbance', 'static', 'disturbance', 'apiary_authority_template.docx')
def create_infringement_notice_pdf_contents(pdf_filename, sanction_outcome):
acos = AllegedCommittedOffence.objects.filter(sanction_outcome=sanction_outcome, included=True)
if acos.count() == 1:
alleged_offence = acos[0].alleged_offence
else:
# Should not reach here. For an infringement notice, there should be only one alleged offence.
str = 'Sanction Outcome: {} has {} alleged offences. There should be 1.'.format(sanction_outcome.lodgement_nubmer, acos.count())
logger.error(str)
raise Exception(str)

path_to_template = os.path.join(settings.BASE_DIR, 'wildlifecompliance', 'static', 'wildlifecompliance', 'infringement-notice-bca.docx')
test = SanctionOutcomeWordTemplate.objects.all().first()
path_to_template = test._file.path
# Retrieve the latest template which matches the Act and SanctionOutcome type.
try:
so_outcome_template = SanctionOutcomeWordTemplate.objects.filter(act=alleged_offence.act, sanction_outcome_type=sanction_outcome.type).order_by('-id').first()
except:
raise Exception('Template not found for the Act: {} and Sanction Outcome type: {}'.format(alleged_offence.act, sanction_outcome.type))

path_to_template = so_outcome_template._file.path

doc = DocxTemplate(path_to_template)
# address = ''
# if len(booking.details.get('postal_address_line_2', '')) > 0:
# address = '{}, {}'.format(booking.details.get('postal_address_line_1', ''),
# booking.details.get('postal_address_line_2', ''))
# else:
# address = '{}'.format(booking.details.get('postal_address_line_1', ''))
# bookingdate = booking.created + timedelta(hours=8)
# todaydate = datetime.utcnow() + timedelta(hours=8)
# stickercreated = ''
# if booking.sticker_created:
# sc = booking.sticker_created + timedelta(hours=8)
# stickercreated = sc.strftime('%d %B %Y')
# from disturbance.components.approvals.serializers import ApprovalSerializerForLicenceDoc
# serializer_context = {
# 'approver': approver,
# 'site_transfer_preview': site_transfer_preview,
# }
# context_obj = ApprovalSerializerForLicenceDoc(approval, context=serializer_context)
# context = context_obj.data
context = {}

try:
offender = sanction_outcome.get_offender()[0]
except:
raise Exception('No offender found for the Sanction Outcome: {}'.format(sanction_outcome.lodgement_number))
offender_dob = offender.dob.strftime('%d/%m/%Y') if offender.dob else ''
offender_postcode = offender.residential_address.postcode if offender.residential_address else ''
offender_email = offender.email if offender.email else ''
rego = sanction_outcome.registration_number if sanction_outcome.registration_number else ''
offence_datetime = sanction_outcome.offence.offence_occurrence_datetime if sanction_outcome.offence.offence_occurrence_datetime else ''
responsible_officer_name = sanction_outcome.responsible_officer.get_full_name() if sanction_outcome.responsible_officer else ''
issue_date = sanction_outcome.date_of_issue.strftime('%d/%m/%Y')
issue_time = sanction_outcome.time_of_issue.strftime('%I:%M %p')

context = {
'lodgement_number': sanction_outcome.lodgement_number,
'offender_family_name': offender.last_name,
'offender_given_name': offender.first_name,
'offender_date_of_birth': offender_dob,
'offender_postcode': offender_postcode,
'offender_residential_address': offender.residential_address,
'offender_email': offender_email,
'registration_number': rego,
'offence_location': sanction_outcome.offence.location,
'offence_date': offence_datetime.strftime('%d/%m/%Y'),
'offence_time': offence_datetime.strftime('%I:%M %p'),
'sanction_outcome_description': sanction_outcome.description,
'responsible_officer_name': responsible_officer_name,
'issue_date': issue_date,
'issue_time': issue_time,
}

doc.render(context)

temp_directory = settings.BASE_DIR + "/tmp/"
Expand Down

0 comments on commit d5ddb2f

Please sign in to comment.