【Vue+DRF 生鲜电商】用户个人中心(六)

1. docs 文档

drf 的 api 文档优点:

  • 自动生成
  • 支持交互
  • 支持多语言片段(js、Python、shell

1、MxShop/urls.py

# drf 文档,title:文档标题
path('docs', include_docs_urls(title='hubery')),

2、文档注释:

class UserAddressViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin, mixins.ListModelMixin,
                         mixins.DestroyModelMixin, mixins.UpdateModelMixin):
    """
    文档注释:
    用户收件地址:新增地址(CreateModelMixin)、删除地址(DestroyModelMixin)
    、所有地址(ListModelMixin)、更新地址(UpdateModelMixin)
    """

访问地址:http://127.0.0.1:8000/docs(最后不要加 /

踩坑:docs 文档访问出现:AttributeError: 'AutoSchema' object has no attribute 'get_link'

配置 settings

REST_FRAMEWORK = {
    # docs 文档相关
    "DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.AutoSchema"
}

2. 用户信息

接口地址:

# 获取用户详情:get
http://127.0.0.1:8000/users/1/

# 提交:post
http://127.0.0.1:8000/users/1/

# 修改:update(patch)
http://127.0.0.1:8000/users/1/

需求:

  • 获取单个用户信息(只能获取当前登录用户的信息,所以必须登录)
  • 修改用户信息

1、users/serializers.py

class UserDetailSerializer(serializers.ModelSerializer):
    """用户详情:用于用户个人中心"""
    class Meta:
        model = User
        fields = ("name", "birthday", "gender", "email", "mobile")

2、users/views.py

class UserCreateViewSet(CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, viewsets.GenericViewSet):
    """
    创建用户、注册用户、修改用户信息、获取用户详情
    """
    serializer_class = UserSerializer
    queryset = User.objects.all()
    authentication_classes = (JSONWebTokenAuthentication, authentication.SessionAuthentication)

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = self.perform_create(serializer)
        re_dict = serializer.data
        payload = jwt_payload_handler(user)
        re_dict["token"] = jwt_encode_handler(payload)
        re_dict["name"] = user.name if user.name else user.username

        headers = self.get_success_headers(serializer.data)

        return Response(re_dict, status=status.HTTP_201_CREATED, headers=headers)

    def get_permissions(self):
        """
        设置动态权限:用户注册时没有权限限制,获取用户详情必须登录才可以
        :return:
        """
        if self.action == "retrieve":
            return [permissions.IsAuthenticated()]
        elif self.action == "create":
            return []

        return []

    def get_serializer_class(self):
        """
        动态选择使用哪个序列化方式
        UserSerializer:用于用户注册,UserDetailSerializer:用于返回用户详情
        :return:
        """
        if self.action == 'retrieve':
            return UserDetailSerializer
        elif self.action == 'create':
            return UserSerializer
        return UserDetailSerializer

    def get_object(self):
        """
        获取登录用户
        用于返回用户对象,判断当前是哪个用户,配合 UserDetailSerializer 获取用户详情
        :return:
        """
        return self.request.user  # 即当前登录用户

    def perform_create(self, serializer):
        return serializer.save()

与注册使用的同一视图函数 UserCreateViewSet,只是在原有基础上多继承了 RetrieveModelMixin,用于获取用户详情。

另外需要注意的是视图函数要动态设置权限认证、序列化方法:

  • 用户注册:不需要 jwt 验证,序列化类 UserSerializer
  • 用户详情:需要 jwt 验证,序列化类 UserDetailSerializer

因此需要重写 get_permissions()、get_serializers_class() 方法,根据动作动态选择。

3. 用户收藏

接口地址:

# 获取所有收藏:get,ListModelMixin
http://127.0.0.1:8000/userfavs/

# 取消收藏:delete,需要指定商品 id ,DestroyModelMixin
http://127.0.0.1:8000/userfavs/1/

1、user_operation/serializers.py

class UserFavDetailSerializer(serializers.ModelSerializer):
    """
    收藏详情
    """
    # 通过商品 id 获取收藏的商品,需要嵌套商品的序列化
    goods = GoodsSerializer()

    class Meta:
        model = UserFav
        fields = ('goods', 'id')

2、user_operation/views.py

class UserFavViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.CreateModelMixin, mixins.DestroyModelMixin):
    """
    用户商品收藏
    ListModelMixin:收藏列表
    CreateModelMixin:收藏
    DestroyModelMixin:取消(删除)收藏,相应地要删除数据库中数据
    """
    serializer_class = UserFavSerializer
    queryset = UserFav.objects.all()

    # IsAuthenticated:必须登录用户;IsOwnerOrReadOnly:必须是当前登录的用户
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)

    # 用户认证
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)

    # 搜索的字段
    lookup_field = 'goods_id'

    def get_serializer_class(self):
        """
        动态设置 serializer,get 时获取用户收藏详情
        :return:
        """
        if self.action == 'list':
            # 收藏列表
            return UserFavDetailSerializer
        elif self.action == 'create':
            return UserFavSerializer

        return UserFavSerializer

    def get_queryset(self):
        # 只能查看当前登录用户的收藏,禁止获取其他用户的收藏
        return UserFav.objects.filter(user=self.request.user)

4. 用户留言

接口地址:

# 获取所有留言:get
http://127.0.0.1:8000/messages/

# 添加留言:post 参数
http://127.0.0.1:8000/messages/

# 删除留言:delete
http://127.0.0.1:8000/messages/messageId

需求:

  • 获取当前登录用户所有留言:ListModelMixin
  • 新建留言:CreateModelMixin
  • 删除留言:DestroyModelMixin

1、user_operation/serializers.py

class UserLeavingMessageSerializer(serializers.ModelSerializer):
    """用户留言"""
    # 获取当前登录用户
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )
    # read_only:只返回,post时候可以不用提交,format:格式化输出
    add_time = serializers.DateTimeField(read_only=True, format="%Y-%m-%d %H:%M")

    class Meta:
        model = UserLeavingMessage
        fields = ('user', 'message_type', 'subject', 'message', 'file', 'id', 'add_time')

2、user_operation/views.py

class UserLeavingMessageViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin, mixins.ListModelMixin,
                                mixins.DestroyModelMixin):
    """
    用户留言:添加留言、删除留言、留言列表
    """
    serializer_class = UserLeavingMessageSerializer
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)

    def get_queryset(self):
        """用户只能看自己的留言"""
        return UserLeavingMessage.objects.filter(user=self.request.user)

3、MxShop/urls.py

router.register(r'messages', UserLeavingMessageViewSet, basename='message')  # 用户留言

注意:这里没有处理文件相关,所以必须上传文件

5. 用户收藏地址

接口地址:

# 获取所有地址:get
http://127.0.0.1:8000/address/

# 添加地址:post 参数
http://127.0.0.1:8000/address/

# 删除地址:delete
http://127.0.0.1:8000/address/addressID

# 修改地址:update
http://127.0.0.1:8000/address/addressID

需求:

  • 获取当前登录用户所有收件地址:ListModelMixin
  • 新建地址:CreateModelMixin
  • 删除地址:DestroyModelMixin
  • 更新地址:updateModelMixin

1、user_operation/serializers.py

class UserAddressSerializer(serializers.ModelSerializer):
    """用户地址"""
    # 获取当前用户
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )
    add_time = serializers.DateTimeField(read_only=True, format="%Y-%m-%d %H:%M")

    class Meta:
        model = UserAddress
        fields = ('user', 'province', 'city', 'district', 'address', 'signer_name', 'signer_mobile', 'add_time', 'id')

2、user_operation/views.py

class UserAddressViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin, mixins.ListModelMixin,
                         mixins.DestroyModelMixin, mixins.UpdateModelMixin):
    """
    用户收件地址:新增地址(CreateModelMixin)、删除地址(DestroyModelMixin)
    、所有地址(ListModelMixin)、更新地址(UpdateModelMixin)
    """
    serializer_class = UserAddressSerializer
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)

    def get_queryset(self):
        """用户只能查看自己的收藏地址"""
        return UserAddress.objects.filter(user=self.request.user)

3、MxShop/urls.py

router.register(r'address', UserAddressViewSet, basename='address')  # 收藏地址

posted @ 2020-09-07 22:46  Hubery_Jun  阅读(458)  评论(0编辑  收藏  举报