|
@ -1,10 +1,12 @@ |
|
|
from django.shortcuts import render |
|
|
from django.shortcuts import render |
|
|
from django.http import Http404 |
|
|
from django.http import Http404 |
|
|
from decimal import Decimal |
|
|
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 |
|
|
import datetime |
|
|
from rest_framework.views import APIView |
|
|
from rest_framework.views import APIView |
|
|
from rest_framework import viewsets |
|
|
from rest_framework import viewsets |
|
|
|
|
|
from rest_framework import status |
|
|
from rest_framework.authentication import SessionAuthentication, BasicAuthentication |
|
|
from rest_framework.authentication import SessionAuthentication, BasicAuthentication |
|
|
from rest_framework.permissions import IsAuthenticated |
|
|
from rest_framework.permissions import IsAuthenticated |
|
|
from rest_framework.response import Response |
|
|
from rest_framework.response import Response |
|
@ -57,6 +59,16 @@ class DrinkViewSet(viewsets.ModelViewSet): |
|
|
authentication_classes = (SessionAuthentication, BasicAuthentication) |
|
|
authentication_classes = (SessionAuthentication, BasicAuthentication) |
|
|
permission_classes = (IsAuthenticated,) |
|
|
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): |
|
|
def get(self, request, format=None): |
|
|
content = { |
|
|
content = { |
|
|
'user': unicode(request.user), |
|
|
'user': unicode(request.user), |
|
@ -85,6 +97,16 @@ class FoodViewSet(viewsets.ModelViewSet): |
|
|
authentication_classes = (SessionAuthentication, BasicAuthentication) |
|
|
authentication_classes = (SessionAuthentication, BasicAuthentication) |
|
|
permission_classes = (IsAuthenticated,) |
|
|
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): |
|
|
def get(self, request, format=None): |
|
|
content = { |
|
|
content = { |
|
|
'user': unicode(request.user), |
|
|
'user': unicode(request.user), |
|
@ -370,8 +392,8 @@ class RestaurantUserView(APIView): |
|
|
restaurantRatings = restaurantRatings.annotate(username = F('user__username')) \ |
|
|
restaurantRatings = restaurantRatings.annotate(username = F('user__username')) \ |
|
|
.annotate(diet_name = F('diet__diet_name')).values() |
|
|
.annotate(diet_name = F('diet__diet_name')).values() |
|
|
|
|
|
|
|
|
restaurantFoods = Food.objects.all().filter(restaurant=restaurant) |
|
|
restaurantFoods = Food.objects.all().filter(restaurant=restaurant).filter(food_is_approved=True) |
|
|
restaurantDrinks = Drink.objects.all().filter(restaurant=restaurant) |
|
|
restaurantDrinks = Drink.objects.all().filter(restaurant=restaurant).filter(drink_is_approved=True) |
|
|
foods = [FoodSerializer(food).data for food in list(restaurantFoods)] |
|
|
foods = [FoodSerializer(food).data for food in list(restaurantFoods)] |
|
|
drinks = [DrinkSerializer(drink).data for drink in list(restaurantDrinks)] |
|
|
drinks = [DrinkSerializer(drink).data for drink in list(restaurantDrinks)] |
|
|
|
|
|
|
|
@ -478,7 +500,7 @@ class ProfileUserView(APIView): |
|
|
def get(self, request, profile, format=None): |
|
|
def get(self, request, profile, format=None): |
|
|
try: |
|
|
try: |
|
|
profile = User.objects.get(pk=profile) |
|
|
profile = User.objects.get(pk=profile) |
|
|
except Restaurant.DoesNotExist: |
|
|
except User.DoesNotExist: |
|
|
raise Http404 |
|
|
raise Http404 |
|
|
|
|
|
|
|
|
user = request.user |
|
|
user = request.user |
|
@ -530,4 +552,224 @@ class ProfileUserView(APIView): |
|
|
'drinkRatings': drinkRatings, |
|
|
'drinkRatings': drinkRatings, |
|
|
'restaurantRatings': restaurantRatings} |
|
|
'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) |
|
|
return Response(data) |