first rest api
This commit is contained in:
parent
84267c626c
commit
e05d8a2349
@ -17,8 +17,13 @@ from django.conf.urls import url, include
|
|||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
|
||||||
from apps.food import views
|
from apps.food import views
|
||||||
|
|
||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
router.register(r'api', views.FoodViewSet)
|
router.register(r'all', views.FoodViewSet)
|
||||||
|
router.register(r'loc/(?P<location>.+)/$', views.FoodList.as_view(), base_name="food-on-loaction")
|
||||||
|
router.register(r'date/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.FoodList.as_view(),
|
||||||
|
base_name="food-on-day")
|
||||||
|
# router.register(r'(?P<location>.+)/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.FoodList.as_view(), base_name="food-list")
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^$', views.food, name='food'),
|
url(r'^$', views.food, name='food'),
|
||||||
@ -28,5 +33,12 @@ urlpatterns = [
|
|||||||
url(r'^all/$', views.food, name='all-food'),
|
url(r'^all/$', views.food, name='all-food'),
|
||||||
url(r'^daily/rating/$', views.food_rating, name='rating-food'),
|
url(r'^daily/rating/$', views.food_rating, name='rating-food'),
|
||||||
url(r'^weekly/rating/$', views.food_rating, name='rating-food'),
|
url(r'^weekly/rating/$', views.food_rating, name='rating-food'),
|
||||||
url(r'^', include(router.urls)),
|
|
||||||
|
url(r'^api/', include(router.urls)),
|
||||||
|
url(r'^api/(?P<location>[a-zA-Z]+)/$', views.FoodList.as_view(), name='rating-food'),
|
||||||
|
url(r'^api/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.FoodList.as_view(),
|
||||||
|
name='rating-food'),
|
||||||
|
url(r'^api/(?P<location>[a-zA-Z]+)/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', views.FoodList.as_view(),
|
||||||
|
name='rating-food'),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|||||||
@ -7,7 +7,10 @@ from django.shortcuts import render
|
|||||||
|
|
||||||
from apps.food.models import Menu, HappyHour, SingleFood
|
from apps.food.models import Menu, HappyHour, SingleFood
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets, generics
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.decorators import api_view
|
||||||
|
from rest_framework.response import Response
|
||||||
from apps.food.serializers import MenuSerializer, SingleFoodSerializer
|
from apps.food.serializers import MenuSerializer, SingleFoodSerializer
|
||||||
|
|
||||||
|
|
||||||
@ -106,7 +109,15 @@ def food_image(request):
|
|||||||
food.save()
|
food.save()
|
||||||
return HttpResponse(status=200)
|
return HttpResponse(status=200)
|
||||||
|
|
||||||
return HttpResponse(status=404) #
|
return HttpResponse(status=404)
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
def serialize_daily_food(request, location="", year="", month="", day=""):
|
||||||
|
request_date = datetime.datetime.strptime((year + "-" + month + "-" + day), "%Y-%m-%d")
|
||||||
|
queryset = Menu.objects.filter(location__contains=location).filter(date__exact=request_date)
|
||||||
|
serializer = MenuSerializer(queryset)
|
||||||
|
return Response(serializer)
|
||||||
|
|
||||||
|
|
||||||
class FoodViewSet(viewsets.ModelViewSet):
|
class FoodViewSet(viewsets.ModelViewSet):
|
||||||
@ -115,3 +126,26 @@ class FoodViewSet(viewsets.ModelViewSet):
|
|||||||
"""
|
"""
|
||||||
queryset = Menu.objects.all()
|
queryset = Menu.objects.all()
|
||||||
serializer_class = MenuSerializer
|
serializer_class = MenuSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class FoodList(generics.ListAPIView):
|
||||||
|
serializer_class = MenuSerializer
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""
|
||||||
|
This view should return a list of all the purchases for
|
||||||
|
the user as determined by the username portion of the URL.
|
||||||
|
"""
|
||||||
|
if 'location' in self.kwargs:
|
||||||
|
location = self.kwargs['location']
|
||||||
|
else:
|
||||||
|
location = ""
|
||||||
|
|
||||||
|
if 'year' in self.kwargs and 'month' in self.kwargs and 'day' in self.kwargs:
|
||||||
|
request_date = datetime.datetime.strptime(
|
||||||
|
(self.kwargs['year'] + "-" + self.kwargs['month'] + "-" + self.kwargs['day']), "%Y-%m-%d")
|
||||||
|
else:
|
||||||
|
request_date = datetime.datetime.now()
|
||||||
|
print("LOCATION: " + location)
|
||||||
|
print("DATE: " + str(request_date))
|
||||||
|
return Menu.objects.filter(location__contains=location).filter(date__exact=request_date)
|
||||||
|
|||||||
Reference in New Issue
Block a user