Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed User Order views #87

Merged
merged 3 commits into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions canteenApp/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers
from canteenWeb.models import User

class UserIDSerializer(serializers.Serializer):
userID = serializers.CharField(max_length=20)

def create(self, validated_data):
return User.objects.filter(username=validated_data.get('userID'))[0]

10 changes: 10 additions & 0 deletions canteenApp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path, include
from django.conf.urls import url, include
from rest_framework import routers
from . import views

router = routers.DefaultRouter()
router.register("orders", views.UserOrderViewSet)
urlpatterns = [
path("", include(router.urls))
]
65 changes: 64 additions & 1 deletion canteenApp/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect
from django.http import Http404
from django.http import JsonResponse
from rest_framework import viewsets, permissions, status, views
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.generics import CreateAPIView
from rest_framework.views import APIView
from drf_yasg.utils import no_body, swagger_auto_schema
from drf_yasg import openapi

# Create your views here.
from canteenWeb.models import (
Order,
MenuItem,
User,
StudentProfile,
TeacherProfile,
Bill,
Category,
Ingredients,
)
from .serializers import (
UserIDSerializer
)
from canteenWeb.serializers import (
OrderSerializer
)

class UserOrderViewSet(viewsets.ModelViewSet):
queryset = Order.objects.all()
serializer_class = OrderSerializer

@swagger_auto_schema(responses={200: OrderSerializer(many=True)})
@action(detail=False, methods=["post"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using a POST method for getting the list of completed orders?

def completed(self, request):
"""
Return a list of all completed orders for a user.
"""
serializer = UserIDSerializer(data=request.data)
if(serializer.is_valid()):
user = serializer.save()
completed_orders = Order.objects.filter(is_fulfilled=True,user=user)
serializer = self.get_serializer(completed_orders, many=True)
return Response(serializer.data)

@swagger_auto_schema(responses={200: OrderSerializer(many=True)})
@action(detail=False,methods=["post"])
def pending(self, request):
"""
Return a list of all pending orders for a user.
"""
serializer = UserIDSerializer(data=request.data)
if(serializer.is_valid()):
user = serializer.save()
pending_orders = Order.objects.filter(
is_fulfilled=False, status__gte=0,user=user
) # Should not be fulfilled and status should be positive.
serializer = self.get_serializer(pending_orders, many=True)
return Response(serializer.data)





1 change: 1 addition & 0 deletions canteenAutomation/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("canteenWeb.urls")),
path("app/", include("canteenApp.urls")),
path(
"doc/",
schema_view.with_ui("swagger", cache_timeout=1000),
Expand Down