def validate(self, value, model_instance): super(CredentialTypeInjectorField, self).validate(value, model_instance) # make sure the inputs are valid first try: CredentialTypeInputField().validate(model_instance.inputs, model_instance) except django_exceptions.ValidationError: # If `model_instance.inputs` itself is invalid, we can't make an # estimation as to whether our Jinja templates contain valid field # names; don't continue return # In addition to basic schema validation, search the injector fields # for template variables and make sure they match the fields defined in # the inputs valid_namespace = dict((field, 'EXAMPLE') for field in model_instance.defined_fields) class ExplodingNamespace: def __str__(self): raise UndefinedError(_('Must define unnamed file injector in order to reference `tower.filename`.')) class TowerNamespace: def __init__(self): self.filename = ExplodingNamespace() def __str__(self): raise UndefinedError(_('Cannot directly reference reserved `tower` namespace container.')) valid_namespace['tower'] = TowerNamespace() # ensure either single file or multi-file syntax is used (but not both) template_names = [x for x in value.get('file', {}).keys() if x.startswith('template')] if 'template' in template_names: valid_namespace['tower'].filename = 'EXAMPLE_FILENAME' if len(template_names) > 1: raise django_exceptions.ValidationError( _('Must use multi-file syntax when injecting multiple files'), code='invalid', params={'value': value}, ) elif template_names: for template_name in template_names: template_name = template_name.split('.')[1] setattr(valid_namespace['tower'].filename, template_name, 'EXAMPLE_FILENAME') for type_, injector in value.items(): if type_ == 'env': for key in injector.keys(): self.validate_env_var_allowed(key) for key, tmpl in injector.items(): try: sandbox.ImmutableSandboxedEnvironment(undefined=StrictUndefined).from_string(tmpl).render(valid_namespace) except UndefinedError as e: raise django_exceptions.ValidationError( _('{sub_key} uses an undefined field ({error_msg})').format(sub_key=key, error_msg=e), code='invalid', params={'value': value}, ) except SecurityError as e: raise django_exceptions.ValidationError(_('Encountered unsafe code execution: {}').format(e)) except TemplateSyntaxError as e: raise django_exceptions.ValidationError( _('Syntax error rendering template for {sub_key} inside of {type} ({error_msg})').format(sub_key=key, type=type_, error_msg=e), code='invalid', params={'value': value}, )
def clean(self): if len(self.name) >= 40: raise exceptions.ValidationError('Too many characters ...') return self.name
def clean_fixed_price(self): if self.range: raise exceptions.ValidationError( _("No range should be selected as the condition range will " "be used instead."))
def clean_withdrawal_date(self): if self.withdrawal_date and self.withdrawal_date <= self.entry_date: raise exceptions.ValidationError( 'Withdrawal date can not be less or equal than entry date.' )
def clean_entry_date(self): if self.withdrawal_date and self.entry_date >= self.withdrawal_date: raise exceptions.ValidationError( 'Entry date can not be greater or equal than withdrawal date.' )
# This file is the part of walletdjango, released under modified MIT license # See the file LICENSE included in this distribution import datetime import decimal from django.core import exceptions from django.db import models from walletdjango import settings decimal.getcontext().prec = settings.WALLET_MAX_DIGITS + 2 decimal.getcontext().rounding = decimal.ROUND_05UP insufficient_funds = exceptions.ValidationError( message='Operation with transaction rejected: ' 'cannot decrease wallet balance below zero', code='insufficient_funds') def new_wallet_name(): """Generate name of a wallet using timestamp with microsecond precision :return: string with generated name """ now = datetime.datetime.now() ts = str(int(now.timestamp() * 100000)) return f'wallet-{ts}' class UserWallet(models.Model): """Represents user's wallet
def validate(self, value, model_instance): arr_choices = self.get_choices_selected(self.get_choices_default()) for opt_select in value: if (opt_select not in arr_choices): raise exceptions.ValidationError( \ self.error_messages['invalid_choice'] % value)
def clean(self, value, model_instance): value = super(PercentField, self).clean(value, model_instance) if value and (value < 0.0 or value > 1.0): raise exceptions.ValidationError( _("The number must be between 0.0 and 1.0")) return value
def validate(self, value, model_instance): arr_choices = [v for v, s in self.get_choices_default()] for opt in value: if opt not in arr_choices: raise exceptions.ValidationError("%s is not a valid choice" % opt) return
def update_account(user, **kwargs): """ Update an existing user within the HydroShare system. The user calling this method must have write access to the account details. REST URL: PUT /accounts/{userID} Parameters: userID - ID of the existing user to be modified user - An object containing the modified attributes of the user to be modified Returns: The userID of the user that was modified Return Type: userID Raises: Exceptions.NotAuthorized - The user is not authorized Exceptions.NotFound - The user identified by userID does not exist Exceptions.InvalidContent - The content of the user object is invalid Exception.ServiceFailure - The service is unable to process the request Note: This would be done via a JSON object (user) that is in the PUT request. """ from django.contrib.auth.models import Group groups = kwargs.get('groups', []) if groups: if len(groups) == 1: groups = [(Group.objects.get_or_create( name=groups) if isinstance(groups, basestring) else groups)[0]] else: groups = zip(*(Group.objects.get_or_create( name=g) if isinstance(g, basestring) else g for g in groups))[0] if 'password' in kwargs: user.set_password(kwargs['password']) blacklist = {'username', 'password', 'groups'} # handled separately or cannot change for k in blacklist.intersection(kwargs.keys()): del kwargs[k] try: profile = get_profile(user) profile_update = dict() update_keys = filter(lambda x: hasattr(profile, str(x)), kwargs.keys()) for key in update_keys: profile_update[key] = kwargs[key] for k, v in profile_update.items(): if k == 'picture': profile.picture = File(v) if not isinstance( v, UploadedFile) else v elif k == 'cv': profile.cv = File(v) if not isinstance(v, UploadedFile) else v else: setattr(profile, k, v) profile.save() except AttributeError as e: raise exceptions.ValidationError( e.message ) # ignore deprecated user profile module when we upgrade to 1.7 user_update = dict() update_keys = filter(lambda x: hasattr(user, str(x)), kwargs.keys()) for key in update_keys: user_update[key] = kwargs[key] for k, v in user_update.items(): setattr(user, k, v) user.save() user.groups = groups return user.username
def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs): if not app_config.models_module: return try: Permission = apps.get_model('auth', 'Permission') except LookupError: return if not router.allow_migrate_model(using, Permission): return from django.contrib.contenttypes.models import ContentType permission_name_max_length = Permission._meta.get_field('name').max_length verbose_name_max_length = permission_name_max_length - 11 # len('Can change ') prefix # This will hold the permissions we're looking for as # (content_type, (codename, name)) searched_perms = list() # The codenames and ctypes that should exist. ctypes = set() for klass in app_config.get_models(): # Force looking up the content types in the current database # before creating foreign keys to them. ctype = ContentType.objects.db_manager(using).get_for_model(klass) if len(klass._meta.verbose_name) > verbose_name_max_length: raise exceptions.ValidationError( "The verbose_name of %s.%s is longer than %s characters" % ( ctype.app_label, ctype.model, verbose_name_max_length, )) ctypes.add(ctype) for perm in _get_all_permissions(klass._meta, ctype): searched_perms.append((ctype, perm)) # Find all the Permissions that have a content_type for a model we're # looking for. We don't need to check for codenames since we already have # a list of the ones we're going to create. all_perms = set( Permission.objects.using(using).filter( content_type__in=ctypes, ).values_list("content_type", "codename")) perms = [ Permission(codename=codename, name=name, content_type=ct) for ct, (codename, name) in searched_perms if (ct.pk, codename) not in all_perms ] # Validate the permissions before bulk_creation to avoid cryptic database # error when the name is longer than 255 characters for perm in perms: if len(perm.name) > permission_name_max_length: raise exceptions.ValidationError( "The permission name %s of %s.%s is longer than %s characters" % ( perm.name, perm.content_type.app_label, perm.content_type.model, permission_name_max_length, )) Permission.objects.using(using).bulk_create(perms) if verbosity >= 2: for perm in perms: print("Adding permission '%s'" % perm)
def validate(self, value, model_instance): arr_choices = self.get_choices_selected(self.get_choices_default()) for opt_select in value: if (opt_select not in arr_choices): # the int() here is for comparing with integer choices raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value) return
def validate_cidr(value): try: ipaddress.ip_network(value) except ValueError: raise exceptions.ValidationError( _('Enter a valid IP4 or IP6 network.'), code='invalid')
def validate(self, value, model_instance): """ Validate each value in values and raise a ValidationError if something is wrong. """ if not self.editable: # Skip validation for non-editable fields. return if value: try: size = len(value) except TypeError: size = 0 if size != 3: raise exceptions.ValidationError( _("An effort should be a triplet: number, time unit and reference unit." ), code="invalid_format", ) duration, unit, reference = value if duration: # Check that the duration is an integer try: duration = int(duration) except ValueError as error: raise exceptions.ValidationError( _("An effort should be a round number of time units."), code="invalid_effort", ) from error # Check that the duration is positive if duration <= 0: raise exceptions.ValidationError( _("An effort should be positive."), code="negative_effort") choices = list(dict(self.time_units)) # Search the time unit in this set if unit not in choices: raise exceptions.ValidationError(self.error_message, code="invalid_interface", params={"value": unit}) # Search the reference time unit in this set if reference not in choices: raise exceptions.ValidationError( self.error_message, code="invalid_reference", params={"value": reference}, ) if choices.index(unit) >= choices.index(reference): raise exceptions.ValidationError( _("The effort time unit should be shorter than the reference unit." ), code="unit_order", ) if value is None and not self.null: raise exceptions.ValidationError(self.error_messages["null"], code="null") if not self.blank and not value: raise exceptions.ValidationError(self.error_messages["blank"], code="blank")
def validate(self, value, model_instance): # decrypt secret values so we can validate their contents (i.e., # ssh_key_data format) if not isinstance(value, dict): return super(CredentialInputField, self).validate(value, model_instance) # Backwards compatability: in prior versions, if you submit `null` for # a credential field value, it just considers the value an empty string for unset in [ key for key, v in model_instance.inputs.items() if not v ]: default_value = model_instance.credential_type.default_for_field( unset) if default_value is not None: model_instance.inputs[unset] = default_value decrypted_values = {} for k, v in value.items(): if all([ k in model_instance.credential_type.secret_fields, v != '$encrypted$', model_instance.pk ]): decrypted_values[k] = utils.decrypt_field(model_instance, k) else: decrypted_values[k] = v super(JSONSchemaField, self).validate(decrypted_values, model_instance) errors = {} for error in Draft4Validator( self.schema(model_instance), format_checker=self.format_checker).iter_errors( decrypted_values): if error.validator == 'pattern' and 'error' in error.schema: error.message = error.schema['error'] % error.instance if error.validator == 'dependencies': # replace the default error messaging w/ a better i18n string # I wish there was a better way to determine the parameters of # this validation failure, but the exception jsonschema raises # doesn't include them as attributes (just a hard-coded error # string) match = re.search( # 'foo' is a dependency of 'bar' "'" # apostrophe "([^']+)" # one or more non-apostrophes (first group) "'[\w ]+'" # one or more words/spaces "([^']+)", # second group error.message, ) if match: label, extraneous = match.groups() if error.schema['properties'].get(label): label = error.schema['properties'][label]['label'] errors[extraneous] = [ _('cannot be set unless "%s" is set') % label ] continue if 'id' not in error.schema: # If the error is not for a specific field, it's specific to # `inputs` in general raise django_exceptions.ValidationError( error.message, code='invalid', params={'value': value}, ) errors[error.schema['id']] = [error.message] inputs = model_instance.credential_type.inputs for field in inputs.get('required', []): if not value.get(field, None): errors[field] = [ _('required for %s') % (model_instance.credential_type.name) ] # `ssh_key_unlock` requirements are very specific and can't be # represented without complicated JSON schema if (model_instance.credential_type.managed_by_tower is True and 'ssh_key_unlock' in model_instance.credential_type.defined_fields): # in order to properly test the necessity of `ssh_key_unlock`, we # need to know the real value of `ssh_key_data`; for a payload like: # { # 'ssh_key_data': '$encrypted$', # 'ssh_key_unlock': 'do-you-need-me?', # } # ...we have to fetch the actual key value from the database if model_instance.pk and model_instance.ssh_key_data == '$encrypted$': model_instance.ssh_key_data = model_instance.__class__.objects.get( pk=model_instance.pk).ssh_key_data if model_instance.has_encrypted_ssh_key_data and not value.get( 'ssh_key_unlock'): errors['ssh_key_unlock'] = [ _('must be set when SSH key is encrypted.') ] if all([ model_instance.ssh_key_data, value.get('ssh_key_unlock'), not model_instance.has_encrypted_ssh_key_data ]): errors['ssh_key_unlock'] = [ _('should not be set when SSH key is not encrypted.') ] if errors: raise serializers.ValidationError({'inputs': errors})
def clean(self): """Validates the model.""" super(FlyZone, self).clean() if self.altitude_msl_min > self.altitude_msl_max: raise exceptions.ValidationError( 'Altitude min must be less than altitude max.')
def clean(self): cleaned_data = super(publisher_form, self).clean() cleaned_data['person_name'] = self.clean_person_name( cleaned_data['person_name']) cleaned_data['candidate'] = cleaned_data['candidate'].replace( "%20", ' ') # replace url entities cleaned_data['candidate'] = cleaned_data['candidate'].replace( "%25", ' ') # replace url entities cleaned_data['candidate'] = cleaned_data['candidate'].replace( " ", ' ') # double space to single space if cleaned_data[ 'website'] != '': # when empty, it's an empty string - we want it to be empty - it's a honeypot raise exceptions.ValidationError( 'Thanks for filling in the website field!') if not 'candidate_id' in self.cleaned_data: candidate_id = None else: candidate_id = self.cleaned_data['candidate_id'] cleaned_data['user_object'] = utils.find_or_make_user( cleaned_data['person_name'], self.request_ip, cleaned_data['state']) if candidate_id: # if they provided a candidate_id try: cleaned_data['candidate'] = candidate.objects.get( pk=candidate_id) except candidate.DoesNotExist: try: cleaned_data['candidate'] = utils.find_or_make_candidate( cleaned_data['candidate'], cleaned_data['user_object'], exact=True) except: raise exceptions.ValidationError( 'Candidate ID was set, but does not correspond to an actual candidate' ) else: # ok, now make it do the magic of finding a candidate by name if cleaned_data['candidate']: try: # TODO: This should be a single candidate and in some cases, it's returning more than one cleaned_data['candidate'] = utils.find_or_make_candidate( cleaned_data['candidate'], cleaned_data['user_object'], exact=True) except: raise exceptions.ValidationError( "Problem looking up candidate. We hope this is temporary. Would you care to try again?" ) else: raise exceptions.ValidationError( "No candidate provided. Who are you planning on voting for?" ) if utils.isiterable(cleaned_data['candidate']): # if it would result in multiple candidates, just take the first cleaned_data['candidate'] = cleaned_data['candidate'][0] return cleaned_data
class MultiTextField(models.Field): """ TextField which allows storage of several Strings in one field. NOTE: due to the Base64-encoded String that is used to store the value(s) of a MultiTextField, it is NOT POSSIBLE to access value(s) from instances of this field type in QuerySet operations! If you need to work on MultiTextField instances for filtering, etc. you have to retrieve the "real" object instances and check the respective fields of type MultiTextField using "obj.attr" field access. Django will auto-magically convert the raw database representation of the MultiTextField value(s) to a Python list of Strings during runtime. """ __metaclass__ = models.SubfieldBase default_error_messages = { # pylint: disable-msg=E1102 'too_long': _(u'A single field must be at most {0} characters long ' u'(was {1}).'), } def __init__(self, *args, **kwargs): """ Initialises this MultiTextField instance. """ # If the given kwargs contain widget as key, remove it! if 'widget' in kwargs: self.widget = kwargs.pop('widget') # Check if label is specified in kwargs. If so, bind it. self.label = None if 'label' in kwargs: self.label = kwargs.pop('label') super(MultiTextField, self).__init__(*args, **kwargs) def validate(self, value, model_instance): """ Validates value and throws `ValidationError`. """ super(MultiTextField, self).validate(value, model_instance) if self.max_length and len(value) > self.max_length: raise exceptions.ValidationError( self.error_messages['too_long'].format(self.max_length, len(value))) def clean(self, value, model_instance): """ Convert to the value's type and run validation. Validation errors from to_python and validate are propagated. The correct value is returned iff no error is raised. """ values = self.to_python(value) validation_errors = {} if not len(values) and not self.blank: raise exceptions.ValidationError(self.error_messages['blank']) for value in values: try: self.validate(value, model_instance) self.run_validators(value) except exceptions.ValidationError, exc: validation_errors[value] = exc.messages[0] if len(validation_errors.keys()) > 0: self.widget.errors.clear() self.widget.errors.update(validation_errors) raise exceptions.ValidationError(u'Some fields did not validate.') self.widget.errors = {} return values
def to_python(self, value): if value in (True, False): return value if value in ('t', 'True', '1'): return True if value in ('f', 'False', '0'): return False raise exceptions.ValidationError( _("This value must be either True or False."))
def to_python(self, value): if not (value is None or isinstance(value, (basestring, int, long))): raise exceptions.ValidationError(self.error_messages['invalid']) return value
class Field(object): """Base class for all field types""" # Designates whether empty strings fundamentally are allowed at the # database level. empty_strings_allowed = True # These track each time a Field instance is created. Used to retain order. # The auto_creation_counter is used for fields that Django implicitly # creates, creation_counter is used for all user-specified fields. creation_counter = 0 auto_creation_counter = -1 default_validators = [] # Default set of validators default_error_messages = { 'invalid_choice': _(u'Value %r is not a valid choice.'), 'null': _(u'This field cannot be null.'), 'blank': _(u'This field cannot be blank.'), 'unique': _(u'%(model_name)s with this %(field_label)s ' u'already exists.'), } # Generic field type description, usually overriden by subclasses def _description(self): return _(u'Field of type: %(field_type)s') % { 'field_type': self.__class__.__name__ } description = property(_description) def __init__(self, verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=NOT_PROVIDED, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text='', db_column=None, db_tablespace=None, auto_created=False, validators=[], error_messages=None): self.name = name self.verbose_name = verbose_name self.primary_key = primary_key self.max_length, self._unique = max_length, unique self.blank, self.null = blank, null # Oracle treats the empty string ('') as null, so coerce the null # option whenever '' is a possible value. if (self.empty_strings_allowed and connection.features.interprets_empty_strings_as_nulls): self.null = True self.rel = rel self.default = default self.editable = editable self.serialize = serialize self.unique_for_date, self.unique_for_month = (unique_for_date, unique_for_month) self.unique_for_year = unique_for_year self._choices = choices or [] self.help_text = help_text self.db_column = db_column self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE self.auto_created = auto_created # Set db_index to True if the field has a relationship and doesn't # explicitly set db_index. self.db_index = db_index # Adjust the appropriate creation counter, and save our local copy. if auto_created: self.creation_counter = Field.auto_creation_counter Field.auto_creation_counter -= 1 else: self.creation_counter = Field.creation_counter Field.creation_counter += 1 self.validators = self.default_validators + validators messages = {} for c in reversed(self.__class__.__mro__): messages.update(getattr(c, 'default_error_messages', {})) messages.update(error_messages or {}) self.error_messages = messages def __cmp__(self, other): # This is needed because bisect does not take a comparison function. return cmp(self.creation_counter, other.creation_counter) def __deepcopy__(self, memodict): # We don't have to deepcopy very much here, since most things are not # intended to be altered after initial creation. obj = copy.copy(self) if self.rel: obj.rel = copy.copy(self.rel) memodict[id(self)] = obj return obj def to_python(self, value): """ Converts the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can't be converted. Returns the converted value. Subclasses should override this. """ return value def run_validators(self, value): if value in validators.EMPTY_VALUES: return errors = [] for v in self.validators: try: v(value) except exceptions.ValidationError, e: if hasattr(e, 'code') and e.code in self.error_messages: message = self.error_messages[e.code] if e.params: message = message % e.params errors.append(message) else: errors.extend(e.messages) if errors: raise exceptions.ValidationError(errors)
def save(self, bundle, skip_errors=False): try: return super(MongoEngineResource, self).save(bundle, skip_errors) except mongoengine.ValidationError as ex: raise exceptions.ValidationError(ex.message)
def clean_quantity(self): if self.quantity > self.silo.size: raise exceptions.ValidationError( 'This quantity does not fit in the silo.' )
def create_proxy_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs): if not app_config.models_module: return logger = logging.getLogger(__name__) # print(app_config) try: logger.info("Tentando obter modelo de permissão do app.") Permission = apps.get_model('auth', 'Permission') except LookupError as e: logger.error(str(e)) return if not router.allow_migrate_model(using, Permission): return from django.contrib.contenttypes.models import ContentType permission_name_max_length = Permission._meta.get_field('name').max_length # This will hold the permissions we're looking for as # (content_type, (codename, name)) searched_perms = list() # The codenames and ctypes that should exist. ctypes = set() for klass in list(app_config.get_models()): opts = klass._meta permissions = ( ("list_" + opts.model_name, string_concat(_('Visualizaçao da lista de'), ' ', opts.verbose_name_plural)), ("detail_" + opts.model_name, string_concat(_('Visualização dos detalhes de'), ' ', opts.verbose_name_plural)), ) opts.permissions = tuple( set(list(permissions) + list(opts.permissions))) if opts.proxy: # Force looking up the content types in the current database # before creating foreign keys to them. app_label, model = opts.app_label, opts.model_name try: logger.info("Tentando obter db_manager.") ctype = ContentType.objects.db_manager( using).get_by_natural_key(app_label, model) except Exception as e: logger.error(str(e)) ctype = ContentType.objects.db_manager(using).create( app_label=app_label, model=model) else: ctype = ContentType.objects.db_manager(using).get_for_model(klass) ctypes.add(ctype) # FIXME: Retirar try except quando sapl passar a usar django 1.11 try: logger.info("_get_all_permissions") # Função não existe mais em Django 1.11 # como sapl ainda não foi para Django 1.11 # esta excessão foi adicionada para caso o # Sapl esteja rodando em um projeto 1.11 não ocorra erros _all_perms_of_klass = _get_all_permissions(klass._meta, ctype) except Exception as e: logger.error(str(e)) # Nova função usada em projetos com Django 1.11 e o sapl é uma app _all_perms_of_klass = _get_all_permissions(klass._meta) for perm in _all_perms_of_klass: searched_perms.append((ctype, perm)) # Find all the Permissions that have a content_type for a model we're # looking for. We don't need to check for codenames since we already have # a list of the ones we're going to create. all_perms = set( Permission.objects.using(using).filter( content_type__in=ctypes, ).values_list("content_type", "codename")) perms = [ Permission(codename=codename, name=name, content_type=ct) for ct, (codename, name) in searched_perms if (ct.pk, codename) not in all_perms ] # Validate the permissions before bulk_creation to avoid cryptic database # error when the name is longer than 255 characters for perm in perms: if len(perm.name) > permission_name_max_length: logger.error("The permission name %s of %s.%s " "is longer than %s characters" % ( perm.name, perm.content_type.app_label, perm.content_type.model, permission_name_max_length, )) raise exceptions.ValidationError('The permission name %s of %s.%s ' 'is longer than %s characters' % ( perm.name, perm.content_type.app_label, perm.content_type.model, permission_name_max_length, )) Permission.objects.using(using).bulk_create(perms) if verbosity >= 2: for perm in perms: print("Adding permission '%s'" % perm)
def clean(self): if self.max_num_matches < self.min_num_matches: raise exceptions.ValidationError( {'max_num_matches': ( 'Maximum number of matches must be greater than or ' 'equal to minimum number of matches')})
def validate(self, value, model_instance): super(CredentialTypeInjectorField, self).validate(value, model_instance) # make sure the inputs are valid first try: CredentialTypeInputField().validate(model_instance.inputs, model_instance) except django_exceptions.ValidationError: # If `model_instance.inputs` itself is invalid, we can't make an # estimation as to whether our Jinja templates contain valid field # names; don't continue return # In addition to basic schema validation, search the injector fields # for template variables and make sure they match the fields defined in # the inputs valid_namespace = dict( (field, 'EXAMPLE') for field in model_instance.defined_fields) class TowerNamespace: filename = None valid_namespace['tower'] = TowerNamespace() # ensure either single file or multi-file syntax is used (but not both) template_names = [ x for x in value.get('file', {}).keys() if x.startswith('template') ] if 'template' in template_names and len(template_names) > 1: raise django_exceptions.ValidationError( _('Must use multi-file syntax when injecting multiple files'), code='invalid', params={'value': value}, ) if 'template' not in template_names: valid_namespace['tower'].filename = TowerNamespace() for template_name in template_names: template_name = template_name.split('.')[1] setattr(valid_namespace['tower'].filename, template_name, 'EXAMPLE') for type_, injector in value.items(): for key, tmpl in injector.items(): try: Environment(undefined=StrictUndefined).from_string( tmpl).render(valid_namespace) except UndefinedError as e: raise django_exceptions.ValidationError( _('{sub_key} uses an undefined field ({error_msg})'). format(sub_key=key, error_msg=e), code='invalid', params={'value': value}, ) except TemplateSyntaxError as e: raise django_exceptions.ValidationError( _('Syntax error rendering template for {sub_key} inside of {type} ({error_msg})' ).format(sub_key=key, type=type_, error_msg=e), code='invalid', params={'value': value}, )
def clean(self): if (self.start_datetime and self.end_datetime and self.start_datetime > self.end_datetime): raise exceptions.ValidationError( _('End date should be later than start date'))
def clean(self, value, model_instance): if value is None and not self.allow_blank: raise exceptions.ValidationError(field_required_msg) return super().clean(value, model_instance)
def is_valid_email(value): if not EMAIL_RE.search(value): raise exceptions.ValidationError(_('Enter a valid e-mail address.'))
def _clean(self): if not self.range: raise exceptions.ValidationError( _("Fixed price benefits require a product range."))