Exemplo n.º 1
0
    def validate(self, data):

        # Validate uniqueness of name if one has been provided.
        if data.get('name', None):
            validator = UniqueTogetherValidator(queryset=Secret.objects.all(), fields=('device', 'role', 'name'))
            validator.set_context(self)
            validator(data)

        return data
Exemplo n.º 2
0
    def validate(self, data):

        # Validate uniqueness of (site, facility_id) since we omitted the automatically-created validator from Meta.
        if data.get('facility_id', None):
            validator = UniqueTogetherValidator(queryset=Rack.objects.all(), fields=('site', 'facility_id'))
            validator.set_context(self)
            validator(data)

        return data
Exemplo n.º 3
0
    def validate(self, data):

        # Validate uniqueness of (rack, position, face) since we omitted the automatically-created validator from Meta.
        if data.get('rack') and data.get('position') and data.get('face'):
            validator = UniqueTogetherValidator(queryset=Device.objects.all(), fields=('rack', 'position', 'face'))
            validator.set_context(self)
            validator(data)

        return data
Exemplo n.º 4
0
 def _validate_unique_together_instance(self, validated_data):
     for fields in self.unique_together_validators:
         unique_together_validator = UniqueTogetherValidator(
             self.Meta.model.objects.all(), fields)
         unique_together_validator.set_context(self)
         try:
             unique_together_validator(validated_data)
         except ValidationError as exc:
             raise ValidationError({"non_field_errors": exc.detail})
Exemplo n.º 5
0
    def validate(self, data):

        # Validate uniqueness of name and slug if a site has been assigned.
        if data.get('site', None):
            for field in ['name', 'slug']:
                validator = UniqueTogetherValidator(queryset=VLAN.objects.all(), fields=('site', field))
                validator.set_context(self)
                validator(data)

        return data
Exemplo n.º 6
0
    def validate(self, data):

        # Validate uniqueness of vid and name if a group has been assigned.
        if data.get('group', None):
            for field in ['vid', 'name']:
                validator = UniqueTogetherValidator(queryset=VLAN.objects.all(), fields=('group', field))
                validator.set_context(self)
                validator(data)

        return data
Exemplo n.º 7
0
 def validate_unique_transaction(self, attrs):
     validator = UniqueTogetherValidator(queryset=Transaction.objects.all(),
                                         fields=('product__supplier_id',
                                                 'delivered',
                                                 'customer__currency'))
     validator.set_context(self)
     validator({
         'product__supplier_id': attrs['supplier_id'],
         'customer__currency': attrs['currency'],
         'delivered': attrs['date'],
     })
Exemplo n.º 8
0
    def validate(self, data):

        # Validate uniqueness of name if one has been provided.
        if data.get('name', None):
            validator = UniqueTogetherValidator(queryset=Secret.objects.all(), fields=('device', 'role', 'name'))
            validator.set_context(self)
            validator(data)

        # Enforce model validation
        super(WritableSecretSerializer, self).validate(data)

        return data
Exemplo n.º 9
0
    def validate(self, data):

        # Validate uniqueness of (site, facility_id) since we omitted the automatically-created validator from Meta.
        if data.get('facility_id', None):
            validator = UniqueTogetherValidator(queryset=Rack.objects.all(), fields=('site', 'facility_id'))
            validator.set_context(self)
            validator(data)

        # Enforce model validation
        super(WritableRackSerializer, self).validate(data)

        return data
Exemplo n.º 10
0
    def validate(self, data):

        # Validate uniqueness of (group, facility_id) since we omitted the automatically-created validator from Meta.
        if data.get('facility_id', None):
            validator = UniqueTogetherValidator(queryset=Rack.objects.all(), fields=('group', 'facility_id'))
            validator.set_context(self)
            validator(data)

        # Enforce model validation
        super(RackSerializer, self).validate(data)

        return data
Exemplo n.º 11
0
    def validate(self, data):

        # Validate uniqueness of (rack, position, face) since we omitted the automatically-created validator from Meta.
        if data.get('rack') and data.get('position') and data.get('face'):
            validator = UniqueTogetherValidator(queryset=Device.objects.all(), fields=('rack', 'position', 'face'))
            validator.set_context(self)
            validator(data)

        # Enforce model validation
        super(WritableDeviceSerializer, self).validate(data)

        return data
