def list(self, request): page = request.GET.get('page') users = self.get_queryset() if page is not None: users = paginate(users, page) serializer = UserGeneralSerializer(users, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
class PostGeneralSerializer(serializers.ModelSerializer): class Meta: model = Post fields = '__all__' user_id = UserGeneralSerializer() country_id = CountryGeneralSerializer()
class CommentGeneralSerializer(serializers.ModelSerializer): class Meta: model = Comment fields = '__all__' user_id = UserGeneralSerializer() post_id = PostGeneralSerializer()
def current_user(self, request): try: user = Token.objects.get(key=request.data.get('token')).user serializer = UserGeneralSerializer(user) return Response(serializer.data, status=status.HTTP_200_OK) except: return Response({}, status=status.HTTP_404_NOT_FOUND)
class ReactionGeneralSerializer(serializers.ModelSerializer): class Meta: model = Reaction fields = '__all__' user_id = UserGeneralSerializer() post_id = PostGeneralSerializer()
def AuthenticateUser(request): data = json.loads(request.body) username = data['username'] password = data['password'] user = get_object_or_404(User.objects.all(), username=username) if user.check_password(password): login(request, user) serializer = UserGeneralSerializer(user) return JsonResponse(data=serializer.data, status=status.HTTP_200_OK) else: return JsonResponse(data={}, status=status.HTTP_401_UNAUTHORIZED)
def new_password(self, request): try: user = Token.objects.get(key=request.data.get('token')).user new_password = request.data.get('new_password') request.data['password'] = new_password serializer = UserResetPasswordSerializer(user, request.data) login(request, user) if serializer.is_valid(raise_exception=True): serializer.save() serializer = UserGeneralSerializer(user) return Response(serializer.data, status=status.HTTP_200_OK) except: return Response({}, status=status.HTTP_404_NOT_FOUND)
class PostSingleGeneralSerializer(serializers.ModelSerializer): comments = serializers.SerializerMethodField() like_count = serializers.SerializerMethodField() sad_count = serializers.SerializerMethodField() love_count = serializers.SerializerMethodField() def get_comments(self, instance): return_comments = [] comments = Comment.objects.filter(post_id=instance.id).values( 'id', 'user_id', 'comment', 'date_created').order_by('-date_created') for comment in comments: display = {} user = User.objects.get(pk=comment.get('user_id')) display['id'] = comment.get('id') display['user_id'] = user.id display['username'] = user.username display['avatar'] = user.avatar display['date_created'] = comment.get('date_created') display['comment'] = comment.get('comment') return_comments.append(display) return return_comments def get_like_count(self, instance): return Reaction.objects.filter(post_id=instance.id, reaction=1).count() def get_sad_count(self, instance): return Reaction.objects.filter(post_id=instance.id, reaction=2).count() def get_love_count(self, instance): return Reaction.objects.filter(post_id=instance.id, reaction=3).count() class Meta: model = Post fields = '__all__' user_id = UserGeneralSerializer() country_id = CountryGeneralSerializer()
def retrieve(self, request, pk=None): user = get_object_or_404(self.get_queryset(), pk=pk) serializer = UserGeneralSerializer(user) return Response(serializer.data, status=status.HTTP_200_OK)