Browse Source

Add API support for diets and ingredients views

master
Apostolos Fanakis 6 years ago
parent
commit
09d1da2cec
  1. 11
      UI/Database API/hyrieus/flavoursAPI/hyrieus/models.py
  2. 10
      UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py
  3. 250
      UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py

11
UI/Database API/hyrieus/flavoursAPI/hyrieus/models.py

@ -29,7 +29,6 @@ class Diet(models.Model):
managed = True managed = True
db_table = 'diet' db_table = 'diet'
class DietProhibitsIngredient(models.Model): class DietProhibitsIngredient(models.Model):
diet = models.ForeignKey(Diet, models.DO_NOTHING) diet = models.ForeignKey(Diet, models.DO_NOTHING)
ingredient_name = models.ForeignKey('Ingredient', models.DO_NOTHING, db_column='ingredient_name') ingredient_name = models.ForeignKey('Ingredient', models.DO_NOTHING, db_column='ingredient_name')
@ -50,7 +49,6 @@ class Drink(models.Model):
managed = True managed = True
db_table = 'drink' db_table = 'drink'
class DrinkHasIngredient(models.Model): class DrinkHasIngredient(models.Model):
drink = models.ForeignKey(Drink, models.DO_NOTHING) drink = models.ForeignKey(Drink, models.DO_NOTHING)
ingredient_name = models.ForeignKey('Ingredient', models.DO_NOTHING, db_column='ingredient_name') 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' db_table = 'drink_has_ingredient'
unique_together = (('drink', 'ingredient_name'),) unique_together = (('drink', 'ingredient_name'),)
class Food(models.Model): class Food(models.Model):
food_id = models.AutoField(primary_key=True) food_id = models.AutoField(primary_key=True)
food_name = models.CharField(max_length=500) food_name = models.CharField(max_length=500)
@ -73,7 +70,6 @@ class Food(models.Model):
managed = True managed = True
db_table = 'food' db_table = 'food'
class FoodHasIngredient(models.Model): class FoodHasIngredient(models.Model):
food = models.ForeignKey(Food, models.DO_NOTHING) food = models.ForeignKey(Food, models.DO_NOTHING)
ingredient_name = models.ForeignKey('Ingredient', models.DO_NOTHING, db_column='ingredient_name') ingredient_name = models.ForeignKey('Ingredient', models.DO_NOTHING, db_column='ingredient_name')
@ -91,7 +87,6 @@ class Ingredient(models.Model):
managed = True managed = True
db_table = 'ingredient' db_table = 'ingredient'
class Permission(models.Model): class Permission(models.Model):
permission_id = models.AutoField(primary_key=True) permission_id = models.AutoField(primary_key=True)
permission_description = models.CharField(max_length=700) permission_description = models.CharField(max_length=700)
@ -100,7 +95,6 @@ class Permission(models.Model):
managed = True managed = True
db_table = 'permission' db_table = 'permission'
class Restaurant(models.Model): class Restaurant(models.Model):
restaurant_id = models.AutoField(primary_key=True) restaurant_id = models.AutoField(primary_key=True)
restaurant_name = models.CharField(max_length=500) restaurant_name = models.CharField(max_length=500)
@ -125,7 +119,6 @@ class RoleHasPermission(models.Model):
db_table = 'role_has_permission' db_table = 'role_has_permission'
unique_together = (('role', 'permission'),) unique_together = (('role', 'permission'),)
class UserFollowsDiet(models.Model): class UserFollowsDiet(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING) user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING)
diet = models.ForeignKey(Diet, models.DO_NOTHING) diet = models.ForeignKey(Diet, models.DO_NOTHING)
@ -135,7 +128,6 @@ class UserFollowsDiet(models.Model):
db_table = 'user_follows_diet' db_table = 'user_follows_diet'
unique_together = (('user', 'diet'),) unique_together = (('user', 'diet'),)
class UserProhibitsIngredient(models.Model): class UserProhibitsIngredient(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING) user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING)
ingredient_name = models.ForeignKey(Ingredient, models.DO_NOTHING, db_column='ingredient_name') 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' db_table = 'user_prohibits_ingredient'
unique_together = (('user', 'ingredient_name'),) unique_together = (('user', 'ingredient_name'),)
class UserRatesDrink(models.Model): class UserRatesDrink(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING) user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING)
drink = models.ForeignKey(Drink, models.DO_NOTHING) drink = models.ForeignKey(Drink, models.DO_NOTHING)
@ -159,7 +150,6 @@ class UserRatesDrink(models.Model):
db_table = 'user_rates_drink' db_table = 'user_rates_drink'
unique_together = (('user', 'drink'),) unique_together = (('user', 'drink'),)
class UserRatesFood(models.Model): class UserRatesFood(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING) user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING)
food = models.ForeignKey(Food, models.DO_NOTHING) food = models.ForeignKey(Food, models.DO_NOTHING)
@ -173,7 +163,6 @@ class UserRatesFood(models.Model):
db_table = 'user_rates_food' db_table = 'user_rates_food'
unique_together = (('user', 'food'),) unique_together = (('user', 'food'),)
class UserRatesRestaurant(models.Model): class UserRatesRestaurant(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING, blank=True, null=True) user = models.ForeignKey(settings.AUTH_USER_MODEL, models.DO_NOTHING, blank=True, null=True)
restaurant = models.ForeignKey(Restaurant, models.DO_NOTHING) restaurant = models.ForeignKey(Restaurant, models.DO_NOTHING)

10
UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py

@ -7,7 +7,8 @@ from flavoursAPI.hyrieus.views import (DietViewSet, DietProhibitsIngredientViewS
RoleHasPermissionViewSet, UserViewSet, UserFollowsDietViewSet, RoleHasPermissionViewSet, UserViewSet, UserFollowsDietViewSet,
UserProhibitsIngredientViewSet, UserRatesDrinkViewSet, UserRatesFoodViewSet, UserProhibitsIngredientViewSet, UserRatesDrinkViewSet, UserRatesFoodViewSet,
UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets, UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets,
FoodUserView, DrinkUserView, ProfileUserView, FoodUserView, DrinkUserView, ProfileUserView, AddFood, AddDrink, AddDiet,
FollowDiet, ProhibitIngredient, DietUserView, IngredientUserView,
) )
router = routers.DefaultRouter() router = routers.DefaultRouter()
@ -37,6 +38,13 @@ urlpatterns = [
path('drinkUserView/<int:drink>/', DrinkUserView.as_view(), name='drinkUserView'), path('drinkUserView/<int:drink>/', DrinkUserView.as_view(), name='drinkUserView'),
path('profileUserView/<int:profile>/', ProfileUserView.as_view(), name='profileUserView'), path('profileUserView/<int:profile>/', ProfileUserView.as_view(), name='profileUserView'),
path('userDiets/', UserDiets.as_view(), name='userDiets'), 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/<int:diet>/', FollowDiet.as_view(), name='followDiet'),
path('prohibitIngredient/<ingredient>/', 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')), path('rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')) url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))
] ]

250
UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py

@ -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)
Loading…
Cancel
Save