Skip to content

Commit

Permalink
Add uniquenessand length validators to serializer fields.
Browse files Browse the repository at this point in the history
Move username regex validator from model to serializer, since the model validation
does not run automatically on model save:
https://docs.djangoproject.com/en/dev/ref/validators/#how-validators-are-run

Note: For the importer and publisher uniqueness validator to take effect they must
be declared on the Plugin importer and publisher class like so:

Class PluginPublisherSerializer(pulpcore.plugin.serializers.PublisherSerializer):
    class Meta:
        validators = PublisherSerializer.Meta.validators

closes #2984
https://pulp.plan.io/issues/2984
  • Loading branch information
werwty committed Oct 19, 2017
1 parent 5af4bc4 commit 36e840c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
8 changes: 0 additions & 8 deletions platform/pulpcore/app/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from gettext import gettext as _

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.core import validators
from django.db import models


Expand Down Expand Up @@ -56,13 +55,6 @@ class User(AbstractBaseUser, PermissionsMixin):
verbose_name=_('username'),
max_length=150,
unique=True,
validators=[
validators.RegexValidator(
r'^[\w.@+-]+$',
_('Enter a valid username. This value may contain only letters, numbers '
'and @/./+/-/_ characters.'),
'invalid'),
],
error_messages={
'unique': _("A user with that username already exists.")
}
Expand Down
4 changes: 3 additions & 1 deletion platform/pulpcore/app/serializers/consumer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from gettext import gettext as _

from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from pulpcore.app import models
from pulpcore.app.serializers import GenericKeyValueRelatedField, ModelSerializer
Expand All @@ -13,7 +14,8 @@ class ConsumerSerializer(ModelSerializer):
)

name = serializers.CharField(
help_text=_("The consumer common name.")
help_text=_("The consumer common name."),
validators=[UniqueValidator(queryset=models.Consumer.objects.all())]
)

description = serializers.CharField(
Expand Down
32 changes: 30 additions & 2 deletions platform/pulpcore/app/serializers/repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from gettext import gettext as _

from django.core import validators

from rest_framework import serializers
from rest_framework.validators import UniqueValidator, UniqueTogetherValidator
from rest_framework_nested.serializers import NestedHyperlinkedModelSerializer

from pulpcore.app import models
Expand All @@ -25,7 +28,8 @@ class RepositorySerializer(ModelSerializer):
lookup_field='name',
)
name = serializers.CharField(
help_text=_('A unique name for this repository.')
help_text=_('A unique name for this repository.'),
validators=[UniqueValidator(queryset=models.Repository.objects.all())]
)

description = serializers.CharField(
Expand Down Expand Up @@ -148,6 +152,12 @@ class Meta:
'ssl_client_certificate', 'ssl_client_key', 'ssl_validation', 'proxy_url',
'username', 'password', 'last_synced', 'last_updated', 'repository',
)
validators = [
UniqueTogetherValidator(
queryset=models.Importer.objects.all(),
fields=('name', 'repository')
)
]


class PublisherSerializer(MasterModelSerializer, NestedHyperlinkedModelSerializer):
Expand All @@ -168,7 +178,7 @@ class PublisherSerializer(MasterModelSerializer, NestedHyperlinkedModelSerialize
repository = HrefWritableRepositoryRelatedField(read_only=True)

auto_publish = serializers.BooleanField(
help_text=_('An indicaton that the automatic publish may happen when'
help_text=_('An indication that the automatic publish may happen when'
' the repository content has changed.'),
required=False
)
Expand All @@ -191,6 +201,12 @@ class Meta:
fields = MasterModelSerializer.Meta.fields + (
'name', 'last_updated', 'repository', 'auto_publish', 'last_published', 'distributions',
)
validators = [
UniqueTogetherValidator(
queryset=models.Publisher.objects.all(),
fields=('name', 'repository')
)
]


class DistributionSerializer(ModelSerializer):
Expand All @@ -202,9 +218,21 @@ class DistributionSerializer(ModelSerializer):
)
name = serializers.CharField(
help_text=_('The name of the distribution. Ex, `rawhide` and `stable`.'),
validators=[validators.MaxLengthValidator(
models.Distribution._meta.get_field('name').max_length,
message=_('Distribution name length must be less than {} characters').format(
models.Distribution._meta.get_field('name').max_length
))]
)
base_path = serializers.CharField(
help_text=('The base (relative) path component of the published url.'),
validators=[validators.MaxLengthValidator(
models.Distribution._meta.get_field('base_path').max_length,
message=_('Distribution base_path length must be less than {} characters').format(
models.Distribution._meta.get_field('base_path').max_length
)),
UniqueValidator(queryset=models.Distribution.objects.all()),
],
)
auto_updated = serializers.BooleanField(
help_text=_('The publication is updated automatically when the publisher has created a '
Expand Down
26 changes: 22 additions & 4 deletions platform/pulpcore/app/serializers/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from gettext import gettext as _

from django.core import validators
from django.conf import settings
from django.contrib.auth.hashers import make_password
from django.utils import six
Expand Down Expand Up @@ -31,8 +32,20 @@ class UserSerializer(ModelSerializer):
lookup_field='username')

username = serializers.CharField(
help_text=_("Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."),
validators=[UniqueValidator(queryset=User.objects.all())],
help_text=_("Required. {} characters or fewer. Letters, digits and @/./+/-/_ only.").format(
User._meta.get_field('username').max_length),
validators=[UniqueValidator(queryset=User.objects.all()),
validators.RegexValidator(
regex=r'^[\w.@+-]+$',
message=_(
'Enter a valid username. This value may contain only letters, numbers'
' and @/./+/-/_ characters.'),
code='invalid'),
validators.MaxLengthValidator(
User._meta.get_field('username').max_length,
message=_('The length of username must be less than {} characters').format(
User._meta.get_field('username').max_length)),
],
)

is_superuser = serializers.BooleanField(
Expand All @@ -49,11 +62,16 @@ class UserSerializer(ModelSerializer):
jwt_secret = serializers.CharField(
help_text=_("User JWT authentication secret"),
required=False,
write_only=not settings.DEBUG # If pulp in DEBUG mode secret is visible
write_only=not settings.DEBUG, # If pulp in DEBUG mode secret is visible
validators=[validators.MaxLengthValidator(
User._meta.get_field('jwt_secret').max_length,
message=_('The length of jwt_secret must be less than {} characters').format(
User._meta.get_field('jwt_secret').max_length))
],
)

reset_jwt_secret = serializers.BooleanField(
help_text=_("Rest user JWT secret."),
help_text=_("Reset user JWT secret."),
required=False,
write_only=True,
)
Expand Down

0 comments on commit 36e840c

Please sign in to comment.