Browse Source

Add API support for moderators, Fixes

master
Apostolos Fanakis 6 years ago
parent
commit
29e5ef363a
  1. 8
      UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py
  2. 93
      UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py

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

@ -9,7 +9,8 @@ from flavoursAPI.hyrieus.views import (DietViewSet, DietProhibitsIngredientViewS
UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets, UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets,
FoodUserView, DrinkUserView, ProfileUserView, AddFood, AddDrink, AddDiet, FoodUserView, DrinkUserView, ProfileUserView, AddFood, AddDrink, AddDiet,
FollowDiet, ProhibitIngredient, DietUserView, IngredientUserView, FollowDiet, ProhibitIngredient, DietUserView, IngredientUserView,
AddIngredientToFood, AddIngredientToDrink, AddIngredientToFood, AddIngredientToDrink, AcceptRestaurant,AcceptFood,
AcceptDrink, AcceptDiet,
) )
router = routers.DefaultRouter() router = routers.DefaultRouter()
@ -32,7 +33,6 @@ router.register(r'userratesfood', UserRatesFoodViewSet)
router.register(r'userratesrestaurant', UserRatesRestaurantViewSet) router.register(r'userratesrestaurant', UserRatesRestaurantViewSet)
urlpatterns = [ urlpatterns = [
# url(r'signup', Signup.as_view()),
path('setUserBirthday/<int:pk>/', UserSetBirthDay.as_view(), name='setUserBirthday'), path('setUserBirthday/<int:pk>/', UserSetBirthDay.as_view(), name='setUserBirthday'),
path('restaurantUserView/<int:restaurant>/', RestaurantUserView.as_view(), name='restaurantUserView'), path('restaurantUserView/<int:restaurant>/', RestaurantUserView.as_view(), name='restaurantUserView'),
path('foodUserView/<int:food>/', FoodUserView.as_view(), name='foodUserView'), path('foodUserView/<int:food>/', FoodUserView.as_view(), name='foodUserView'),
@ -48,6 +48,10 @@ urlpatterns = [
path('ingredientUserView/', IngredientUserView.as_view(), name='ingredientUserView'), path('ingredientUserView/', IngredientUserView.as_view(), name='ingredientUserView'),
path('addIngredientToFood/', AddIngredientToFood.as_view(), name='addIngredientToFood'), path('addIngredientToFood/', AddIngredientToFood.as_view(), name='addIngredientToFood'),
path('addIngredientToDrink/', AddIngredientToDrink.as_view(), name='addIngredientToDrink'), path('addIngredientToDrink/', AddIngredientToDrink.as_view(), name='addIngredientToDrink'),
path('acceptRestaurant/<int:restaurant>/', AcceptRestaurant.as_view(), name='acceptRestaurant'),
path('acceptFood/<int:food>/', AcceptFood.as_view(), name='acceptFood'),
path('acceptDrink/<int:drink>/', AcceptDrink.as_view(), name='acceptDrink'),
path('acceptDiet/<int:diet>/', AcceptDiet.as_view(), name='acceptDiet'),
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'))
] ]

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

@ -296,6 +296,7 @@ class UserRatesDrinkViewSet(viewsets.ModelViewSet):
def perform_create(self, serializer): def perform_create(self, serializer):
user = self.request.user user = self.request.user
print(self.request)
try: try:
drink = Drink.objects.get(pk=self.request._data.get('drink')) drink = Drink.objects.get(pk=self.request._data.get('drink'))
@ -419,8 +420,11 @@ 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).filter(food_is_approved=True) restaurantFoods = Food.objects.all().filter(restaurant=restaurant)
restaurantDrinks = Drink.objects.all().filter(restaurant=restaurant).filter(drink_is_approved=True) restaurantDrinks = Drink.objects.all().filter(restaurant=restaurant)
if not user.role.role_id == 1:
restaurantFoods = restaurantFoods.filter(food_is_approved=True)
restaurantDrinks = restaurantDrinks.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)]
@ -607,6 +611,11 @@ class AddFood(APIView):
ingredientInstance = serializer.save() ingredientInstance = serializer.save()
ingredientsList += [ingredientInstance] ingredientsList += [ingredientInstance]
if request.user.role.role_id == 1:
requestData['food_is_approved'] = True
else:
requestData['food_is_approved'] = False
foodSerializer = FoodSerializer(data = requestData) foodSerializer = FoodSerializer(data = requestData)
if foodSerializer.is_valid(): if foodSerializer.is_valid():
food = foodSerializer.save() food = foodSerializer.save()
@ -865,3 +874,83 @@ class AddIngredientToDrink(APIView):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.data) return Response(serializer.data)
class AcceptRestaurant(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
def patch(self, request, restaurant, format=None):
user = request.user
if not user.role.role_id == 1:
raise PermissionDenied({"message":"You don't have permission to access"})
try:
restaurant = Restaurant.objects.get(pk=restaurant)
except Restaurant.DoesNotExist:
raise Http404
serializer = RestaurantSerializer(restaurant, data={'restaurant_is_approved': True}, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class AcceptFood(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
def patch(self, request, food, format=None):
user = request.user
if not user.role.role_id == 1:
raise PermissionDenied({"message":"You don't have permission to access"})
try:
food = Food.objects.get(pk=food)
except Food.DoesNotExist:
raise Http404
serializer = FoodSerializer(food, data={'food_is_approved': True}, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class AcceptDrink(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
def patch(self, request, drink, format=None):
user = request.user
if not user.role.role_id == 1:
raise PermissionDenied({"message":"You don't have permission to access"})
try:
drink = Drink.objects.get(pk=drink)
except Drink.DoesNotExist:
raise Http404
serializer = DrinkSerializer(drink, data={'drink_is_approved': True}, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class AcceptDiet(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
def patch(self, request, diet, format=None):
user = request.user
if not user.role.role_id == 1:
raise PermissionDenied({"message":"You don't have permission to access"})
try:
diet = Diet.objects.get(pk=diet)
except Diet.DoesNotExist:
raise Http404
serializer = DietSerializer(diet, data={'diet_is_approved': True}, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Loading…
Cancel
Save