class Meta: model = UserModel exists = True fields = ('author_pk',) validators = [ RequiredValidator(fields=('author_pk',)) ]
class Meta: model = ImageModel fields = ('id', 'image', 'artwork') read_only_fields = ('id', ) extra_kwargs = { 'image': { 'write_only': True }, 'artwork': { 'write_only': True } } validators = [RequiredValidator(fields=('image', 'artwork'))]
class Meta: model = UserModel fields = ('id', 'username', 'email', 'password', 'first_name', 'last_name', 'avatar', 'is_superuser', 'rating') read_only_fields = ('id', 'first_name', 'last_name', 'avatar', 'rating', 'is_superuser') extra_kwargs = { 'password': { 'write_only': True }, } validators = [ RequiredValidator(fields=('username', 'email', 'password')), UsernameValidator(), PasswordValidator() ]
class Meta: model = CommentModel fields = ('id', 'text', 'author', 'author_details', 'artwork') read_only_fields = ('id', 'author_details') extra_kwargs = { 'text': { 'write_only': True }, 'author': { 'write_only': True }, 'artwork': { 'write_only': True } } validators = [RequiredValidator(fields=('text', 'author', 'artwork'))]
class EditSelfPasswordAPIView(APIView, UpdateUserModelMixin, APIViewValidationMixin): validators = (RequiredValidator(fields=('old_password', 'new_password')), PasswordValidator(password_key='new_password')) def put(self, request, *args, **kwargs): validated_data = self.validate_data(request) if isinstance(validated_data, exceptions.ValidationError): return Response({'detail': validated_data.detail}, status=validated_data.status_code) user = self.get_object() if not user.check_password(validated_data['old_password']): raise exceptions.ValidationError('Current password is incorrect.') user.set_password(validated_data['new_password']) user.save() return Response(status=200)
class Meta: model = ArtworkModel fields = ('id', 'description', 'tags', 'author') read_only_fields = ('id', ) extra_kwargs = { 'description': { 'write_only': True }, 'tags': { 'write_only': True }, 'author': { 'write_only': True } } validators = [ RequiredValidator(fields=('description', 'tags', 'author')) ]
class DeactivateSelfAPIView(APIView, UpdateUserModelMixin, APIViewValidationMixin): validators = (RequiredValidator(fields=('password', )), ) def put(self, request, *args, **kwargs): result = self.validate_data(request) if isinstance(result, exceptions.ValidationError): raise result validated_data = result user = self.get_object() if not user.check_password(validated_data['password']): raise exceptions.ValidationError('Password is incorrect.') if not user.is_active: raise exceptions.ValidationError('Account is already deactivated') user.is_active = False user.save() return Response(status=200)
class EditSelfEmailAPIView(APIView, UpdateUserModelMixin, APIViewValidationMixin): validators = (RequiredValidator(fields=('email', 'password')), ) def update(self, request, *args, **kwargs): validated_data = self.validate_data(request) if isinstance(validated_data, exceptions.ValidationError): return Response({'detail': validated_data.detail}, status=validated_data.status_code) instance = self.get_object() if not instance.check_password(validated_data['password']): raise exceptions.NotAuthenticated('Password is incorrect.') email = validated_data['email'] try: validate_email(email) except ValidationError: raise exceptions.ValidationError('Email is not valid.') instance.email = email instance.save() return Response(data={'email': email}, status=200)
class Meta: model = TagModel fields = ('text',) validators = [ RequiredValidator(fields=('text',)) ]
class Meta: model = CommentModel fields = ('mark', 'points') read_only_fields = ('points', ) mark_range = [-1, 1] validators = [RequiredValidator(fields=('mark', ))]
class Meta: model = CommentModel fields = ('text', ) extra_kwargs = {'text': {'write_only': True}} validators = [RequiredValidator(fields=('text', ))]
class Meta: model = ArtworkModel fields = ('mark', 'points', 'votes_count') read_only_fields = ('points', 'votes_count') mark_range = range(-10, 12) validators = [RequiredValidator(fields=('mark', ))]