From 52c47381f534c67ef0fedc5a7765a0de803818a3 Mon Sep 17 00:00:00 2001 From: Piotr Gawron <piotr.gawron@uni.lu> Date: Mon, 3 Apr 2017 13:49:06 +0200 Subject: [PATCH] refactor of api --- smash/web/api_urls.py | 18 +++---- smash/web/api_views/__init__.py | 6 +++ smash/web/api_views/appointment_type.py | 20 +++++++ smash/web/api_views/location.py | 15 ++++++ .../{api_views.py => api_views/subject.py} | 52 ++----------------- smash/web/api_views/worker.py | 20 +++++++ smash/web/tests/test_api.py | 2 +- 7 files changed, 76 insertions(+), 57 deletions(-) create mode 100644 smash/web/api_views/__init__.py create mode 100644 smash/web/api_views/appointment_type.py create mode 100644 smash/web/api_views/location.py rename smash/web/{api_views.py => api_views/subject.py} (79%) create mode 100644 smash/web/api_views/worker.py diff --git a/smash/web/api_urls.py b/smash/web/api_urls.py index 5f8a865f..1efb028f 100644 --- a/smash/web/api_urls.py +++ b/smash/web/api_urls.py @@ -15,15 +15,15 @@ Including another URLconf """ from django.conf.urls import url -from web import api_views +from web.api_views import worker, location, subject, appointment_type urlpatterns = [ - url(r'^cities$', api_views.cities, name='web.api.cities'), - url(r'^countries$', api_views.countries, name='web.api.countries'), - url(r'^specializations$', api_views.specializations, name='web.api.specializations'), - url(r'^units$', api_views.units, name='web.api.units'), - url(r'^locations$', api_views.locations, name='web.api.locations'), - url(r'^referrals$', api_views.referrals, name='web.api.referrals'), - url(r'^appointment_types$', api_views.appointment_types, name='web.api.appointment_types'), - url(r'^subjects/(?P<type>[A-z]+)$', api_views.subjects, name='web.api.subjects'), + url(r'^cities$', subject.cities, name='web.api.cities'), + url(r'^countries$', subject.countries, name='web.api.countries'), + url(r'^specializations$', worker.specializations, name='web.api.specializations'), + url(r'^units$', worker.units, name='web.api.units'), + url(r'^locations$', location.locations, name='web.api.locations'), + url(r'^referrals$', subject.referrals, name='web.api.referrals'), + url(r'^appointment_types$', appointment_type.appointment_types, name='web.api.appointment_types'), + url(r'^subjects/(?P<type>[A-z]+)$', subject.subjects, name='web.api.subjects'), ] diff --git a/smash/web/api_views/__init__.py b/smash/web/api_views/__init__.py new file mode 100644 index 00000000..e23cf420 --- /dev/null +++ b/smash/web/api_views/__init__.py @@ -0,0 +1,6 @@ +# coding=utf-8 + +import appointment_type +import location +import subject +import worker diff --git a/smash/web/api_views/appointment_type.py b/smash/web/api_views/appointment_type.py new file mode 100644 index 00000000..66f9af83 --- /dev/null +++ b/smash/web/api_views/appointment_type.py @@ -0,0 +1,20 @@ +from django.contrib.auth.decorators import login_required +from django.http import JsonResponse + +from web.models import AppointmentType + + +@login_required +def appointment_types(request): + appointments = AppointmentType.objects.filter().all() + result = [] + for appointment in appointments: + result.append({ + "id": appointment.id, + "type": appointment.code, + "default_duration": appointment.default_duration, + "can_be_parallelized": appointment.can_be_parallelized, + }) + return JsonResponse({ + "appointment_types": result + }) diff --git a/smash/web/api_views/location.py b/smash/web/api_views/location.py new file mode 100644 index 00000000..d5da64ab --- /dev/null +++ b/smash/web/api_views/location.py @@ -0,0 +1,15 @@ +from django.contrib.auth.decorators import login_required +from django.http import JsonResponse + +from web.models import Location + + +@login_required +def locations(request): + locations = Location.objects.all() + data = [] + for location in locations: + data.append({"id": location.id, "name": location.name}) + return JsonResponse({ + "locations": data + }) diff --git a/smash/web/api_views.py b/smash/web/api_views/subject.py similarity index 79% rename from smash/web/api_views.py rename to smash/web/api_views/subject.py index 1d56a1fd..9368d26c 100644 --- a/smash/web/api_views.py +++ b/smash/web/api_views/subject.py @@ -1,10 +1,10 @@ from django.contrib.auth.decorators import login_required from django.http import JsonResponse -from models import Subject, Worker, AppointmentType, Location -from views import e500_error -from views.subject import SUBJECT_LIST_GENERIC, SUBJECT_LIST_NO_VISIT, SUBJECT_LIST_REQUIRE_CONTACT -from views.notifications import get_subjects_with_no_visit, get_subjects_with_reminder +from web.models import Subject +from web.views import e500_error +from web.views.notifications import get_subjects_with_no_visit, get_subjects_with_reminder +from web.views.subject import SUBJECT_LIST_GENERIC, SUBJECT_LIST_NO_VISIT, SUBJECT_LIST_REQUIRE_CONTACT @login_required @@ -15,17 +15,6 @@ def cities(request): }) -@login_required -def locations(request): - locations = Location.objects.all() - data = [] - for location in locations: - data.append({"id": location.id, "name": location.name}) - return JsonResponse({ - "locations": data - }) - - @login_required def countries(request): X = Subject.objects.filter(country__isnull=False).values_list('country').distinct() @@ -42,22 +31,6 @@ def referrals(request): }) -@login_required -def specializations(request): - X = Worker.objects.filter(specialization__isnull=False).values_list('specialization').distinct() - return JsonResponse({ - "specializations": [x[0] for x in X] - }) - - -@login_required -def units(request): - X = Worker.objects.filter(unit__isnull=False).values_list('unit').distinct() - return JsonResponse({ - "units": [x[0] for x in X] - }) - - @login_required def get_subjects(request, type): if type == SUBJECT_LIST_GENERIC: @@ -170,6 +143,7 @@ def subjects(request, type): except: return e500_error(request) + def get_yes_no(val): if val: return "YES" @@ -194,19 +168,3 @@ def serialize_subject(subject): "id": subject.id, } return result - - -@login_required -def appointment_types(request): - appointments = AppointmentType.objects.filter().all() - result = [] - for appointment in appointments: - result.append({ - "id": appointment.id, - "type": appointment.code, - "default_duration": appointment.default_duration, - "can_be_parallelized": appointment.can_be_parallelized, - }) - return JsonResponse({ - "appointment_types": result - }) diff --git a/smash/web/api_views/worker.py b/smash/web/api_views/worker.py new file mode 100644 index 00000000..3c141e25 --- /dev/null +++ b/smash/web/api_views/worker.py @@ -0,0 +1,20 @@ +from django.contrib.auth.decorators import login_required +from django.http import JsonResponse + +from web.models import Worker + + +@login_required +def specializations(request): + X = Worker.objects.filter(specialization__isnull=False).values_list('specialization').distinct() + return JsonResponse({ + "specializations": [x[0] for x in X] + }) + + +@login_required +def units(request): + X = Worker.objects.filter(unit__isnull=False).values_list('unit').distinct() + return JsonResponse({ + "units": [x[0] for x in X] + }) diff --git a/smash/web/tests/test_api.py b/smash/web/tests/test_api.py index 762001b1..b90f1c35 100644 --- a/smash/web/tests/test_api.py +++ b/smash/web/tests/test_api.py @@ -9,7 +9,7 @@ from django.urls import reverse from web.tests.functions import create_subject, create_worker, create_appointment_type, create_location, \ create_get_suffix from web.views.subject import SUBJECT_LIST_GENERIC, SUBJECT_LIST_NO_VISIT, SUBJECT_LIST_REQUIRE_CONTACT -from web.api_views import get_subjects_order, get_subjects_filtered, serialize_subject +from web.api_views.subject import get_subjects_order, get_subjects_filtered, serialize_subject from web.models import Subject -- GitLab