diff --git a/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py b/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py index 8f6666c..884a7a1 100644 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py +++ b/UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py @@ -7,7 +7,7 @@ from flavoursAPI.hyrieus.views import (DietViewSet, DietProhibitsIngredientViewS RoleHasPermissionViewSet, UserViewSet, UserFollowsDietViewSet, UserProhibitsIngredientViewSet, UserRatesDrinkViewSet, UserRatesFoodViewSet, UserRatesRestaurantViewSet, UserSetBirthDay, RestaurantUserView, UserDiets, - FoodUserView, DrinkUserView, + FoodUserView, DrinkUserView, ProfileUserView, ) router = routers.DefaultRouter() @@ -35,6 +35,7 @@ urlpatterns = [ path('restaurantUserView//', RestaurantUserView.as_view(), name='restaurantUserView'), path('foodUserView//', FoodUserView.as_view(), name='foodUserView'), path('drinkUserView//', DrinkUserView.as_view(), name='drinkUserView'), + path('profileUserView//', ProfileUserView.as_view(), name='profileUserView'), path('userDiets/', UserDiets.as_view(), name='userDiets'), 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 bab9785..296f22e 100644 --- a/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py +++ b/UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py @@ -2,6 +2,7 @@ from django.shortcuts import render from django.http import Http404 from decimal import Decimal from django.db.models import F, ExpressionWrapper, FloatField, Avg, Prefetch +import datetime from rest_framework.views import APIView from rest_framework import viewsets from rest_framework.authentication import SessionAuthentication, BasicAuthentication @@ -366,7 +367,8 @@ class RestaurantUserView(APIView): .annotate(average_rating = Avg('rating_grade')) \ .annotate(diet_name = F('diet__diet_name')) - restaurantRatings = restaurantRatings.annotate(username = F('user__username')).values() + 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) @@ -467,4 +469,65 @@ class DrinkUserView(APIView): 'ingredients': drinkIngredients, 'ratings': drinkRatings} + return Response(data) + +class ProfileUserView(APIView): + authentication_classes = (SessionAuthentication, BasicAuthentication) + permission_classes = (IsAuthenticated,) + + def get(self, request, profile, format=None): + try: + profile = User.objects.get(pk=profile) + except Restaurant.DoesNotExist: + raise Http404 + + user = request.user + + if profile == user or user.role.role_id == 1: + dateToday = datetime.date.today() + ageInYears = dateToday.year - profile.user_age.year + if dateToday.month < profile.user_age.month or \ + (dateToday.month == profile.user_age.month and dateToday.day < profile.user_age.day): + ageInYears -= 1 + + profileInfo = { + 'email': profile.email, + 'username': profile.username, + 'age': ageInYears + } + else: + profileInfo = { + 'email': None, + 'username': profile.username, + 'age': -1 + } + + userDiets = UserFollowsDiet.objects.all().filter(user=profile).prefetch_related('diet') + userDiets = Diet.objects.filter(diet_id__in = userDiets.values('diet')) \ + .values('diet_id', 'diet_name') + + profileInfo['diets'] = userDiets + + ownsRestaurants = Restaurant.objects.filter(user=profile).values('restaurant_id', 'restaurant_name') + profileInfo['owns'] = ownsRestaurants + + restaurantRatings = UserRatesRestaurant.objects.filter(user=profile).prefetch_related('user') \ + .prefetch_related('diet') + restaurantRatings = restaurantRatings.annotate(username = F('user__username')) \ + .annotate(diet_name = F('diet__diet_name')).values() + + profileInfo['reviewsNumber'] = restaurantRatings.count() + + + foodRatings = UserRatesFood.objects.filter(user=profile).prefetch_related('user') + foodRatings = foodRatings.annotate(username = F('user__username')).values() + + drinkRatings = UserRatesDrink.objects.filter(user=profile).prefetch_related('user') + drinkRatings = drinkRatings.annotate(username = F('user__username')).values() + + data = {'userInfo': profileInfo, + 'foodRatings': foodRatings, + 'drinkRatings': drinkRatings, + 'restaurantRatings': restaurantRatings} + return Response(data) \ No newline at end of file