|
@ -2,6 +2,7 @@ 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, ExpressionWrapper, FloatField, Avg, Prefetch |
|
|
|
|
|
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.authentication import SessionAuthentication, BasicAuthentication |
|
|
from rest_framework.authentication import SessionAuthentication, BasicAuthentication |
|
@ -366,7 +367,8 @@ class RestaurantUserView(APIView): |
|
|
.annotate(average_rating = Avg('rating_grade')) \ |
|
|
.annotate(average_rating = Avg('rating_grade')) \ |
|
|
.annotate(diet_name = F('diet__diet_name')) |
|
|
.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) |
|
|
restaurantFoods = Food.objects.all().filter(restaurant=restaurant) |
|
|
restaurantDrinks = Drink.objects.all().filter(restaurant=restaurant) |
|
|
restaurantDrinks = Drink.objects.all().filter(restaurant=restaurant) |
|
@ -468,3 +470,64 @@ class DrinkUserView(APIView): |
|
|
'ratings': drinkRatings} |
|
|
'ratings': drinkRatings} |
|
|
|
|
|
|
|
|
return Response(data) |
|
|
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) |