From f1eb239a739444f21c920a5c989bced80c69669e Mon Sep 17 00:00:00 2001 From: Yirou Qiu <y2qiu@uwaterloo.ca> Date: Sun, 19 Mar 2023 17:25:42 -0400 Subject: [PATCH] Added an api for searching the sum of price in different categories --- core/api/billPriceSum.py | 44 ++++++++++++++++++++++++++++++++++++++++ core/api/billSearch.py | 1 - core/models/bill.py | 13 ------------ core/urls.py | 6 +++++- 4 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 core/api/billPriceSum.py diff --git a/core/api/billPriceSum.py b/core/api/billPriceSum.py new file mode 100644 index 0000000..1fcefc4 --- /dev/null +++ b/core/api/billPriceSum.py @@ -0,0 +1,44 @@ +from rest_framework import generics, response +from core.models.bill import Bill, BillSearchQuerySet +from core.serializers.bill import BillSerializer +from rest_framework import authentication +from django.db.models import Sum + +class BillSumPriceListView(generics.ListAPIView): + queryset = Bill.objects.all() + serializer_class = BillSerializer + authentication_classes = [ + authentication.SessionAuthentication, + authentication.TokenAuthentication + ] + + + def get(self, request, *args, **kwargs): + categorieModel = { + 1: "Food", + 2: "Groceries", + 3: "Transportation", + 4: "clothing", + 5: "Entertainment", + 6: "Bill", + 7: "Sports", + 8: "Electronics", + 9: "Travel", + 10: "House & Car", + 11: "Others", + } + queryset = self.filter_queryset(self.get_queryset()) + user = None + if self.request.user.is_authenticated: + user = self.request.user + data = {} + + for i in range(1,12): + qs = BillSearchQuerySet(Bill).searchCategories(query=i, user=user) + total_price = qs.aggregate(Sum('price')) + print(total_price) + data[categorieModel[i]] = total_price['price__sum'] + print(data) + return response.Response(data) + +BillSumPriceListViewAPI = BillSumPriceListView.as_view() \ No newline at end of file diff --git a/core/api/billSearch.py b/core/api/billSearch.py index f35d1e8..a976a1b 100644 --- a/core/api/billSearch.py +++ b/core/api/billSearch.py @@ -35,5 +35,4 @@ class SearchBillListView(generics.ListAPIView): result = qs.searchToday(user=user) elif item == "title": result = qs.searchTitle(keyword, user=user) - print(result) return result \ No newline at end of file diff --git a/core/models/bill.py b/core/models/bill.py index e834fc3..3579d86 100644 --- a/core/models/bill.py +++ b/core/models/bill.py @@ -41,20 +41,7 @@ class BillSearchQuerySet(models.QuerySet): if user is not None: qs = self.filter(user=user).filter(lookup) return qs - - # def searchTitle(self, keyword, user=None): - # lookup = Q(title__contains = keyword) - # qs = self.filter(lookup) - # if user is not None: - # qs = self.filter(user=user).filter(lookup) - # return qs - - # def categorieAmount(self, date, categories, user=None): - # if date == "day": - # qs = self.searchCategories(categories, user=user).searchToday(user=user) - # elif date == "month": - # qs = self.searchCategories(categories, user=user).searchThisMonth(user=user) class BillManager(models.Manager): diff --git a/core/urls.py b/core/urls.py index e5180f0..b8b9db7 100644 --- a/core/urls.py +++ b/core/urls.py @@ -8,6 +8,7 @@ from core.api.profile import ProfileViewSet from core.api.coupon import CouponViewSet from core.api.bill import * from core.api.billSearch import SearchBillListView +from core.api.billPriceSum import BillSumPriceListViewAPI router = routers.DefaultRouter() @@ -36,7 +37,10 @@ urlpatterns += [ path('api/bill/<uuid:pk>/', bill_detail_api, name="bill-detail"), # bill search - path('api/bill/search/', SearchBillListView.as_view(), name="bill-search") + path('api/bill/search/', SearchBillListView.as_view(), name="bill-search"), + + # bill price list searched by categories + path('api/bill/price-sum/', BillSumPriceListViewAPI, name="bill-price-sum") ] -- GitLab