Skip to content
Snippets Groups Projects
Commit 595e51a9 authored by G ZHU's avatar G ZHU
Browse files

Create Coupon Model/Serializer/View for /api/coupon.

parent 88e20a8c
No related branches found
No related tags found
1 merge request!4Merge zhuge/feature/coupon-api into main branch
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()
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] + '...'
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}>'
from rest_framework import serializers
from core.models.coupon import Coupon
class CouponSerializer(serializers.ModelSerializer):
class Meta:
model = Coupon
fields = ('__all__', )
depth = 1
...@@ -5,6 +5,7 @@ from rest_framework import routers ...@@ -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.auth import RegisterAPI, LoginAPI, AppleLogin, GoogleLogin, FacebookLogin, validate_token, verify_user_and_activate
from core.api.password import ChangePasswordView from core.api.password import ChangePasswordView
from core.api.profile import ProfileViewSet from core.api.profile import ProfileViewSet
from core.api.coupon import CouponViewSet
router = routers.DefaultRouter() router = routers.DefaultRouter()
...@@ -24,5 +25,7 @@ urlpatterns += [ ...@@ -24,5 +25,7 @@ urlpatterns += [
path('api/change-password', ChangePasswordView.as_view(), name='change-password'), path('api/change-password', ChangePasswordView.as_view(), name='change-password'),
path('api/password_reset/', include('django_rest_passwordreset.urls', namespace='password_reset')), path('api/password_reset/', include('django_rest_passwordreset.urls', namespace='password_reset')),
# profile # 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'),
] ]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment