diff --git a/core/admin.py b/core/admin.py index 8c38f3f3dad51e4585f3984282c2a4bec5349c1e..d994f05bbd40837cf5c672ae10e7a898c3c5b523 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,3 +1,6 @@ from django.contrib import admin # Register your models here. +from core.models.bill import Bill + +admin.site.register(Bill) diff --git a/core/api/bill.py b/core/api/bill.py new file mode 100644 index 0000000000000000000000000000000000000000..ef0a6f1ab6cd617d811d06f2ea0eef337d5568f4 --- /dev/null +++ b/core/api/bill.py @@ -0,0 +1,69 @@ +from rest_framework import generics, mixins, status +from core.models.bill import Bill +from core.serializers.bill import BillSerializer +from rest_framework.response import Response +from rest_framework import permissions, authentication + + +class BillListCreate(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView): + queryset = Bill.objects.all() + serializer_class = BillSerializer + authentication_classes = [ + authentication.SessionAuthentication, + authentication.TokenAuthentication + ] + # permission_classes = (permissions.IsAuthenticated,) + + def get(self, request, *args, **kwargs): + return self.list(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + self.perform_create(serializer) + headers = self.get_success_headers(serializer.data) + return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + + def perform_create(self, serializer): + default_user = self.request.user + if not default_user: + default_user = None + # serializer.is_valid() + serializer.save(user = default_user) + + def get_queryset(self, *args, **kwargs): + qs =super().get_queryset(*args, **kwargs) + request = self.request + user = request.user + if not user.is_authenticated: + return Bill.objects.none() + return qs.filter(user = user) + + + +bill_list_create_api = BillListCreate.as_view() + + +class BillDetail(mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, + generics.GenericAPIView): + queryset = Bill.objects.all() + serializer_class = BillSerializer + authentication_classes = [ + authentication.SessionAuthentication, + authentication.TokenAuthentication + ] + # permission_classes = (permissions.IsAuthenticated,) + lookup_field = "pk" + + def get(self, request, *args, **kwargs): + return self.retrieve(request, *args, **kwargs) + + def put(self, request, *args, **kwargs): + return self.update(request, *args, **kwargs) + + def delete(self, request, *args, **kwargs): + return self.destroy(request, *args, **kwargs) + +bill_detail_api = BillDetail.as_view() diff --git a/core/models/bill.py b/core/models/bill.py new file mode 100644 index 0000000000000000000000000000000000000000..f26e86bf04f8cffffe2ff720ddda8896740edd35 --- /dev/null +++ b/core/models/bill.py @@ -0,0 +1,39 @@ +from django.db import models +from django.utils import timezone +from django.contrib.auth.models import User +from core.models.utils import UUIDModel +import datetime + + +class Bill(UUIDModel): + + user = models.ForeignKey(User, default=1, null=True, on_delete=models.SET_NULL) + + title = models.CharField(max_length = 120) + + date = models.DateField(verbose_name="payment time", default = datetime.date.today) + + datetime = models.DateTimeField(verbose_name="payment date & time", default = timezone.now) + + price = models.DecimalField(verbose_name='payment amount', max_digits=15, decimal_places=2, default=0.00) + + comment = models.TextField(verbose_name= "comment", blank = True, null = True, default=None) + + + # need confirmation + categories = ( + (1, "Food"), + (2, "Groceries"), + (3, "Transportation"), + (4, "clothing"), + (5, "Entertainment"), + (6, "Bill"), + (7, "Sports"), + (8, "Electronics"), + (9, "Travel"), + (10, "House & Car"), + (11, "Others") + ) + + + categories = models.SmallIntegerField(verbose_name="bill categories", choices = categories, default=11) \ No newline at end of file diff --git a/core/serializers/OwnerSerializers.py b/core/serializers/OwnerSerializers.py new file mode 100644 index 0000000000000000000000000000000000000000..b5560459acbe704d128c9bc512ba12f8ca952523 --- /dev/null +++ b/core/serializers/OwnerSerializers.py @@ -0,0 +1,5 @@ +from rest_framework import serializers + +class UserSerializer(serializers.Serializer): + username = serializers.CharField(read_only=True) + email = serializers.CharField(read_only=True) \ No newline at end of file diff --git a/core/serializers/bill.py b/core/serializers/bill.py new file mode 100644 index 0000000000000000000000000000000000000000..d3dfdff6df4a1f656b7959add1708284d80a8d59 --- /dev/null +++ b/core/serializers/bill.py @@ -0,0 +1,27 @@ +from rest_framework import serializers +from core.models.bill import Bill +from rest_framework.reverse import reverse +from core.serializers.OwnerSerializers import UserSerializer + + +class BillSerializer(serializers.ModelSerializer): + url = serializers.SerializerMethodField(read_only=True) + user = UserSerializer(read_only=True) + class Meta: + model = Bill + fields = ( + 'user', + 'url', + 'title', + 'date', + 'datetime', + 'price', + 'categories', + 'comment' + ) + + def get_url(self, obj): + request = self.context.get("request") + if not request: + return None + return reverse("bill-detail", kwargs = {"pk": obj.pk}, request = request) \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index 7a014bcf1191a337597f90cb50f70443cfe0fdc4..f515f05990588377151215d633ab91a7be0804cc 100644 --- a/core/urls.py +++ b/core/urls.py @@ -6,6 +6,7 @@ from core.api.auth import RegisterAPI, LoginAPI, AppleLogin, GoogleLogin, Facebo from core.api.password import ChangePasswordView from core.api.profile import ProfileViewSet from core.api.coupon import CouponViewSet +from core.api.bill import * router = routers.DefaultRouter() @@ -28,4 +29,8 @@ urlpatterns += [ path('api/profile', ProfileViewSet.as_view(), name='profile'), # coupon path('api/coupon', CouponViewSet.as_view(), name='coupon'), + + # bill + path('api/bill/', bill_list_create_api, name="bill-list"), + path('api/bill/<uuid:pk>/', bill_detail_api, name="bill-detail") ]