From 595e51a960128b85faa655eea6069fbddf6e9bbe Mon Sep 17 00:00:00 2001 From: Ge Zhu <g4zhu@uwaterloo.ca> Date: Mon, 13 Feb 2023 15:39:27 -0500 Subject: [PATCH] Create Coupon Model/Serializer/View for /api/coupon. --- core/api/coupon.py | 11 +++++++++++ core/models/coupon.py | 25 +++++++++++++++++++++++++ core/models/coupon_category.py | 11 +++++++++++ core/serializers/coupon.py | 10 ++++++++++ core/urls.py | 5 ++++- 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 core/api/coupon.py create mode 100644 core/models/coupon.py create mode 100644 core/models/coupon_category.py create mode 100644 core/serializers/coupon.py diff --git a/core/api/coupon.py b/core/api/coupon.py new file mode 100644 index 0000000..6e68e91 --- /dev/null +++ b/core/api/coupon.py @@ -0,0 +1,11 @@ +from rest_framework import viewsets +from rest_framework.permissions import IsAuthenticated + +from core.serializers.coupon import CouponSerializer +from core.models.coupon import Coupon + + +class CouponViewSet(viewsets.ReadOnlyModelViewSet): + permission_classes = (IsAuthenticated,) + serializer_class = CouponSerializer + queryset = Coupon.objects.all() diff --git a/core/models/coupon.py b/core/models/coupon.py new file mode 100644 index 0000000..9982c5e --- /dev/null +++ b/core/models/coupon.py @@ -0,0 +1,25 @@ +from django.db import models + +from .coupon_category import CouponCategory +from .utils import UUIDModel + + +class Coupon (UUIDModel): + store = models.CharField(max_length=100) + merchant_homepage = models.URLField(max_length=200) + offer_text = models.CharField(max_length=100) + offer_value = models.CharField(max_length=100) + title = models.CharField(max_length=200, default='') + description = models.TextField(default='') + code = models.CharField(max_length=50) + smart_link = models.URLField(max_length=200) + image_url = models.URLField(max_length=200) + type = models.CharField(max_length=50) + status = models.CharField(max_length=50) + start_date = models.DateField('coupon valid date') + expire_date = models.DateField('coupon expired date') + categories = models.ManyToManyField(to=CouponCategory) + + def __str__(self) -> str: + return self.coupon_text[:12] + '...' + diff --git a/core/models/coupon_category.py b/core/models/coupon_category.py new file mode 100644 index 0000000..ce6c37b --- /dev/null +++ b/core/models/coupon_category.py @@ -0,0 +1,11 @@ +from django.db import models + +from .utils import UUIDModel + + +class CouponCategory(UUIDModel): + category = models.CharField(max_length=50) + sub_category = models.CharField(max_length=50) + + def __str__(self) -> str: + return f'<CouponCategory: {self.category}-{self.sub_category}>' diff --git a/core/serializers/coupon.py b/core/serializers/coupon.py new file mode 100644 index 0000000..be88d37 --- /dev/null +++ b/core/serializers/coupon.py @@ -0,0 +1,10 @@ +from rest_framework import serializers + +from core.models.coupon import Coupon + + +class CouponSerializer(serializers.ModelSerializer): + class Meta: + model = Coupon + fields = ('__all__', ) + depth = 1 diff --git a/core/urls.py b/core/urls.py index 02cc948..7a014bc 100644 --- a/core/urls.py +++ b/core/urls.py @@ -5,6 +5,7 @@ from rest_framework import routers from core.api.auth import RegisterAPI, LoginAPI, AppleLogin, GoogleLogin, FacebookLogin, validate_token, verify_user_and_activate from core.api.password import ChangePasswordView from core.api.profile import ProfileViewSet +from core.api.coupon import CouponViewSet router = routers.DefaultRouter() @@ -24,5 +25,7 @@ urlpatterns += [ path('api/change-password', ChangePasswordView.as_view(), name='change-password'), path('api/password_reset/', include('django_rest_passwordreset.urls', namespace='password_reset')), # profile - path('api/profile', ProfileViewSet.as_view(), name='profile') + path('api/profile', ProfileViewSet.as_view(), name='profile'), + # coupon + path('api/coupon', CouponViewSet.as_view(), name='coupon'), ] -- GitLab