Merge branch 'master' into app.wiai.de
This commit is contained in:
commit
1b03a73b75
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,6 +1,8 @@
|
|||||||
docker.env
|
docker.env
|
||||||
static_files/
|
static_files/
|
||||||
migrations/
|
migrations/
|
||||||
|
coverage_html/
|
||||||
|
|
||||||
# Created by https://www.gitignore.io/api/pycharm,python,django
|
# Created by https://www.gitignore.io/api/pycharm,python,django
|
||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
|
|||||||
0
ofu_app/__init__.py
Normal file
0
ofu_app/__init__.py
Normal file
@ -1,6 +1,6 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from apps.food.models import Menu, SingleFood, HappyHour, Allergene, UserFoodImage
|
from apps.food.models import Menu, SingleFood, HappyHour, Allergene, UserFoodImage, FoodImage
|
||||||
|
|
||||||
|
|
||||||
class UserFoodImageSerializer(serializers.HyperlinkedModelSerializer):
|
class UserFoodImageSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
@ -9,6 +9,12 @@ class UserFoodImageSerializer(serializers.HyperlinkedModelSerializer):
|
|||||||
fields = ('id', 'image_image', 'image_thumb')
|
fields = ('id', 'image_image', 'image_thumb')
|
||||||
|
|
||||||
|
|
||||||
|
class FoodImageSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = FoodImage
|
||||||
|
fields = ('id', 'image', 'thumb')
|
||||||
|
|
||||||
|
|
||||||
class AllergensSerializer(serializers.HyperlinkedModelSerializer):
|
class AllergensSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Allergene
|
model = Allergene
|
||||||
@ -17,7 +23,7 @@ class AllergensSerializer(serializers.HyperlinkedModelSerializer):
|
|||||||
|
|
||||||
class SingleFoodSerializer(serializers.HyperlinkedModelSerializer):
|
class SingleFoodSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
allergens = AllergensSerializer(many=True, read_only=True)
|
allergens = AllergensSerializer(many=True, read_only=True)
|
||||||
image = UserFoodImageSerializer(many=True, read_only=True)
|
image = FoodImageSerializer(many=False, read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = SingleFood
|
model = SingleFood
|
||||||
|
|||||||
@ -19,15 +19,15 @@ from apps.food.models import Menu
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# API Version 1.1
|
# API Version 1.1
|
||||||
url(r'^food/$', api_views.FoodViewSetV1_1.as_view({'get': 'list'})),
|
url(r'^food/$', api_views.FoodViewSetV1_1.as_view({'get': 'list'}), name='api-v1_1-food-all'),
|
||||||
url(r'^food/(?P<location>' + Menu.FEKI + '|' + Menu.MARKUSPLATZ + '|' + Menu.ERBA + '|' + Menu.AUSTRASSE + ')/$',
|
url(r'^food/(?P<location>' + Menu.FEKI + '|' + Menu.MARKUSPLATZ + '|' + Menu.ERBA + '|' + Menu.AUSTRASSE + ')/$',
|
||||||
api_views.FoodViewSetV1_1.as_view({'get': 'list'})),
|
api_views.FoodViewSetV1_1.as_view({'get': 'list'}), name='api-v1_1-food-location'),
|
||||||
url(r'food/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$',
|
url(r'food/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$',
|
||||||
api_views.FoodViewSetV1_1.as_view({'get': 'list'})),
|
api_views.FoodViewSetV1_1.as_view({'get': 'list'}), name='api-v1_1-food-date'),
|
||||||
url(
|
url(
|
||||||
r'food/(?P<location>' + Menu.FEKI + '|' + Menu.MARKUSPLATZ + '|' + Menu.ERBA + '|' + Menu.AUSTRASSE + ')/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$',
|
r'food/(?P<location>' + Menu.FEKI + '|' + Menu.MARKUSPLATZ + '|' + Menu.ERBA + '|' + Menu.AUSTRASSE + ')/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$',
|
||||||
api_views.FoodViewSetV1_1.as_view({'get': 'list'})),
|
api_views.FoodViewSetV1_1.as_view({'get': 'list'}), name='api-v1_1-food-location-date'),
|
||||||
url(r'food/today/$', api_views.FoodViewSetV1_1.as_view({'get': 'list'})),
|
url(r'food/today/$', api_views.FoodViewSetV1_1.as_view({'get': 'list'}), name='api-v1_1-food-today'),
|
||||||
url(r'food/week/$', api_views.FoodViewSetV1_1.as_view({'get': 'list'})),
|
url(r'food/week/$', api_views.FoodViewSetV1_1.as_view({'get': 'list'}), name='api-v1_1-food-week'),
|
||||||
url(r'happy-hour', api_views.HappyHourViewSet.as_view({'get': 'list'})),
|
url(r'happy-hour', api_views.HappyHourViewSet.as_view({'get': 'list'}), name='api-v1_1-happy-hour-all'),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -112,6 +112,63 @@ class FoodViewSetV1_1(viewsets.ModelViewSet, ):
|
|||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
|
# @api_view(['GET'])
|
||||||
|
@permission_classes((AllowAny,))
|
||||||
|
class FoodViewSetV1_1(viewsets.ModelViewSet, ):
|
||||||
|
"""
|
||||||
|
API endpoint that allows users to be viewed or edited.
|
||||||
|
"""
|
||||||
|
# queryset = Menu.objects.all()
|
||||||
|
serializer_class = MenuSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
queryset = Menu.objects.all()
|
||||||
|
location = None
|
||||||
|
if 'location' in self.kwargs:
|
||||||
|
location = self.kwargs['location']
|
||||||
|
|
||||||
|
year = None
|
||||||
|
if 'year' in self.kwargs:
|
||||||
|
year = self.kwargs['year']
|
||||||
|
month = None
|
||||||
|
if 'month' in self.kwargs:
|
||||||
|
month = self.kwargs['month']
|
||||||
|
day = None
|
||||||
|
if 'day' in self.kwargs:
|
||||||
|
day = self.kwargs['day']
|
||||||
|
|
||||||
|
if location:
|
||||||
|
if str(location).upper() == Menu.ERBA.upper():
|
||||||
|
queryset = queryset.filter(location__contains=Menu.ERBA)
|
||||||
|
elif str(location).upper() == Menu.FEKI.upper():
|
||||||
|
queryset = queryset.filter(location__contains=Menu.FEKI)
|
||||||
|
elif str(location).upper() == Menu.AUSTRASSE.upper():
|
||||||
|
print("Before: " + str(queryset))
|
||||||
|
queryset = queryset.filter(location__contains=Menu.AUSTRASSE)
|
||||||
|
elif str(location).upper() == Menu.MARKUSPLATZ.upper():
|
||||||
|
queryset = queryset.filter(location__contains=Menu.MARKUSPLATZ)
|
||||||
|
print(queryset)
|
||||||
|
if year and month and day:
|
||||||
|
date = '%s-%s-%s' % (year, month, day)
|
||||||
|
queryset = queryset.filter(date=datetime.strptime(date, '%Y-%m-%d'))
|
||||||
|
|
||||||
|
# if date == "week":
|
||||||
|
# today = datetime.now()
|
||||||
|
# weekday = today.weekday()
|
||||||
|
# monday = today - timedelta(weekday)
|
||||||
|
# sunday = today + (timedelta(6 - weekday))
|
||||||
|
# print("Monday: " + str(monday))
|
||||||
|
# print("Sunday: " + str(sunday))
|
||||||
|
# queryset = queryset.filter(date__gte=monday, date__lte=sunday)
|
||||||
|
# else:
|
||||||
|
# queryset = queryset.filter(date=datetime.strptime(date, "%Y-%m-%d"))
|
||||||
|
|
||||||
|
print("LOCATION: %s" % str(location))
|
||||||
|
print(str(queryset))
|
||||||
|
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
|
||||||
# @api_view(['GET'])
|
# @api_view(['GET'])
|
||||||
@permission_classes((AllowAny,))
|
@permission_classes((AllowAny,))
|
||||||
class HappyHourViewSet(viewsets.ModelViewSet):
|
class HappyHourViewSet(viewsets.ModelViewSet):
|
||||||
|
|||||||
0
ofu_app/apps/food/tests/__init__.py
Normal file
0
ofu_app/apps/food/tests/__init__.py
Normal file
134
ofu_app/apps/food/tests/tests_api.py
Normal file
134
ofu_app/apps/food/tests/tests_api.py
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.urls import reverse
|
||||||
|
from apps.food.models import SingleFood, Menu
|
||||||
|
from apps.food.api.serializers import MenuSerializer
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.test import APIRequestFactory
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
# Create your tests here.
|
||||||
|
|
||||||
|
class SingleFood_Scope(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.singlefood_1 = SingleFood.objects.create(name="testfood1")
|
||||||
|
self.singlefood_2 = SingleFood.objects.create(name="testfood2")
|
||||||
|
self.singlefood_3 = SingleFood.objects.create(name="testfood3")
|
||||||
|
self.singlefood_4 = SingleFood.objects.create(name="testfood4")
|
||||||
|
self.singlefood_5 = SingleFood.objects.create(name="testfood5")
|
||||||
|
self.singlefood_6 = SingleFood.objects.create(name="testfood6")
|
||||||
|
self.today_menu = Menu.objects.create(date='2017-01-15', location=Menu.ERBA)
|
||||||
|
self.today_menu.menu.add(self.singlefood_1)
|
||||||
|
self.today_menu.menu.add(self.singlefood_2)
|
||||||
|
self.today_menu.menu.add(self.singlefood_3)
|
||||||
|
self.menu_2 = Menu.objects.create(date='2017-01-10', location=Menu.FEKI)
|
||||||
|
self.menu_2.menu.add(self.singlefood_4)
|
||||||
|
self.menu_2.menu.add(self.singlefood_5)
|
||||||
|
self.menu_2.menu.add(self.singlefood_6)
|
||||||
|
|
||||||
|
def test_get_food_root(self):
|
||||||
|
"""
|
||||||
|
All menus in response
|
||||||
|
"""
|
||||||
|
menus = Menu.objects.all()
|
||||||
|
serializer = MenuSerializer(menus, many=True)
|
||||||
|
response = self.client.get(reverse('api-v1_1-food-all'))
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
self.assertEqual(response.data, serializer.data)
|
||||||
|
|
||||||
|
def test_get_food_date_param(self):
|
||||||
|
"""
|
||||||
|
Menu for a given date is in the response
|
||||||
|
"""
|
||||||
|
serializer = MenuSerializer(self.menu_2, many=False)
|
||||||
|
response_1 = self.client.get(reverse('api-v1_1-food-date', kwargs={'year': '2017', 'month': '01', 'day': '10'}))
|
||||||
|
self.assertEqual(response_1.status_code, 200)
|
||||||
|
self.assertEqual(response_1.data, [serializer.data])
|
||||||
|
|
||||||
|
serializer_2 = MenuSerializer(self.today_menu, many=False)
|
||||||
|
response_2 = self.client.get(reverse('api-v1_1-food-date', kwargs={'year': '2017', 'month': '01', 'day': '15'}))
|
||||||
|
self.assertEqual(response_2.status_code, 200)
|
||||||
|
self.assertEqual(response_2.data, [serializer_2.data])
|
||||||
|
|
||||||
|
def test_get_food_date_param_not_in_set(self):
|
||||||
|
"""
|
||||||
|
Empty response if no object with request date in set
|
||||||
|
"""
|
||||||
|
response_1 = self.client.get(reverse('api-v1_1-food-date', kwargs={'year': '2017', 'month': '01', 'day': '11'}))
|
||||||
|
self.assertEqual(response_1.status_code, 200)
|
||||||
|
self.assertEqual(response_1.data, [])
|
||||||
|
|
||||||
|
def test_get_food_location_param(self):
|
||||||
|
"""
|
||||||
|
Menu for a given location is in the response
|
||||||
|
"""
|
||||||
|
serializer_1 = MenuSerializer(self.today_menu, many=False)
|
||||||
|
response_1 = self.client.get(reverse('api-v1_1-food-location', kwargs={'location': Menu.ERBA}))
|
||||||
|
self.assertEqual(response_1.status_code, 200)
|
||||||
|
self.assertEqual(response_1.data, [serializer_1.data])
|
||||||
|
|
||||||
|
serializer_2 = MenuSerializer(self.menu_2, many=False)
|
||||||
|
response_2 = self.client.get(reverse('api-v1_1-food-location', kwargs={'location': Menu.FEKI}))
|
||||||
|
self.assertEqual(response_2.status_code, 200)
|
||||||
|
self.assertEqual(response_2.data, [serializer_2.data])
|
||||||
|
|
||||||
|
def test_get_food_location_param_not_in_set(self):
|
||||||
|
"""
|
||||||
|
Empty response if no object with request location in set
|
||||||
|
"""
|
||||||
|
response_1 = self.client.get(reverse('api-v1_1-food-location', kwargs={'location': Menu.AUSTRASSE}))
|
||||||
|
self.assertEqual(response_1.status_code, 200)
|
||||||
|
self.assertEqual(response_1.data, [])
|
||||||
|
|
||||||
|
def test_get_food_location_date_param(self):
|
||||||
|
"""
|
||||||
|
Menu for request location and date is in the response
|
||||||
|
"""
|
||||||
|
|
||||||
|
serializer = MenuSerializer(self.menu_2, many=False)
|
||||||
|
response = self.client.get(reverse('api-v1_1-food-location-date',
|
||||||
|
kwargs={'year': '2017', 'month': '01', 'day': '10',
|
||||||
|
'location': Menu.FEKI}))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.data, [serializer.data])
|
||||||
|
|
||||||
|
def test_get_food_location_date_param_not_in_set(self):
|
||||||
|
"""
|
||||||
|
Empty response if no object with request date and location in set
|
||||||
|
"""
|
||||||
|
response_1 = self.client.get(reverse('api-v1_1-food-location-date',
|
||||||
|
kwargs={'year': '2017', 'month': '01', 'day': '11',
|
||||||
|
'location': Menu.FEKI}))
|
||||||
|
self.assertEqual(response_1.status_code, 200)
|
||||||
|
self.assertEqual(response_1.data, [])
|
||||||
|
|
||||||
|
response_1 = self.client.get(reverse('api-v1_1-food-location-date',
|
||||||
|
kwargs={'year': '2017', 'month': '01', 'day': '10',
|
||||||
|
'location': Menu.AUSTRASSE}))
|
||||||
|
self.assertEqual(response_1.status_code, 200)
|
||||||
|
self.assertEqual(response_1.data, [])
|
||||||
|
|
||||||
|
def test_get_food_today_param(self):
|
||||||
|
"""
|
||||||
|
Menu for request today is in the response
|
||||||
|
"""
|
||||||
|
self.singlefood = SingleFood.objects.create(name="testfood")
|
||||||
|
self.today_menu = Menu.objects.create(date=datetime.today().strftime('%Y-%m-%d'), location=Menu.ERBA)
|
||||||
|
self.today_menu.menu.add(self.singlefood)
|
||||||
|
|
||||||
|
serializer = MenuSerializer(self.today_menu, many=False)
|
||||||
|
response = self.client.get(reverse('api-v1_1-food-today'))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.data, [serializer.data])
|
||||||
|
|
||||||
|
def test_get_food_today_param_not_in_set(self):
|
||||||
|
"""
|
||||||
|
Empty response if no object with date 'today' is in set
|
||||||
|
"""
|
||||||
|
response = self.client.get(reverse('api-v1_1-food-today'))
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.data, [])
|
||||||
@ -8,7 +8,7 @@ from apps.food.models import SingleFood
|
|||||||
|
|
||||||
# Create your tests here.
|
# Create your tests here.
|
||||||
|
|
||||||
class SingleFood_Created_Just_with_name(TestCase):
|
class SingleFood_Scope(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
SingleFood.objects.create(name="testfood")
|
SingleFood.objects.create(name="testfood")
|
||||||
|
|
||||||
@ -6,4 +6,5 @@ requests==2.18.4
|
|||||||
beautifulsoup4==4.6.0
|
beautifulsoup4==4.6.0
|
||||||
#psycopg2==2.7.3.2
|
#psycopg2==2.7.3.2
|
||||||
xmltodict==0.11.0
|
xmltodict==0.11.0
|
||||||
|
coverage==3.6
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user