-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
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) | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?