Skip to content

Commit

Permalink
Merge pull request #12 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 23, 2021
2 parents bc00eae + ff98113 commit 29df9a2
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 23 deletions.
26 changes: 24 additions & 2 deletions wildlifecompliance/components/main/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin
#from ledger.accounts.models import EmailUser
from wildlifecompliance.components.main import models, forms
#from reversion.admin import VersionAdmin
from wildlifecompliance.components.main.models import SanctionOutcomeWordTemplate
from wildlifecompliance.components.main.utils import to_local_tz


@admin.register(models.GlobalSettings)
Expand All @@ -16,3 +16,25 @@ class SystemMaintenanceAdmin(admin.ModelAdmin):
ordering = ('start_date',)
readonly_fields = ('duration',)
form = forms.SystemMaintenanceAdminForm


@admin.register(SanctionOutcomeWordTemplate)
class SanctionOutcomeWordTemplateAdmin(admin.ModelAdmin):
list_display = ('Version', '_file', 'description', 'Date', 'Time')

def get_readonly_fields(self, request, obj=None):
if obj:
return ['_file', 'description', 'Date', 'Time']
else:
return []

def Version(self, obj):
return obj.id

def Date(self, obj):
local_date = to_local_tz(obj.uploaded_date)
return local_date.strftime('%d/%m/%Y')

def Time(self, obj):
local_date = to_local_tz(obj.uploaded_date)
return local_date.strftime('%H:%M')
21 changes: 21 additions & 0 deletions wildlifecompliance/components/main/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals
import logging
from datetime import datetime

from django.db import models
from django.db.models.query import QuerySet
from django.utils.encoding import python_2_unicode_compatible
Expand Down Expand Up @@ -235,3 +237,22 @@ def get_related_items_identifier(self):
def get_related_items_descriptor(self):
return self.get_full_name()


def update_sanction_outcome_word_filename(instance, filename):
cur_time = datetime.now().strftime('%Y%m%d_%H_%M')
new_filename = 'sanction_outcome_template_{}'.format(cur_time)
return 'sanction_outcome_template/{}.docx'.format(new_filename)


class SanctionOutcomeWordTemplate(models.Model):
_file = models.FileField(upload_to=update_sanction_outcome_word_filename, max_length=255)
uploaded_date = models.DateTimeField(auto_now_add=True, editable=False)
description = models.TextField(blank=True, verbose_name='description', help_text='')

class Meta:
app_label = 'wildlifecompliance'
verbose_name_plural = 'Wildlife Compliance Templates'
ordering = ['-id']

def __str__(self):
return "Version: {}, {}".format(self.id, self._file.name)
7 changes: 7 additions & 0 deletions wildlifecompliance/components/main/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ast

import pytz
import requests
import json
import logging
Expand Down Expand Up @@ -549,3 +551,8 @@ class FakeRequest():
def __init__(self, data):
self.data = data
self.user = None


def to_local_tz(_date):
local_tz = pytz.timezone(settings.TIME_ZONE)
return _date.astimezone(local_tz)
22 changes: 1 addition & 21 deletions wildlifecompliance/components/sanction_outcome/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,33 +563,13 @@ def send_infringement_notice(to_address, sanction_outcome, workflow_entry, reque


def create_infringement_notice_ybw(sanction_outcome, workflow_entry):
# pdf_file_name_y = 'infringement_notice_y_{}_{}.pdf'.format(sanction_outcome.lodgement_number,
# datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
pdf_file_name_b = 'infringement_notice_b_{}_{}.pdf'.format(sanction_outcome.lodgement_number,
datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
# pdf_file_name_w = 'infringement_notice_w_{}_{}.pdf'.format(sanction_outcome.lodgement_number,
# datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
# document = create_prosecution_notice_pdf_bytes(pdf_file_name, sanction_outcome)
# document = create_court_hearing_notice_pdf_bytes(pdf_file_name, sanction_outcome)
# document_y = create_infringement_notice_yellow(pdf_file_name_y, sanction_outcome)
pdf_file_name_b = 'infringement_notice_b_{}_{}.pdf'.format(sanction_outcome.lodgement_number, datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
document_b = create_infringement_notice_blue(pdf_file_name_b, sanction_outcome)
# document_w = create_infringement_notice_white(pdf_file_name_w, sanction_outcome)
# Attach files (files from the modal, and the PDF file generated above)
attachments = prepare_attachments(workflow_entry.documents)
mime = mimetypes.guess_type(document_b._file.path)[0]
# attachments.append((pdf_file_name_y, document_y._file.read(), mime))
attachments.append((pdf_file_name_b, document_b._file.read(), 'application/pdf'))
# attachments.append((pdf_file_name_w, document_w._file.read(), 'application/pdf'))
# Attach the pdf file created above to the communication log entry
# doc = workflow_entry.documents.create(name=document_y.name)
# doc._file = document_y._file
# doc.save()
doc = workflow_entry.documents.create(name=document_b.name)
doc._file = document_b._file
doc.save()
# doc = workflow_entry.documents.create(name=document_w.name)
# doc._file = document_w._file
# doc.save()
return attachments


Expand Down
3 changes: 3 additions & 0 deletions wildlifecompliance/doctopdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.conf import settings
from docxtpl import DocxTemplate
# from disturbance.components.main.models import ApiaryGlobalSettings
from wildlifecompliance.components.main.models import SanctionOutcomeWordTemplate


def create_infringement_notice_pdf_contents(pdf_filename):
Expand All @@ -22,6 +23,8 @@ def create_infringement_notice_pdf_contents(pdf_filename):
# path_to_template = os.path.join(settings.BASE_DIR, 'disturbance', 'static', 'disturbance', 'apiary_authority_template.docx')

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

doc = DocxTemplate(path_to_template)
# address = ''
Expand Down
29 changes: 29 additions & 0 deletions wildlifecompliance/migrations/0536_sanctionoutcomewordtemplate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2021-02-23 03:13
from __future__ import unicode_literals

from django.db import migrations, models
import wildlifecompliance.components.main.models


class Migration(migrations.Migration):

dependencies = [
('wildlifecompliance', '0535_auto_20210218_0748'),
]

operations = [
migrations.CreateModel(
name='SanctionOutcomeWordTemplate',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('_file', models.FileField(max_length=255, upload_to=wildlifecompliance.components.main.models.update_sanction_outcome_word_filename)),
('uploaded_date', models.DateTimeField(auto_now_add=True)),
('description', models.TextField(blank=True, verbose_name='description')),
],
options={
'verbose_name_plural': 'Wildlife Compliance Templates',
'ordering': ['-id'],
},
),
]

0 comments on commit 29df9a2

Please sign in to comment.