Skip to content
Snippets Groups Projects
Commit cc698788 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

api for voucher_types and for listing available voucher columns

parent a6cd4f1a
No related branches found
No related tags found
1 merge request!138list of vouchers with possibility to filter and sort
......@@ -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'),
]
......@@ -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,
......
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
# 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment