Skip to content

Commit

Permalink
Merge pull request #112 from gaiaresources/1845-renewal-sent-flag
Browse files Browse the repository at this point in the history
Added renewal_sent flag to licences
  • Loading branch information
serge-gaia committed Oct 11, 2016
2 parents ac23b87 + 17226c3 commit 5126e74
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 45 deletions.
18 changes: 1 addition & 17 deletions wildlifelicensing/apps/applications/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,8 @@

from django_cron import CronJobBase, Schedule

from wildlifelicensing.apps.main.models import WildlifeLicence
from wildlifelicensing.apps.applications.models import Assessment
from wildlifelicensing.apps.applications.emails import send_licence_renewal_email_notification, \
send_assessment_reminder_email


class CheckLicenceRenewalsCronJob(CronJobBase):
RUN_AT_TIMES = ['00:00']
LICENCE_RENEWAL_NOTIFICATION_DAYS = 30

schedule = Schedule(run_at_times=RUN_AT_TIMES)
code = 'applications.check_licence_renewals'

def do(self):
expiry_notification_date = date.today() + timedelta(days=self.LICENCE_RENEWAL_NOTIFICATION_DAYS)

for licence in WildlifeLicence.objects.filter(end_date=expiry_notification_date, is_renewable=True):
send_licence_renewal_email_notification(licence)
from wildlifelicensing.apps.applications.emails import send_assessment_reminder_email


