Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor bug fix of returns api URL. #219

Merged
merged 11 commits into from
Jul 18, 2017
Merged
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ pycountry==17.1.8
six>=1.10.0
django-ical==1.4
django-taggit==0.21.3
geojson==1.3.3
geojson==1.3.3
unicodecsv==0.14.1
2 changes: 1 addition & 1 deletion wildlifelicensing/apps/returns/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from wildlifelicensing.apps.returns.api import views

urlpatterns = [
url(r'data/(?P<return_type_pk>[0-9])/(?P<resource_number>[0-9]?)/?', views.ReturnsDataView.as_view(),
url(r'data/(?P<return_type_pk>[0-9]+)/(?P<resource_number>[0-9]+)/?', views.ReturnsDataView.as_view(),
name='data'),
url(r'', views.ExplorerView.as_view(), name='explorer')
]
8 changes: 5 additions & 3 deletions wildlifelicensing/apps/returns/api/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import csv
import unicodecsv as csv

from collections import OrderedDict

from django.core.urlresolvers import reverse
from django.http import HttpResponse, Http404, JsonResponse
from django.shortcuts import get_object_or_404
from django.views.generic import View
from django.utils import timezone
from django.utils.encoding import smart_text

from wildlifelicensing.apps.returns.api.mixins import APIUserRequiredMixin
from wildlifelicensing.apps.returns.models import ReturnType, ReturnRow
Expand All @@ -25,7 +27,7 @@ class ExplorerView(APIUserRequiredMixin, View):
(@see ReturnsDataView view)
"""

def get(self, request):
def get(self, request, *args, **kwargs):
queryset = ReturnType.objects.all()
# for API purpose, increase the session timeout
set_api_session_timeout(request)
Expand Down Expand Up @@ -101,6 +103,6 @@ def get(self, request, *args, **kwargs):
for ret_row in qs:
row = []
for field in schema.field_names:
row.append(unicode(ret_row.data.get(field, '')))
row.append(smart_text(ret_row.data.get(field, ''), errors='replace'))
writer.writerow(row)
return response
70 changes: 70 additions & 0 deletions wildlifelicensing/apps/returns/tests/test_returns.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
get_or_create_licence_type, clear_mailbox
from wildlifelicensing.apps.returns.models import Return
from wildlifelicensing.apps.returns.tests.helpers import create_return, get_or_create_return_type
from wildlifelicensing.apps.returns.utils import create_returns_due_dates

TEST_SPREADSHEET_PATH = os.path.join('wildlifelicensing', 'apps', 'returns', 'test_data', 'regulation17.xlsx')

Expand Down Expand Up @@ -614,3 +615,72 @@ def test_declined_amended(self):
expected_status = 'declined'
self.assertEqual(ret.status, expected_status)


class TestUtils(TestCase):

def test_create_returns_due_dates(self):
# one year with 1 month period
start_date = date(2017, 6, 30)
end_date = date(2018, 6, 30)
monthly_frequency = 1
due_dates = create_returns_due_dates(start_date, end_date, monthly_frequency)
expected_due_dates = [
date(2017, 7, 30),
date(2017, 8, 30),
date(2017, 9, 30),
date(2017, 10, 30),
date(2017, 11, 30),
date(2017, 12, 30),
date(2018, 1, 30),
date(2018, 2, 28),
date(2018, 3, 30),
date(2018, 4, 30),
date(2018, 5, 30),
date(2018, 6, 30)
]
self.assertEqual(due_dates, expected_due_dates)

# one year with 4 months period
start_date = date(2017, 6, 30)
end_date = date(2018, 6, 30)
monthly_frequency = 4
due_dates = create_returns_due_dates(start_date, end_date, monthly_frequency)
expected_due_dates = [
date(2017, 10, 30),
date(2018, 2, 28),
date(2018, 6, 30)
]
self.assertEqual(due_dates, expected_due_dates)

# two years with 6 months period
start_date = date(2017, 6, 30)
end_date = date(2019, 6, 30)
monthly_frequency = 6
due_dates = create_returns_due_dates(start_date, end_date, monthly_frequency)
expected_due_dates = [
date(2017, 12, 30),
date(2018, 6, 30),
date(2018, 12, 30),
date(2019, 6, 30)
]
self.assertEqual(due_dates, expected_due_dates)

# case where monthly period exceed the end date, should return the end_date
start_date = date(2017, 6, 30)
end_date = date(2017, 7, 15)
monthly_frequency = 1
due_dates = create_returns_due_dates(start_date, end_date, monthly_frequency)
expected_due_dates = [
end_date,
]
self.assertEqual(due_dates, expected_due_dates)

# negative monthly frequency = one off
start_date = date(2017, 6, 30)
end_date = date(2019, 6, 30)
monthly_frequency = -1
due_dates = create_returns_due_dates(start_date, end_date, monthly_frequency)
expected_due_dates = [
end_date
]
self.assertEqual(due_dates, expected_due_dates)
8 changes: 6 additions & 2 deletions wildlifelicensing/apps/returns/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ def create_returns_due_dates(start_date, end_date, monthly_frequency):
if monthly_frequency < 0:
due_dates.append(end_date)
else:
delta = relativedelta(months=monthly_frequency)
count = 1
delta = relativedelta(months=count*monthly_frequency)
due_date = start_date + delta
if due_date > end_date:
# case where the first return due date is > end_date
# treat it like a one off
due_dates.append(end_date)
else:
count += 1
while due_date <= end_date:
due_dates.append(due_date)
due_date += delta
delta = relativedelta(months=count*monthly_frequency)
due_date = start_date + delta
count += 1
return due_dates


Expand Down