From cc69878893ef39e50d71a61a8de3382b6fa8e972 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Tue, 5 Jun 2018 13:41:42 +0200
Subject: [PATCH] api for voucher_types and for listing available voucher
 columns

---
 smash/web/api_urls.py                         |  9 +++++-
 smash/web/api_views/voucher.py                | 24 +++++++++++---
 smash/web/api_views/voucher_type.py           | 32 +++++++++++++++++++
 .../web/tests/api_views/test_voucher_type.py  | 24 ++++++++++++++
 4 files changed, 84 insertions(+), 5 deletions(-)
 create mode 100644 smash/web/api_views/voucher_type.py
 create mode 100644 smash/web/tests/api_views/test_voucher_type.py

diff --git a/smash/web/api_urls.py b/smash/web/api_urls.py
index 7e13acda..bea229e7 100644
--- a/smash/web/api_urls.py
+++ b/smash/web/api_urls.py
@@ -16,7 +16,7 @@ Including another URLconf
 from django.conf.urls import url
 
 from web.api_views import worker, location, subject, appointment_type, appointment, configuration, daily_planning, \
-    redcap, flying_team, visit, voucher
+    redcap, flying_team, visit, voucher, voucher_type
 
 urlpatterns = [
     # appointments
@@ -69,5 +69,12 @@ urlpatterns = [
 
     # vouchers data
     url(r'^vouchers/$', voucher.get_vouchers, name='web.api.vouchers'),
+    url(r'^vouchers:columns$', voucher.get_voucher_columns,
+        name='web.api.vouchers.columns'),
+
+    # voucher types data
+    url(r'^voucher_types/$', voucher_type.get_voucher_types, name='web.api.voucher_types'),
+    url(r'^vouchers:columns$', voucher.get_voucher_columns,
+        name='web.api.vouchers.columns'),
 
 ]
diff --git a/smash/web/api_views/voucher.py b/smash/web/api_views/voucher.py
index 536e64b9..424bf086 100644
--- a/smash/web/api_views/voucher.py
+++ b/smash/web/api_views/voucher.py
@@ -2,9 +2,9 @@ import logging
 
 from django.http import JsonResponse
 
-from web.api_views.serialization_utils import serialize_datetime, get_filters_for_data_table_request
+from web.api_views.serialization_utils import get_filters_for_data_table_request, add_column, \
+    serialize_date
 from web.models import Voucher
-from web.views import e500_error
 
 logger = logging.getLogger(__name__)
 
@@ -67,6 +67,22 @@ def get_vouchers_filtered(vouchers_to_be_filtered, filters):
     return result
 
 
+# noinspection PyUnusedLocal
+def get_voucher_columns(request):
+    result = []
+    add_column(result, "First name", "first_name", None, "string_filter")
+    add_column(result, "Last name", "last_name", None, "string_filter")
+    add_column(result, "Number", "number", None, "string_filter")
+    add_column(result, "Type", "type", None, "voucher_type_filter")
+    add_column(result, "Status", "status", None, "voucher_status_filter")
+    add_column(result, "Voucher partner", "voucher_partner", None, "voucher_partner_filter")
+    add_column(result, "Issue date", "issue_date", None, None)
+    add_column(result, "Expiry date", "expiry_date", None, None)
+    add_column(result, "Edit", "edit", None, None, sortable=False)
+
+    return JsonResponse({"columns": result})
+
+
 def get_vouchers(request):
     # id of the query from dataTable: https://datatables.net/manual/server-side
     draw = int(request.GET.get("draw", "-1"))
@@ -104,8 +120,8 @@ def get_vouchers(request):
 
 
 def serialize_voucher(voucher):
-    issue_date = serialize_datetime(voucher.issue_date)
-    expiry_date = serialize_datetime(voucher.expiry_date)
+    issue_date = serialize_date(voucher.issue_date)
+    expiry_date = serialize_date(voucher.expiry_date)
     result = {
         "first_name": voucher.study_subject.subject.first_name,
         "last_name": voucher.study_subject.subject.last_name,
diff --git a/smash/web/api_views/voucher_type.py b/smash/web/api_views/voucher_type.py
new file mode 100644
index 00000000..8cf1c18f
--- /dev/null
+++ b/smash/web/api_views/voucher_type.py
@@ -0,0 +1,32 @@
+import logging
+
+from django.http import JsonResponse
+
+from web.models import VoucherType
+
+logger = logging.getLogger(__name__)
+
+
+def get_voucher_types(request):
+    all_vouchers = VoucherType.objects.all()
+
+    count = all_vouchers.count()
+
+    data = []
+    for voucher_type in all_vouchers:
+        data.append(serialize_voucher_type(voucher_type))
+
+    return JsonResponse({
+        "recordsTotal": count,
+        "recordsFiltered": count,
+        "data": data,
+    })
+
+
+def serialize_voucher_type(voucher_type):
+    result = {
+        "code": voucher_type.code,
+        "description": voucher_type.description,
+        "id": voucher_type.id,
+    }
+    return result
diff --git a/smash/web/tests/api_views/test_voucher_type.py b/smash/web/tests/api_views/test_voucher_type.py
new file mode 100644
index 00000000..c4f79998
--- /dev/null
+++ b/smash/web/tests/api_views/test_voucher_type.py
@@ -0,0 +1,24 @@
+# coding=utf-8
+import datetime
+import logging
+
+from django.urls import reverse
+
+from web.api_views.voucher import get_vouchers_filtered, get_vouchers_order
+from web.models import Voucher
+from web.models.constants import VOUCHER_STATUS_USED, VOUCHER_STATUS_NEW
+from web.tests import LoggedInWithWorkerTestCase
+from web.tests.functions import create_get_suffix, create_voucher, create_voucher_type
+from web.views.notifications import get_today_midnight_date
+
+logger = logging.getLogger(__name__)
+
+
+class TestVoucherTypeApi(LoggedInWithWorkerTestCase):
+    def setUp(self):
+        super(TestVoucherTypeApi, self).setUp()
+        self.voucher_type = create_voucher_type()
+
+    def test_voucher_types_render(self):
+        response = self.client.get(reverse('web.api.voucher_types'))
+        self.assertEqual(response.status_code, 200)
-- 
GitLab