class AssessmentRemindersCronJob(CronJobBase):
Expand Down
18 changes: 0 additions & 18 deletions wildlifelicensing/apps/applications/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,6 @@ def send_licence_issued_email(licence, application, request, to=None, cc=None, b
return log_entry


class LicenceRenewalNotificationEmail(TemplateEmailBase):
subject = 'Your wildlife licence is due for renewal.'
html_template = 'wl/emails/renew_licence_notification.html'
txt_template = 'wl/emails/renew_licence_notification.txt'


def send_licence_renewal_email_notification(licence):
email = LicenceRenewalNotificationEmail()
url = host_reverse('wl_home')

context = {
'url': url,
'licence': licence
}

email.send(licence.profile.email, context=context)


class UserNameChangeNotificationEmail(TemplateEmailBase):
subject = 'User has changed name and requires licence reissue.'
html_template = 'wl/emails/user_name_change_notification.html'
Expand Down
15 changes: 12 additions & 3 deletions wildlifelicensing/apps/applications/views/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ def post(self, request, *args, **kwargs):
licence.holder = application.applicant
licence.issuer = request.user

previous_licence = None
if application.previous_application is not None:
licence.licence_number = application.previous_application.licence.licence_number
previous_licence = application.previous_application.licence
licence.licence_number = previous_licence.licence_number

# if licence is renewal, use previous licence's sequence number
# if licence is renewal, start with previous licence's sequence number
if licence.licence_sequence == 0:
licence.licence_sequence = application.previous_application.licence.licence_sequence
licence.licence_sequence = previous_licence.licence_sequence

if not licence.licence_number:
licence.save(no_revision=True)
Expand All @@ -105,6 +107,9 @@ def post(self, request, *args, **kwargs):

licence.licence_sequence += 1

# reset renewal_sent flag in case of reissue
licence.renewal_sent = False

licence_filename = 'licence-%s-%d.pdf' % (licence.licence_number, licence.licence_sequence)

licence.licence_document = create_licence_pdf_document(licence_filename, licence, application,
Expand All @@ -118,6 +123,10 @@ def post(self, request, *args, **kwargs):

licence.save()

if previous_licence is not None:
previous_licence.replaced_by = licence
previous_licence.save()

licence.variants.clear()
for index, avl in enumerate(application.variants.through.objects.all().order_by('order')):
WildlifeLicenceVariantLink.objects.create(licence=licence, variant=avl.variant, order=index)
Expand Down
2 changes: 2 additions & 0 deletions wildlifelicensing/apps/dashboard/views/officer.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,6 @@ def get(self, request, *args, **kwargs):
licences = self.qs
response = HttpResponse(content_type='application/pdf')
response.write(bulk_licence_renewal_pdf_bytes(licences, request.build_absolute_uri(reverse('home'))))
if licences:
licences.update(renewal_sent=True)
return response
29 changes: 29 additions & 0 deletions wildlifelicensing/apps/main/cron.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from datetime import date, timedelta

from django_cron import CronJobBase, Schedule

from wildlifelicensing.apps.main.models import WildlifeLicence
from wildlifelicensing.apps.main.emails import send_licence_renewal_email_notification


class CheckLicenceRenewalsCronJob(CronJobBase):
RUN_AT_TIMES = ['00:00']
LICENCE_RENEWAL_NOTIFICATION_DAYS = 30

schedule = Schedule(run_at_times=RUN_AT_TIMES)
code = 'main.check_licence_renewals'

def do(self):
expiry_notification_date = date.today() + timedelta(days=self.LICENCE_RENEWAL_NOTIFICATION_DAYS)

renewal_conditions = {
'end_date__lte': expiry_notification_date,
'is_renewable': True,
'renewal_sent': False,
'replaced_by__isnull': True
}

for licence in WildlifeLicence.objects.filter(**renewal_conditions):
if send_licence_renewal_email_notification(licence):
licence.renewal_sent = True
licence.save()
19 changes: 19 additions & 0 deletions wildlifelicensing/apps/main/emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from wildlifelicensing.apps.emails.emails import TemplateEmailBase, host_reverse


class LicenceRenewalNotificationEmail(TemplateEmailBase):
subject = 'Your wildlife licence is due for renewal.'
html_template = 'wl/emails/renew_licence_notification.html'
txt_template = 'wl/emails/renew_licence_notification.txt'


def send_licence_renewal_email_notification(licence):
email = LicenceRenewalNotificationEmail()
url = host_reverse('wl_home')

context = {
'url': url,
'licence': licence
}

return email.send(licence.profile.email, context=context) is not None
29 changes: 29 additions & 0 deletions wildlifelicensing/apps/main/migrations/0020_auto_20161011_1127.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-10-11 03:27
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('wl_main', '0019_auto_20160905_1454'),
]

operations = [
migrations.RenameField(
model_name='wildlifelicence',
old_name='previous_licence',
new_name='replaced_by',
),
migrations.RemoveField(
model_name='wildlifelicence',
name='sequence_number',
),
migrations.AddField(
model_name='wildlifelicence',
name='renewal_sent',
field=models.BooleanField(default=False),
),
]
6 changes: 3 additions & 3 deletions wildlifelicensing/apps/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ class WildlifeLicence(Licence):
DEFAULT_FREQUENCY = MONTH_FREQUENCY_CHOICES[0][0]

profile = models.ForeignKey(Profile)
sequence_number = models.IntegerField(default=1)
purpose = models.TextField(blank=True)
locations = models.TextField(blank=True)
cover_letter_message = models.TextField(blank=True)
additional_information = models.TextField(blank=True)
licence_document = models.ForeignKey(Document, blank=True, null=True, related_name='licence_document')
cover_letter_document = models.ForeignKey(Document, blank=True, null=True, related_name='cover_letter_document')
return_frequency = models.IntegerField(choices=MONTH_FREQUENCY_CHOICES, default=DEFAULT_FREQUENCY)
previous_licence = models.ForeignKey('self', blank=True, null=True)
replaced_by = models.ForeignKey('self', blank=True, null=True)
regions = models.ManyToManyField(Region, blank=False)
variants = models.ManyToManyField('Variant', blank=True, through='WildlifeLicenceVariantLink')
renewal_sent = models.BooleanField(default=False)

def __str__(self):
return self.reference
Expand All @@ -115,7 +115,7 @@ def get_title_with_variants(self):

@property
def reference(self):
return '{}-{}'.format(self.licence_number, self.sequence_number)
return '{}-{}'.format(self.licence_number, self.licence_sequence)


class DefaultCondition(models.Model):
Expand Down
7 changes: 7 additions & 0 deletions wildlifelicensing/apps/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ def get(self, request, *args, **kwargs):
response.write(create_licence_renewal_pdf_bytes(filename, licence,
request.build_absolute_uri(reverse('home'))))

licence.renewal_sent = True
licence.save()

return response


Expand All @@ -240,6 +243,10 @@ def post(self, request, *args, **kwargs):
filename = 'bulk-renewals.pdf'
response = HttpResponse(content_type='application/pdf')
response.write(bulk_licence_renewal_pdf_bytes(licences, request.build_absolute_uri(reverse('home'))))

if licences:
licences.update(renewal_sent=True)

return response


Expand Down
6 changes: 3 additions & 3 deletions wildlifelicensing/apps/reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ class LicencesReportView(OfficerRequiredMixin, View):
'Regions',
'Purpose',
'Locations',
'Additional Info'
'Previous Licence',
'Additional Info',
'Replaced By',
'Lodgement Number',
)

Expand All @@ -167,7 +167,7 @@ def export(licence):
to_string(licence.purpose),
to_string(licence.locations),
to_string(licence.additional_information),
licence.previous_licence.reference if licence.previous_licence else '',
licence.replaced_by.reference if licence.replaced_by else '',
application.reference if application else '',
)

Expand Down
2 changes: 1 addition & 1 deletion wildlifelicensing/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
STATICFILES_DIRS.append(os.path.join(os.path.join(BASE_DIR, 'wildlifelicensing', 'static')))

CRON_CLASSES = [
'wildlifelicensing.apps.applications.cron.CheckLicenceRenewalsCronJob',
'wildlifelicensing.apps.applications.cron.AssessmentRemindersCronJob',
'wildlifelicensing.apps.main.cron.CheckLicenceRenewalsCronJob',
'wildlifelicensing.apps.returns.cron.CheckOverdueReturnsCronJob',
]

Expand Down

0 comments on commit 5126e74

Please sign in to comment.