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)
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')
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)
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)
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)
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)
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)