Exemplo n.º 1
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        headers = self.get_success_headers(serializer.data)
        #if serializer.data.get('fan')==serializer.data.get('follow'):
        if serializer.validated_data['fan'] == serializer.validated_data[
                'follow']:
            return Response({"message": "不能自己关注自己"},
                            status=status.HTTP_201_CREATED,
                            headers=headers)
        #elif Follows.objects.filter(fan=serializer.data.get('fan'),follow=serializer.data.get('follow')).exists():
        elif Follows.objects.filter(
                fan=serializer.validated_data['fan'],
                follow=serializer.validated_data['follow']).exists():
            return Response({"message": "不能重复关注"},
                            status=status.HTTP_201_CREATED,
                            headers=headers)
        else:
            save = Follows(follow=serializer.validated_data['follow'],
                           fan=serializer.validated_data['fan'])
            save.save()
            return Response({"message": "关注成功"},
                            status=status.HTTP_201_CREATED,
                            headers=headers)
Exemplo n.º 2
0
 def post(self, request):
     print(request.user.is_authenticated)
     if request.user.is_authenticated:
         froms = Follow_Forms(request.POST)
         username = request.POST.get('username')
         if froms.is_valid():
             follow = Follows()
             if request.POST.get('follow') == str(username):
                 return JsonResponse({'status': 201, 'message': '不能自己关注自己'})
             else:
                 cun = Follows.objects.filter(
                     follow=froms.cleaned_data.get('follow'), fan=username)
                 if cun:
                     cun.delete()
                     return JsonResponse({
                         'status': 200,
                         'message': '已取消关注'
                     })
                 follow.follow = froms.cleaned_data.get('follow')
                 follow.fan_id = request.user.id
                 follow.save()
                 return JsonResponse({'status': 200, 'message': '成功关注'})
         else:
             return JsonResponse({'status': 400, 'message': '失败'})
     else:
         return JsonResponse({'status': 403, 'message': '请先登录'})