Skip to content

Commit aee56a9

Browse files
committed
第八章完结,完成了商品详情页功能,实现了热卖商品接口,用户收藏接口,drf权限验证。
1 parent 7a7efa2 commit aee56a9

File tree

10 files changed

+312
-178
lines changed

10 files changed

+312
-178
lines changed

.idea/workspace.xml

Lines changed: 226 additions & 170 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VueDjangoFrameWorkShop/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,12 @@
162162
'DEFAULT_AUTHENTICATION_CLASSES': (
163163
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
164164
'rest_framework.authentication.BasicAuthentication',
165-
'rest_framework.authentication.SessionAuthentication',
166165
)
167166
}
168167

169168
# 与drf的jwt相关的设置
170169
JWT_AUTH = {
171-
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=20),
170+
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=3600),
172171
'JWT_AUTH_HEADER_PREFIX': 'Bearer',
173172
}
174173

VueDjangoFrameWorkShop/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from VueDjangoFrameWorkShop.settings import MEDIA_ROOT
2424
from goods.views import GoodsListViewSet, CategoryViewset
25+
from user_operation.views import UserFavViewset
2526
from users.views import SmsCodeViewset, UserViewset
2627
# from goods.views import GoodsListView,
2728
# from goods.views_base import GoodsListView
@@ -46,6 +47,8 @@
4647
# 配置users的url
4748
router.register(r'users', UserViewset, base_name="users")
4849

50+
# 配置用户收藏的url
51+
router.register(r'userfavs', UserFavViewset, base_name="userfavs")
4952
urlpatterns = [
5053
# path('admin/', admin.site.urls),
5154
path('xadmin/', xadmin.site.urls),

apps/goods/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def top_category_filter(self, queryset, name, value):
2626

2727
class Meta:
2828
model = Goods
29-
fields = ['pricemin', 'pricemax', 'name']
29+
fields = ['pricemin', 'pricemax', 'name', 'is_hot']
3030
# fields = {
3131
# 'shop_price': ['gte','lte', 'icontains'],
3232
# }

apps/goods/serializers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__author__ = 'mtianyan'
33
__date__ = '2018/2/14 0014 16:44'
44

5-
from goods.models import Goods, GoodsCategory
5+
from goods.models import Goods, GoodsCategory, GoodsImage
66
from rest_framework import serializers
77

88

@@ -12,8 +12,14 @@ class Meta:
1212
fields = "__all__"
1313

1414

15+
class GoodsImageSerializer(serializers.ModelSerializer):
16+
class Meta:
17+
model = GoodsImage
18+
fields = ("image",)
19+
1520
class GoodsSerializer(serializers.ModelSerializer):
1621
category = CategorySerializer()
22+
images = GoodsImageSerializer(many=True)
1723

1824
class Meta:
1925
model = Goods

apps/goods/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class GoodsPagination(PageNumberPagination):
3232
# class GoodsListView(mixins.ListModelMixin, generics.GenericAPIView):
3333
# class GoodsListView(ListAPIView):
3434
# class GoodsListView(mixins.ListModelMixin, viewsets.GenericViewSet):
35-
class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
35+
class GoodsListViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
3636
"""
37-
商品列表页,分页,搜索,过滤,排序
37+
商品列表页,分页,搜索,过滤,排序,取某一个具体商品的详情
3838
"""
3939

4040
# queryset是一个属性

apps/user_operation/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from goods.models import Goods
44
from django.contrib.auth import get_user_model
55
# Create your models here.
6+
67
User = get_user_model()
78

89

@@ -18,7 +19,7 @@ class Meta:
1819
verbose_name = '用户收藏'
1920
verbose_name_plural = verbose_name
2021

21-
#
22+
# 多个字段作为一个联合唯一索引
2223
unique_together = ("user", "goods")
2324

2425
def __str__(self):

apps/user_operation/serializers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# encoding: utf-8
2+
from rest_framework.validators import UniqueTogetherValidator
3+
4+
__author__ = 'mtianyan'
5+
__date__ = '2018/3/10 0010 09:54'
6+
from rest_framework import serializers
7+
from user_operation.models import UserFav
8+
9+
10+
class UserFavSerializer(serializers.ModelSerializer):
11+
user = serializers.HiddenField(
12+
default=serializers.CurrentUserDefault()
13+
)
14+
15+
class Meta:
16+
model = UserFav
17+
18+
# 使用validate方式实现唯一联合
19+
validators = [
20+
UniqueTogetherValidator(
21+
queryset=UserFav.objects.all(),
22+
fields=('user', 'goods'),
23+
message="已经收藏"
24+
)
25+
]
26+
27+
fields = ("user", "goods", "id")

apps/user_operation/views.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
from django.shortcuts import render
2-
2+
from rest_framework import viewsets
3+
from rest_framework import mixins
34
# Create your views here.
5+
from user_operation.models import UserFav
6+
from user_operation.serializers import UserFavSerializer
7+
from rest_framework.permissions import IsAuthenticated
8+
from utils.permissions import IsOwnerOrReadOnly
9+
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
10+
from rest_framework.authentication import SessionAuthentication
11+
12+
13+
class UserFavViewset(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.CreateModelMixin, mixins.RetrieveModelMixin,
14+
mixins.DestroyModelMixin):
15+
"""
16+
用户收藏功能
17+
"""
18+
# queryset = UserFav.objects.all()
19+
permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
20+
serializer_class = UserFavSerializer
21+
lookup_field = 'goods_id'
22+
authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
23+
24+
def get_queryset(self):
25+
return UserFav.objects.filter(user=self.request.user)

apps/utils/permissions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# encoding: utf-8
2+
__author__ = 'mtianyan'
3+
__date__ = '2018/3/10 0010 17:38'
4+
from rest_framework import permissions
5+
6+
7+
class IsOwnerOrReadOnly(permissions.BasePermission):
8+
"""
9+
Object-level permission to only allow owners of an object to edit it.
10+
Assumes the model instance has an `owner` attribute.
11+
"""
12+
13+
def has_object_permission(self, request, view, obj):
14+
# Read permissions are allowed to any request,
15+
# so we'll always allow GET, HEAD or OPTIONS requests.
16+
if request.method in permissions.SAFE_METHODS:
17+
return True
18+
19+
# Instance must have an attribute named `owner`.
20+
return obj.user == request.user

0 commit comments

Comments
 (0)