Exemplo n.º 12
0
    def validate(self, data):

        # Validate uniqueness of name and slug if a site has been assigned.
        if data.get('site', None):
            for field in ['name', 'slug']:
                validator = UniqueTogetherValidator(queryset=VLANGroup.objects.all(), fields=('site', field))
                validator.set_context(self)
                validator(data)

        # Enforce model validation
        super(VLANGroupSerializer, self).validate(data)

        return data
Exemplo n.º 13
0
    def validate(self, data):

        # Validate uniqueness of vid and name if a group has been assigned.
        if data.get('group', None):
            for field in ['vid', 'name']:
                validator = UniqueTogetherValidator(queryset=VLAN.objects.all(), fields=('group', field))
                validator.set_context(self)
                validator(data)

        # Enforce model validation
        super(WritableVLANSerializer, self).validate(data)

        return data
Exemplo n.º 14
0
    def validate(self, data):

        # Validate uniqueness of vid and name if a group has been assigned.
        if data.get('group', None):
            for field in ['vid', 'name']:
                validator = UniqueTogetherValidator(queryset=VLAN.objects.all(), fields=('group', field))
                validator.set_context(self)
                validator(data)

        # Enforce model validation
        super(WritableVLANSerializer, self).validate(data)

        return data
Exemplo n.º 15
0
    def validate(self, data):

        # Validate uniqueness of (virtual_chassis, position)
        validator = UniqueTogetherValidator(
            queryset=VCMembership.objects.all(),
            fields=('virtual_chassis', 'position'))
        validator.set_context(self)
        validator(data)

        # Enforce model validation
        super(WritableVCMembershipSerializer, self).validate(data)

        return data
Exemplo n.º 16
0
    def validate(self, data):

        # Validate uniqueness of name and slug if a site has been assigned.
        if data.get('site', None):
            for field in ['name', 'slug']:
                validator = UniqueTogetherValidator(queryset=VLANGroup.objects.all(), fields=('site', field))
                validator.set_context(self)
                validator(data)

        # Enforce model validation
        super(WritableVLANGroupSerializer, self).validate(data)

        return data
Exemplo n.º 17
0
    def validate(self, data):

        # Validate uniqueness of name if one has been provided.
        if data.get('name', None):
            validator = UniqueTogetherValidator(queryset=Secret.objects.all(),
                                                fields=('device', 'role',
                                                        'name'))
            validator.set_context(self)
            validator(data)

        # Enforce model validation
        super(WritableSecretSerializer, self).validate(data)

        return data
    def _validate_unique_together_instance(self, validated_data):
        for fields in self.unique_together_validators:
            unique_together_validator = UniqueTogetherValidator(self.Meta.model.objects.all(),
                                                                fields)
            call_args = [validated_data]
            if hasattr(unique_together_validator, "set_context"):
                unique_together_validator.set_context(self)
            else:
                call_args.append(self)

            try:
                unique_together_validator(*call_args)
            except ValidationError as exc:
                raise ValidationError({"non_field_errors": exc.detail})
Exemplo n.º 19
0
    def validate(self, data):

        # Encrypt plaintext data using the master key provided from the view context
        if data.get('plaintext'):
            s = Secret(plaintext=data['plaintext'])
            s.encrypt(self.context['master_key'])
            data['ciphertext'] = s.ciphertext
            data['hash'] = s.hash

        # Validate uniqueness of name if one has been provided.
        if data.get('name'):
            validator = UniqueTogetherValidator(queryset=Secret.objects.all(), fields=('device', 'role', 'name'))
            validator.set_context(self)
            validator(data)

        # Enforce model validation
        super(SecretSerializer, self).validate(data)

        return data
Exemplo n.º 20
0
    def validate(self, data):

        # Encrypt plaintext data using the master key provided from the view context
        if data.get('plaintext'):
            s = Secret(plaintext=data['plaintext'])
            s.encrypt(self.context['master_key'])
            data['ciphertext'] = s.ciphertext
            data['hash'] = s.hash

        # Validate uniqueness of name if one has been provided.
        if data.get('name'):
            validator = UniqueTogetherValidator(queryset=Secret.objects.all(), fields=('device', 'role', 'name'))
            validator.set_context(self)
            validator(data)

        # Enforce model validation
        super().validate(data)

        return data
Exemplo n.º 21
0
 def validate_unique_file(self, attrs):
     validator = UniqueTogetherValidator(
         queryset=self.Meta.model.objects.all(),
         fields=('supplier_id', 'date', 'currency'))
     validator.set_context(self)
     validator(attrs)