广告小程序后端开发(14.个人中心:获取我的消息,将我的消息标记为已读,删除我的消息)

1.获取我的消息,将我的消息标记为已读,删除我的消息

1.在apps/user_operation新建序列化类serializers.py备用:

from rest_framework import serializers
from .models import Message,GetPrize,UserAdress,Fans,MakeMoney,GetCash


class MessageModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Message
        fields="__all__"


class GetPrizeModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = GetPrize
        fields="__all__"


class UserAdressModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserAdress
        fields="__all__"


class FansModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Fans
        fields="__all__"


class MakeMoneyModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = MakeMoney
        fields="__all__"


class GetCashModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = GetCash
        fields="__all__"

 2.在apps/user_operations/views.py中:

from django.shortcuts import render,HttpResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer
from gg.settings import HOST
from datetime import datetime
from users.models import UserProfile
from .serializers import MessageModelSerializer
from .models import Message

# Create your views here.


class GetMessage(APIView):
    """
    获取消息
    """
    renderer_classes = [JSONRenderer]  # 渲染器

    def get(self,request):
        token = request.GET.get('token')
        user = UserProfile.objects.filter(token=token).first()
        if user:
            msg_list=Message.objects.filter(user=user).order_by('-add_time')
            msg=MessageModelSerializer(msg_list,many=True)
            return Response(msg.data)
        else:
            return HttpResponse('参数不足')


class ReadMessage(APIView):
    """将消息标记为已读"""
    renderer_classes = [JSONRenderer]  # 渲染器

    def get(self,request):
        token = request.GET.get('token')
        user = UserProfile.objects.filter(token=token).first()
        if user:
            msg_list=Message.objects.filter(user=user,is_read=False)
            for item in msg_list:
                item.is_read=True
                item.save()
            return Response(200)
        else:
            return HttpResponse('参数不足')


class DelMessage(APIView):
    """删除消息"""
    renderer_classes = [JSONRenderer]  # 渲染器

    def get(self,request):
        token = request.GET.get('token')
        user = UserProfile.objects.filter(token=token).first()
        msg_id=request.GET.get('msgid')
        if user and msg_id:
            msg_id=int(msg_id)
            Message.objects.filter(id=msg_id).delete()
            return HttpResponse(200)
        else:
            return HttpResponse('参数不足')

3.在apps/user_operations目录下新建urls.py:

from django.urls import path
from .views import GetMessage,ReadMessage,DelMessage

urlpatterns = [
    path('getmessage/',GetMessage.as_view()),
    path('readmessage/',ReadMessage.as_view()),
    path('delmessage/',DelMessage.as_view()),
]

4.在gg/urls.py中配置:

#……

urlpatterns = [
    #……
    path('useroperation/',include('user_operation.urls'))
]

 

posted @ 2019-06-04 20:15  雪落忆海  阅读(403)  评论(0编辑  收藏  举报