From 08cc68ec9f724c60d687b74db17d8c374c8d4e66 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 4 Dec 2017 17:09:40 +0100
Subject: [PATCH] api for obtaining flying teams

---
 smash/web/api_urls.py                         |  5 ++-
 smash/web/api_views/flying_team.py            | 15 ++++++++
 smash/web/tests/api_views/test_flying_team.py | 38 +++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 smash/web/api_views/flying_team.py
 create mode 100644 smash/web/tests/api_views/test_flying_team.py

diff --git a/smash/web/api_urls.py b/smash/web/api_urls.py
index b6eb1740..098a31aa 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
+    redcap, flying_team
 
 urlpatterns = [
     # appointments
@@ -41,6 +41,9 @@ urlpatterns = [
     # locations
     url(r'^locations$', location.locations, name='web.api.locations'),
 
+    # flying_teams
+    url(r'^flying_teams$', flying_team.flying_teams, name='web.api.flying_teams'),
+
     # worker data
     url(r'^specializations$', worker.specializations, name='web.api.specializations'),
     url(r'^units$', worker.units, name='web.api.units'),
diff --git a/smash/web/api_views/flying_team.py b/smash/web/api_views/flying_team.py
new file mode 100644
index 00000000..26b4a166
--- /dev/null
+++ b/smash/web/api_views/flying_team.py
@@ -0,0 +1,15 @@
+from django.contrib.auth.decorators import login_required
+from django.http import JsonResponse
+
+from web.models import FlyingTeam
+
+
+@login_required
+def flying_teams(request):
+    all_flying_teams = FlyingTeam.objects.all()
+    data = []
+    for flying_team in all_flying_teams:
+        data.append({"id": flying_team.id, "name": flying_team.place})
+    return JsonResponse({
+        "flying_teams": data
+    })
diff --git a/smash/web/tests/api_views/test_flying_team.py b/smash/web/tests/api_views/test_flying_team.py
new file mode 100644
index 00000000..ca1895f2
--- /dev/null
+++ b/smash/web/tests/api_views/test_flying_team.py
@@ -0,0 +1,38 @@
+# coding=utf-8
+import json
+
+from django.contrib.auth.models import User
+from django.test import Client
+from django.test import TestCase
+from django.urls import reverse
+
+from web.tests.functions import create_worker, create_flying_team
+
+
+class TestFlyingTeamApi(TestCase):
+    def setUp(self):
+        self.client = Client()
+        username = 'piotr'
+        password = 'top_secret'
+        self.user = User.objects.create_user(
+            username=username, email='jacob@bla', password=password)
+        self.worker = create_worker(self.user)
+        self.client.login(username=username, password=password)
+
+    def test_flying_teams(self):
+        flying_team_name = "some flying_team"
+
+        response = self.client.get(reverse('web.api.flying_teams'))
+        self.assertEqual(response.status_code, 200)
+
+        create_flying_team(flying_team_name)
+
+        response = self.client.get(reverse('web.api.flying_teams'))
+        flying_teams = json.loads(response.content)['flying_teams']
+
+        found = False
+        for flying_team in flying_teams:
+            if flying_team['name'] == flying_team_name:
+                found = True
+
+        self.assertTrue(found)
-- 
GitLab