Browse Source

Add API support for profile view, Minor fix

master
Apostolos Fanakis 6 years ago
parent
commit
65c236fede
  1. 3
      UI/Database API/hyrieus/flavoursAPI/hyrieus/urls.py
  2. 65
      UI/Database API/hyrieus/flavoursAPI/hyrieus/views.py

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

@ -7,7 +7,7 @@ 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, FoodUserView, DrinkUserView, ProfileUserView,
) )
router = routers.DefaultRouter() router = routers.DefaultRouter()
@ -35,6 +35,7 @@ urlpatterns = [
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'),
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('userDiets/', UserDiets.as_view(), name='userDiets'), path('userDiets/', UserDiets.as_view(), name='userDiets'),
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'))

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

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