From 09d1da2cecd354fcd1c44ff23c2a0497ca68693d Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 12 Jan 2019 22:09:59 +0200 Subject: [PATCH] Add API support for diets and ingredients views --- .../hyrieus/flavoursAPI/hyrieus/models.py | 11 - .../hyrieus/flavoursAPI/hyrieus/urls.py | 10 +- .../hyrieus/flavoursAPI/hyrieus/views.py | 250 +++++++++++++++++- 3 files changed, 255 insertions(+), 16 deletions(-) diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/models.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/models.py index 29ed905..8b20e65 100644 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/models.py +++ b/UI/Database API/hyrieus/flavoursAPI/hyrieus/models.py @@ -29,7 +29,6 @@ class Diet(models.Model): managed = True db_table = 'diet' - class DietProhibitsIngredient(models.Model): diet = models.ForeignKey(Diet, models.DO_NOTHING) ingredient_name = models.ForeignKey('Ingredient', models.DO_NOTHING, db_column='ingredient_name') @@ -50,7 +49,6 @@ class Drink(models.Model): managed = True db_table = 'drink' - class DrinkHasIngredient(models.Model): drink = models.ForeignKey(Drink, models.DO_NOTHING) ingredient_name = models.ForeignKey('Ingredient', models.DO_NOTHING, db_column='ingredient_name') @@ -60,7 +58,6 @@ class DrinkHasIngredient(models.Model): db_table = 'drink_has_ingredient' unique_together = (('drink', 'ingredient_name'),) - class Food(models.Model): food_id = models.AutoField(primary_key=True) food_name = models.CharField(max_length=500) @@ -73,7 +70,6 @@ class Food(models.Model): managed = True db_table = 'food' - class FoodHasIngredient(models.Model): food = models.ForeignKey(Food, models.DO_NOTHING) ingredient_name = models.ForeignKey('Ingredient', models.DO_NOTHING, db_column='ingredient_name') @@ -91,7 +87,6 @@ class Ingredient(models.Model): managed = True db_table = 'ingredient' - class Permission(models.Model): permission_id = models.AutoField(primary_key=True) permission_description = models.CharField(max_length=700) @@ -100,7 +95,6 @@ class Permission(models.Model): managed = True db_table = 'permission' - class Restaurant(models.Model): restaurant_id = models.AutoField(primary_key=True) restaurant_name = models.CharField(max_length=500) @@ -125,7 +119,6 @@ class RoleHasPermission(models.Model): db_table = 'role_has_permission' unique_together = (('role', 'permission'),) - class UserFollowsDiet(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING) diet = models.ForeignKey(Diet, models.DO_NOTHING) @@ -135,7 +128,6 @@ class UserFollowsDiet(models.Model): db_table = 'user_follows_diet' unique_together = (('user', 'diet'),) - class UserProhibitsIngredient(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING) ingredient_name = models.ForeignKey(Ingredient, models.DO_NOTHING, db_column='ingredient_name') @@ -145,7 +137,6 @@ class UserProhibitsIngredient(models.Model): db_table = 'user_prohibits_ingredient' unique_together = (('user', 'ingredient_name'),) - class UserRatesDrink(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING) drink = models.ForeignKey(Drink, models.DO_NOTHING) @@ -159,7 +150,6 @@ class UserRatesDrink(models.Model): db_table = 'user_rates_drink' unique_together = (('user', 'drink'),) - class UserRatesFood(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING) food = models.ForeignKey(Food, models.DO_NOTHING) @@ -173,7 +163,6 @@ class UserRatesFood(models.Model): db_table = 'user_rates_food' unique_together = (('user', 'food'),) - class UserRatesRestaurant(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING, blank=True, null=True) restaurant = models.ForeignKey(Restaurant, models.DO_NOTHING) diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py index 884a7a1..67225a7 100644 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py +++ b/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py @@ -7,7 +7,8 @@ from flavoursAPI.hyrieus.views import (DietViewSet, DietProhibitsIngredientViewS RoleHasPermissionViewSet, UserViewSet, UserFollowsDietViewSet, UserProhibitsIngredientViewSet, UserRatesDrinkViewSet, UserRatesFoodViewSet, UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets, - FoodUserView, DrinkUserView, ProfileUserView, + FoodUserView, DrinkUserView, ProfileUserView, AddFood, AddDrink, AddDiet, + FollowDiet, ProhibitIngredient, DietUserView, IngredientUserView, ) router = routers.DefaultRouter() @@ -37,6 +38,13 @@ urlpatterns = [ path('drinkUserView//', DrinkUserView.as_view(), name='drinkUserView'), path('profileUserView//', ProfileUserView.as_view(), name='profileUserView'), path('userDiets/', UserDiets.as_view(), name='userDiets'), + path('addFood/', AddFood.as_view(), name='addFood'), + path('addDrink/', AddDrink.as_view(), name='addDrink'), + path('addDiet/', AddDiet.as_view(), name='addDiet'), + path('followDiet//', FollowDiet.as_view(), name='followDiet'), + path('prohibitIngredient//', ProhibitIngredient.as_view(), name='prohibitIngredient'), + path('dietUserView/', DietUserView.as_view(), name='dietUserView'), + path('ingredientUserView/', IngredientUserView.as_view(), name='ingredientUserView'), path('rest-auth/', include('rest_auth.urls')), url(r'^rest-auth/registration/', include('rest_auth.registration.urls')) ] diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py index 296f22e..0b6a78a 100644 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py +++ b/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py @@ -1,10 +1,12 @@ from django.shortcuts import render from django.http import Http404 from decimal import Decimal -from django.db.models import F, ExpressionWrapper, FloatField, Avg, Prefetch +from django.db.models import F, Q, ExpressionWrapper, FloatField, Avg, Prefetch, Count +from django.db.models.expressions import RawSQL import datetime from rest_framework.views import APIView from rest_framework import viewsets +from rest_framework import status from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response @@ -57,6 +59,16 @@ class DrinkViewSet(viewsets.ModelViewSet): authentication_classes = (SessionAuthentication, BasicAuthentication) permission_classes = (IsAuthenticated,) + def get_queryset(self): + user = self.request.user + + drinks = Drink.objects.all() + + if not user.role.role_id == 1: + drinks = drinks.filter(drink_is_approved=True) + + return drinks + def get(self, request, format=None): content = { 'user': unicode(request.user), @@ -85,6 +97,16 @@ class FoodViewSet(viewsets.ModelViewSet): authentication_classes = (SessionAuthentication, BasicAuthentication) permission_classes = (IsAuthenticated,) + def get_queryset(self): + user = self.request.user + + foods = Food.objects.all() + + if not user.role.role_id == 1: + foods = foods.filter(food_is_approved=True) + + return foods + def get(self, request, format=None): content = { 'user': unicode(request.user), @@ -370,8 +392,8 @@ class RestaurantUserView(APIView): restaurantRatings = restaurantRatings.annotate(username = F('user__username')) \ .annotate(diet_name = F('diet__diet_name')).values() - restaurantFoods = Food.objects.all().filter(restaurant=restaurant) - restaurantDrinks = Drink.objects.all().filter(restaurant=restaurant) + restaurantFoods = Food.objects.all().filter(restaurant=restaurant).filter(food_is_approved=True) + restaurantDrinks = Drink.objects.all().filter(restaurant=restaurant).filter(drink_is_approved=True) foods = [FoodSerializer(food).data for food in list(restaurantFoods)] drinks = [DrinkSerializer(drink).data for drink in list(restaurantDrinks)] @@ -478,7 +500,7 @@ class ProfileUserView(APIView): def get(self, request, profile, format=None): try: profile = User.objects.get(pk=profile) - except Restaurant.DoesNotExist: + except User.DoesNotExist: raise Http404 user = request.user @@ -530,4 +552,224 @@ class ProfileUserView(APIView): 'drinkRatings': drinkRatings, 'restaurantRatings': restaurantRatings} + return Response(data) + +class AddFood(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def post(self, request, format=None): + requestData = request.data.copy() + ingredientsList = [] + if 'ingredients' in requestData: + import json + ingredients = requestData.pop('ingredients') + ingredientsJson = json.loads(ingredients[0]) + + for ingredient in ingredientsJson: + try: + ingredientInstance = Ingredient.objects.get(ingredient_name=ingredient['ingredient_name']) + except Ingredient.DoesNotExist: + if 'ingredient_has_alcohol' in ingredient: + ingredient['ingredient_has_alcohol'] = False if ingredient['ingredient_has_alcohol'] == "false" else True + serializer = IngredientSerializer(data = ingredient) + if serializer.is_valid(): + ingredientInstance = serializer.save() + ingredientsList += [ingredientInstance] + + foodSerializer = FoodSerializer(data = requestData) + if foodSerializer.is_valid(): + food = foodSerializer.save() + else: + return Response(foodSerializer.errors, status=status.HTTP_400_BAD_REQUEST) + + for ingredient in ingredientsList: + relation = { + 'food': food.food_id, + 'ingredient_name': ingredient.ingredient_name + } + serializer = FoodHasIngredientSerializer(data = relation) + if serializer.is_valid(): + serializer.save() + return Response(foodSerializer.data) + +class AddDrink(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def post(self, request, format=None): + requestData = request.data.copy() + ingredientsList = [] + if 'ingredients' in requestData: + import json + ingredients = requestData.pop('ingredients') + ingredientsJson = json.loads(ingredients[0]) + + for ingredient in ingredientsJson: + try: + ingredientInstance = Ingredient.objects.get(ingredient_name=ingredient['ingredient_name']) + except Ingredient.DoesNotExist: + if 'ingredient_has_alcohol' in ingredient: + ingredient['ingredient_has_alcohol'] = False if ingredient['ingredient_has_alcohol'] == "false" else True + serializer = IngredientSerializer(data = ingredient) + if serializer.is_valid(): + ingredientInstance = serializer.save() + ingredientsList += [ingredientInstance] + + if request.user.role.role_id == 1: + requestData['drink_is_approved'] = True + else: + requestData['drink_is_approved'] = False + + drinkSerializer = DrinkSerializer(data = requestData) + if drinkSerializer.is_valid(): + drink = drinkSerializer.save() + else: + return Response(drinkSerializer.errors, status=status.HTTP_400_BAD_REQUEST) + + for ingredient in ingredientsList: + relation = { + 'drink': drink.drink_id, + 'ingredient_name': ingredient.ingredient_name + } + serializer = DrinkHasIngredientSerializer(data = relation) + if serializer.is_valid(): + serializer.save() + return Response(drinkSerializer.data) + +class AddDiet(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def post(self, request, format=None): + requestData = request.data.copy() + ingredientsList = [] + if 'ingredients' in requestData: + import json + ingredients = requestData.pop('ingredients') + ingredientsJson = json.loads(ingredients[0]) + + for ingredient in ingredientsJson: + try: + ingredientInstance = Ingredient.objects.get(ingredient_name=ingredient['ingredient_name']) + except Ingredient.DoesNotExist: + if 'ingredient_has_alcohol' in ingredient: + ingredient['ingredient_has_alcohol'] = False if ingredient['ingredient_has_alcohol'] == "false" else True + serializer = IngredientSerializer(data = ingredient) + if serializer.is_valid(): + ingredientInstance = serializer.save() + ingredientsList += [ingredientInstance] + + if request.user.role.role_id == 1: + requestData['diet_is_approved'] = True + else: + requestData['diet_is_approved'] = False + + dietSerializer = DietSerializer(data = requestData) + if dietSerializer.is_valid(): + diet = dietSerializer.save() + else: + return Response(dietSerializer.errors, status=status.HTTP_400_BAD_REQUEST) + + for ingredient in ingredientsList: + relation = { + 'drink': diet.diet_id, + 'ingredient_name': ingredient.ingredient_name + } + serializer = DrinkHasIngredientSerializer(data = relation) + if serializer.is_valid(): + serializer.save() + return Response(dietSerializer.data) + +class FollowDiet(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def patch(self, request, diet, format=None): + try: + diet = Diet.objects.get(pk=diet) + except Diet.DoesNotExist: + raise Http404 + + if not diet.diet_is_approved == 1: + raise PermissionDenied({"message":"You don't have permission to access"}) + + user = request.user + + userFollowsDiet = UserFollowsDiet.objects.filter(user=user, diet=diet) + if userFollowsDiet == None or userFollowsDiet.count() == 0: + serializer = UserFollowsDietSerializer(data={'user': user.id, 'diet': diet.diet_id}) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + userFollowsDiet.delete() + return Response(status=status.HTTP_204_NO_CONTENT) + +class ProhibitIngredient(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def patch(self, request, ingredient, format=None): + try: + ingredient = Ingredient.objects.get(pk=ingredient) + except Ingredient.DoesNotExist: + raise Http404 + + user = request.user + + userProhibitsIngredient = UserProhibitsIngredient.objects.filter(user=user, ingredient_name=ingredient) + if userProhibitsIngredient == None or userProhibitsIngredient.count() == 0: + serializer = UserProhibitsIngredientSerializer(data={'user': user.id, 'ingredient_name': ingredient.ingredient_name}) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data) + else: + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + else: + userProhibitsIngredient.delete() + return Response(status=status.HTTP_204_NO_CONTENT) + +class DietUserView(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def get(self, request, format=None): + user = self.request.user + + userDiets = UserFollowsDiet.objects.filter(user=user) + userDiets = [UserFollowsDietSerializer(diet).data for diet in list(userDiets)] + diets = Diet.objects.all() + + if not user.role.role_id == 1: + diets = diets.filter(diet_is_approved=True) + + diets = [DietSerializer(diet).data for diet in list(diets)] + + data = { + 'diets': diets, + 'followed': userDiets + } + + return Response(data) + +class IngredientUserView(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def get(self, request, format=None): + user = self.request.user + + userProhibitsIngredient = UserProhibitsIngredient.objects.filter(user=user) + userProhibitsIngredient = [UserProhibitsIngredientSerializer(ingredient).data for ingredient in list(userProhibitsIngredient)] + ingredients = Ingredient.objects.all() + ingredients = [IngredientSerializer(ingredient).data for ingredient in list(ingredients)] + + data = { + 'ingredients': ingredients, + 'prohibits': userProhibitsIngredient + } + return Response(data) \ No newline at end of file