Skip to content

Commit

Permalink
Merge pull request #168 from gaiaresources/3226-default-licence-period
Browse files Browse the repository at this point in the history
Default period for licence
  • Loading branch information
serge-gaia committed Jun 2, 2017
2 parents 50859aa + 6027aac commit ee614fb
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
34 changes: 33 additions & 1 deletion wildlifelicensing/apps/applications/tests/test_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

from django_dynamic_fixture import G

from dateutil.relativedelta import relativedelta

from wildlifelicensing.apps.applications.tests import helpers as app_helpers
from wildlifelicensing.apps.main.tests import helpers as main_helpers
from wildlifelicensing.apps.main.models import Region
from wildlifelicensing.apps.main.forms import DATE_FORMAT


class TestViewsAccess(TestCase):
class TestIssueLicence(TestCase):
"""
Only officers can access any of the views in the entry modules
"""
Expand Down Expand Up @@ -166,3 +169,32 @@ def test_issue_licence(self):
self.assertEquals(application.processing_status, 'issued')
self.assertIsNotNone(application.licence)
self.assertTrue(application.licence.is_issued)


def test_default_licence_period(self):
"""
Test that a licence default period correctly calculates the end date of a licence.
"""
default_period = 183

self.client.login(self.officer.email)

licence_type = main_helpers.get_or_create_licence_type('test')
licence_type.default_period = default_period
licence_type.save()

application = app_helpers.create_and_lodge_application(self.user, licence_type=licence_type)

response = self.client.get(reverse('wl_applications:issue_licence', args=[application.pk]))

todays_date_string = (datetime.date.today() + relativedelta(days=default_period)).strftime(DATE_FORMAT)

self.assertIn(todays_date_string, response.context['issue_licence_form']['end_date'].initial)

# test that end date not set when default_period is not set
licence_type.default_period = None
licence_type.save()

response = self.client.get(reverse('wl_applications:issue_licence', args=[application.pk]))

self.assertIsNone(response.context['issue_licence_form']['end_date'].initial)
5 changes: 3 additions & 2 deletions wildlifelicensing/apps/applications/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def _create_data_from_item(item, post_data, file_data, repetition, suffix):
def extract_licence_fields(schema, data):
licence_fields = []

for item in schema:
_extract_licence_fields_from_item(item, data, licence_fields)
if schema is not None:
for item in schema:
_extract_licence_fields_from_item(item, data, licence_fields)

return licence_fields

Expand Down
1 change: 1 addition & 0 deletions wildlifelicensing/apps/applications/views/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def get_context_data(self, **kwargs):

kwargs['issue_licence_form'] = IssueLicenceForm(purpose=purposes,
is_renewable=application.licence_type.is_renewable,
default_period=application.licence_type.default_period,
return_frequency=return_frequency)

kwargs['extracted_fields'] = extract_licence_fields(application.licence_type.application_schema,
Expand Down
7 changes: 5 additions & 2 deletions wildlifelicensing/apps/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ def __init__(self, *args, **kwargs):

is_renewable = kwargs.pop('is_renewable', False)

default_period = kwargs.pop('default_period', None)

return_frequency = kwargs.pop('return_frequency', WildlifeLicence.DEFAULT_FREQUENCY)

skip_required = kwargs.pop('skip_required', False)
Expand All @@ -115,9 +117,10 @@ def __init__(self, *args, **kwargs):

self.fields['issue_date'].localize = False

one_year_today = today_date + relativedelta(years=1, days=-1)
if default_period is not None:
end_date = today_date + relativedelta(days=default_period)

self.fields['end_date'].initial = one_year_today.strftime(DATE_FORMAT)
self.fields['end_date'].initial = end_date.strftime(DATE_FORMAT)

self.fields['is_renewable'].initial = is_renewable

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-06-01 04:06
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('wl_main', '0024_auto_20161104_1036'),
]

operations = [
migrations.AddField(
model_name='wildlifelicencetype',
name='default_period',
field=models.PositiveIntegerField(blank=True, null=True, verbose_name='Default Licence Period (days)'),
),
]
1 change: 1 addition & 0 deletions wildlifelicensing/apps/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class WildlifeLicenceType(LicenceType):
product_title = models.CharField(max_length=64, unique=True)
identification_required = models.BooleanField(default=False)
senior_applicable = models.BooleanField(default=False)
default_period = models.PositiveIntegerField('Default Licence Period (days)', blank=True, null=True)
default_conditions = models.ManyToManyField(Condition, through='DefaultCondition', blank=True)
application_schema = JSONField(blank=True, null=True)
category = models.ForeignKey(WildlifeLicenceCategory, null=True, blank=True)
Expand Down

0 comments on commit ee614fb

Please sign in to comment.