Ejemplo n.º 1
0
def get_relation_kwargs(field_name, relation_info):
    """
    Creating a default instance of a flat relational field.
    """
    model_field, related_model = relation_info
    kwargs = {}
    if related_model and not issubclass(related_model, EmbeddedDocument):
        kwargs['queryset'] = related_model.objects

    if model_field:
        if hasattr(model_field, 'verbose_name') and needs_label(
                model_field, field_name):
            kwargs['label'] = capfirst(model_field.verbose_name)
        if hasattr(model_field, 'help_text'):
            kwargs['help_text'] = model_field.help_text

        kwargs['required'] = model_field.required

        if model_field.null:
            kwargs['allow_null'] = True
        if getattr(model_field, 'unique', False):
            validator = UniqueValidator(queryset=related_model.objects)
            kwargs['validators'] = [validator]

    return kwargs
Ejemplo n.º 2
0
class UniqueValidatorSerializer(DocumentSerializer):
    class Meta:
        model = NonValidatingModel
        fields = '__all__'

    name = serializers.CharField(
        validators=[UniqueValidator(queryset=NonValidatingModel.objects)])
Ejemplo n.º 3
0
class AgentSerializer(DocumentSerializer):

    name = CharField(allow_blank=False,
                     allow_null=True,
                     required=False,
                     validators=[UniqueValidator(Agent.objects.all())])

    options = EscapedDictField()

    class Meta:
        model = Agent
        fields = ('id', 'type', 'name', 'heartbeat', 'status', 'options')
Ejemplo n.º 4
0
class GroupSerializer(DocumentSerializer):

    name = CharField(allow_blank=False,
                     allow_null=True,
                     required=False,
                     validators=[UniqueValidator(Group.objects.all())])

    query = EscapedDictField()

    include = ListField(child=CharField(), default=list)
    exclude = ListField(child=CharField(), default=list)

    class Meta:
        model = Group
        fields = ('id', 'name', 'query', 'include', 'exclude', 'services')
    def get_uniqueness_extra_kwargs(self, field_names, extra_kwargs):
        # extra_kwargs contains 'default', 'required', 'validators=[UniqValidator]'
        # hidden_fields contains fields involved in constraints, but missing in serializer fields
        model = self.Meta.model

        uniq_extra_kwargs = {}

        hidden_fields = {}

        field_names = set(field_names)
        unique_fields = set()
        unique_together_fields = set()

        # include `unique_with` from model indexes
        # so long as all the field names are included on the serializer.
        uniq_indexes = filter(lambda i: i.get('unique', False),
                              model._meta.get('index_specs', []))
        for idx in uniq_indexes:
            field_set = set(map(lambda e: e[0], idx['fields']))
            if field_names.issuperset(field_set):
                if len(field_set) == 1:
                    unique_fields |= field_set
                else:
                    unique_together_fields |= field_set

        for field_name in unique_fields:
            uniq_extra_kwargs[field_name] = {
                'required': True,
                'validators': [UniqueValidator(queryset=model.objects)]
            }

        for field_name in unique_together_fields:
            fld = model._fields[field_name]
            if has_default(fld):
                uniq_extra_kwargs[field_name] = {'default': fld.default}
            else:
                uniq_extra_kwargs[field_name] = {'required': True}

        # Update `extra_kwargs` with any new options.
        for key, value in uniq_extra_kwargs.items():
            if key in extra_kwargs:
                if key == 'validators' and key in extra_kwargs:
                    extra_kwargs[key].append(value)
                extra_kwargs[key].update(value)
            else:
                extra_kwargs[key] = value

        return extra_kwargs, hidden_fields
Ejemplo n.º 6
0
class ProcedureSerializer(DocumentSerializer):

    name = CharField(allow_blank=False,
                     allow_null=True,
                     required=False,
                     validators=[UniqueValidator(Procedure.objects.all())])

    options = EscapedDictField()
    params = EscapedDictField()

    class Meta:
        model = Procedure
        fields = ('id', 'type', 'name', 'content', 'options', 'params')
        extra_kwargs = {
            'content': {
                'required': False,
                'allow_blank': True,
                'default': ''
            },
        }
Ejemplo n.º 7
0
class UserSerializer(DocumentSerializer):
    # override
    username = serializers.CharField(min_length=4,
                                     max_length=16,
                                     validators=[
                                         UniqueValidator(
                                             queryset=User.objects.all(),
                                             message="Already taken!")
                                     ])
    password = serializers.CharField(max_length=16, write_only=True)
    # extra field
    url = serializers.HyperlinkedIdentityField(view_name='user-detail',
                                               lookup_field='pk')
    '''
    overide validate to hashing user password
    '''
    @staticmethod
    def validate_password(value):
        return make_password(value)

    class Meta:
        model = User
        fields = '__all__'
Ejemplo n.º 8
0
class UserSerializer(DocumentSerializer):
    username = drf_serializer.CharField(validators=[UniqueValidator(User.objects.all())])

    class Meta:
        model = User
        depth = 2