예제 #1
0
    def signup(self, request):
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        data = UserModelSerializer(user).data

        return Response(data, status=status.HTTP_201_CREATED)
예제 #2
0
class RoleRequestModelSerializer(serializers.ModelSerializer):

    requesting_user = UserModelSerializer(read_only=True)

    class Meta:
        model = RoleRequest
        fields = ('new_role', 'message', 'requesting_user', 'status')

        read_only_fields = ('new_role', 'message', 'requesting_user', 'status')
예제 #3
0
    def change_role(self, request):
        """Handle of change de role from a user petition"""
        staff = request.user
        user = User.objects.get(username=request.data['username'])
        user.role = request.data['new_role']
        user.save()
        data = UserModelSerializer(user).data

        return Response(data, status=status.HTTP_200_OK)
예제 #4
0
    def login(self, request):
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        user, token = serializer.save()

        data = {'user': UserModelSerializer(user).data, 'token': token}

        return Response(data, status=status.HTTP_201_CREATED)
예제 #5
0
 def profile(self, request, *args, **kwargs):
     """ Update profile data. """
     user = self.get_object()
     profile = user.profile
     partial = request.method == 'PATCH'
     serializer = ProfileModelSerializer(profile,
                                         data=request.data,
                                         partial=partial)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     data = UserModelSerializer(user).data
     return Response(data, status=status.HTTP_200_OK)
예제 #6
0
    def ban_users(self, request):
        """ Endpoint to bann an user. """
        user = User.objects.get(username=request.data['username'])

        if not user.is_active:
            return Response({"message": "user is already banned"},
                            status=status.HTTP_400_BAD_REQUEST)

        user.is_active = False
        user.save()
        data = UserModelSerializer(user).data

        return Response(data, status=status.HTTP_200_OK)
예제 #7
0
    def report_post(self, request, *args, **kwargs):
        post = self.get_object()
        user = request.user

        context = {'reporting_user': user, 'reported_post': post}

        data = {
            'reporting_user': UserModelSerializer(user).data,
            'reported_post': PostModelSerializer(post).data
        }

        serializer_class = self.get_serializer_class()
        serializer = serializer_class(data=data, context=context)
        serializer.is_valid(raise_exception=True)

        data = PostComplaintModelSerializer(serializer.save()).data

        return Response(data, status=status.HTTP_201_CREATED)