@ -1,127 +1,327 @@
from django . shortcuts import render
from decimal import Decimal
from django . db . models import F , ExpressionWrapper , FloatField
from rest_framework . views import APIView
from rest_framework import viewsets
from . models import (
Diet ,
DietProhibitsIngredient ,
Drink ,
DrinkHasIngredient ,
Food ,
FoodHasIngredient ,
Ingredient ,
Permission ,
Restaurant ,
Role ,
RoleHasPermission ,
User ,
UserFollowsDiet ,
UserProhibitsIngredient ,
UserRatesDrink ,
UserRatesFood ,
UserRatesRestaurant ,
from rest_framework . authentication import SessionAuthentication , BasicAuthentication
from rest_framework . permissions import IsAuthenticated
from rest_framework . response import Response
from rest_framework . exceptions import PermissionDenied
from . models import ( Diet , DietProhibitsIngredient , Drink , DrinkHasIngredient ,
Food , FoodHasIngredient , Ingredient , Permission , Restaurant , Role ,
RoleHasPermission , User , UserFollowsDiet , UserProhibitsIngredient ,
UserRatesDrink , UserRatesFood , UserRatesRestaurant ,
)
from . serializers import (
DietSerializer ,
DietProhibitsIngredientSerializer ,
DrinkSerializer ,
DrinkHasIngredientSerializer ,
FoodSerializer ,
FoodHasIngredientSerializer ,
IngredientSerializer ,
PermissionSerializer ,
RestaurantSerializer ,
RoleSerializer ,
RoleHasPermissionSerializer ,
UserSerializer ,
UserFollowsDietSerializer ,
UserProhibitsIngredientSerializer ,
UserRatesDrinkSerializer ,
UserRatesFoodSerializer ,
DietSerializer , DietProhibitsIngredientSerializer , DrinkSerializer ,
DrinkHasIngredientSerializer , FoodSerializer , FoodHasIngredientSerializer ,
IngredientSerializer , PermissionSerializer , RestaurantSerializer ,
RoleSerializer , RoleHasPermissionSerializer , UserSerializer ,
UserFollowsDietSerializer , UserProhibitsIngredientSerializer ,
UserRatesDrinkSerializer , UserRatesFoodSerializer ,
UserRatesRestaurantSerializer ,
)
class DietViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = Diet . objects . all ( )
serializer_class = DietSerializer
class DietProhibitsIngredientViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = DietProhibitsIngredient . objects . all ( )
serializer_class = DietProhibitsIngredientSerializer
class DrinkViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = Drink . objects . all ( )
serializer_class = DrinkSerializer
class DrinkHasIngredientViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = DrinkHasIngredient . objects . all ( )
serializer_class = DrinkHasIngredientSerializer
class FoodViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = Food . objects . all ( )
serializer_class = FoodSerializer
class FoodHasIngredientViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = FoodHasIngredient . objects . all ( )
serializer_class = FoodHasIngredientSerializer
class IngredientViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = Ingredient . objects . all ( )
serializer_class = IngredientSerializer
class PermissionViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = Permission . objects . all ( )
serializer_class = PermissionSerializer
class RestaurantViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get_queryset ( self ) :
user = self . request . user
filterType = self . request . query_params . get ( ' filter_restaurant_type ' , None )
filterDistanceRadius = self . request . query_params . get ( ' filter_restaurant_radius ' , None )
filterDistanceLongitude = self . request . query_params . get ( ' filter_restaurant_longitude ' , None )
filterDistanceLatitude = self . request . query_params . get ( ' filter_restaurant_latitude ' , None )
filterCalories = self . request . query_params . get ( ' filter_restaurant_calories ' , None )
restaurants = Restaurant . objects . all ( )
if filterDistanceRadius is not None :
filterDistanceRadius = float ( filterDistanceRadius )
filterDistanceLongitude = float ( filterDistanceLongitude )
filterDistanceLatitude = float ( filterDistanceLatitude )
import math
restaurants = restaurants . filter (
restaurant_latitude__range = ( filterDistanceLatitude - ( filterDistanceRadius / 111.045 ) ,
filterDistanceLatitude + ( filterDistanceRadius / 111.045 ) ) )
restaurants = restaurants . filter (
restaurant_longitude__range = ( filterDistanceLongitude - ( filterDistanceRadius / ( 111.045 * math . cos ( math . radians ( filterDistanceLatitude ) ) ) ) ,
filterDistanceLongitude + ( filterDistanceRadius / ( 111.045 * math . cos ( math . radians ( filterDistanceLatitude ) ) ) ) ) )
# ======================================================================================
# WHY THE FUCK DOES BELOW CODE FAIL? :-(
# ======================================================================================
# restaurants = restaurants.annotate(distance =
# 111.045 * math.degrees(math.acos(
# math.cos(math.radians(filterDistanceLatitude))
# * math.cos(math.radians(F('restaurant_latitude')))
# * math.cos(math.radians(filterDistanceLongitude - float(F('restaurant_longitude'))))
# + math.sin(math.radians(filterDistanceLatitude))
# * math.sin(math.radians(F('restaurant_latitude')))
# )))
# restaurants = restaurants.filter(distance__lt=filterDistanceRadius)
# restaurants = restaurants.order_by('distance')
restaurants = restaurants . distinct ( ) ;
if filterType is not None :
restaurants = restaurants . filter ( restaurant_category = filterType )
if not user . role . role_id == 1 :
restaurants = restaurants . filter ( restaurant_is_approved = True )
return restaurants
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = Restaurant . objects . all ( )
serializer_class = RestaurantSerializer
class RoleViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = Role . objects . all ( )
serializer_class = RoleSerializer
class RoleHasPermissionViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = RoleHasPermission . objects . all ( )
serializer_class = RoleHasPermissionSerializer
class UserViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = User . objects . all ( )
serializer_class = UserSerializer
class UserFollowsDietViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = UserFollowsDiet . objects . all ( )
serializer_class = UserFollowsDietSerializer
class UserProhibitsIngredientViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = UserProhibitsIngredient . objects . all ( )
serializer_class = UserProhibitsIngredientSerializer
class UserRatesDrinkViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = UserRatesDrink . objects . all ( )
serializer_class = UserRatesDrinkSerializer
class UserRatesFoodViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = UserRatesFood . objects . all ( )
serializer_class = UserRatesFoodSerializer
class UserRatesRestaurantViewSet ( viewsets . ModelViewSet ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
def get ( self , request , format = None ) :
content = {
' user ' : unicode ( request . user ) ,
' auth ' : unicode ( request . auth ) ,
}
return Response ( content )
queryset = UserRatesRestaurant . objects . all ( )
serializer_class = UserRatesRestaurantSerializer
from rest_framework . views import APIView , Response
class UserSetBirthDay ( APIView ) :
authentication_classes = ( SessionAuthentication , BasicAuthentication )
permission_classes = ( IsAuthenticated , )
class Signup ( APIView ) :
def get ( self , request , format = None ) :
serializer = User . objects . all ( )
return Response ( serializer . data , status = status . HTTP_201_CREATED )
def put ( self , request , pk , format = None ) :
try :
user = User . objects . get ( pk = pk )
except Snippet . DoesNotExist :
raise Http404
def post ( self , request , format = None ) :
serializer = UserSerializer ( data = request . data )
if not request . user == user :
raise PermissionDenied ( { " message " : " You don ' t have permission to access " } )
serializer = UserSerializer ( user , data = request . data , partial = True )
if serializer . is_valid ( ) :
serializer . save ( )
return Response ( serializer . data , status = status . HTTP_201_CREATED )
return Response ( serializer . data )
return Response ( serializer . errors , status = status . HTTP_400_BAD_REQUEST )
@classmethod
def get_extra_actions ( cls ) :
return [ ]