Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate(self, data):
if not data and self.required:
raise InvalidField('Widget must be selected')
if not data['id']:
raise InvalidField("Must specify a widget id")
try:
if data['id'] and data['data'] is not None:
errors = {}
widget = None
try:
widget = self.get_widget(data['id'])
except WidgetNotFound:
errors['self'] = 'Invalid Widget'
if widget:
for field in widget.fields:
if field.name in data['data'] and data['data'][field.name]:
try:
field.validate(data['data'][field.name])
def validate(self, data):
if type(data) is not bool:
raise InvalidField('%s must be boolean' % self.label)
def __init__(self, label, options=[], required=False):
self.options = options
self.valid_options = set(option[0] for option in self.options)
if required and not self.options:
raise InvalidField('Empty select fields cannot be required fields')
super(SelectField, self).__init__(label=label, many=False, required=required)
def __init__(self, label, many=False, required=False):
self.label = label
self.many = many
self.required = required
if self.many:
self.default = []
else:
self.default = None
self._creation_counter = Field._creation_counter
Field._creation_counter += 1
if not isinstance(self.label, str):
raise InvalidField('Label must be a string')
def validate(self, data):
if not data and self.required:
raise InvalidField('Widget must be selected')
if not data['id']:
raise InvalidField("Must specify a widget id")
try:
if data['id'] and data['data'] is not None:
errors = {}
widget = None
try:
widget = self.get_widget(data['id'])
except WidgetNotFound:
errors['self'] = 'Invalid Widget'
if widget:
for field in widget.fields:
def validate(self, data):
if not data or (isinstance(data, str) and not len(data)):
if self.required:
raise InvalidField('%s is required')
else:
return
try:
if isinstance(data, int):
value = data
else:
value = int(data, base=10)
except ValueError:
raise InvalidField('%s must be integer' % self.label)
if self.min_value is not None and value < self.min_value:
raise InvalidField('%s must be greater than %d' % (self.label, self.min_value))
if self.max_value is not None and value > self.max_value:
raise InvalidField('%s must be less than than %d' % (self.label, self.max_value))
def validate(self, data):
if self.many:
if not isinstance(data, list) or not all([is_valid_id(id) for id in data]):
raise InvalidField('Data must be list of integers or UUIDs')
else:
if not is_valid_id(data):
raise InvalidField('Data must be an integer or UUID')
if widget:
for field in widget.fields:
if field.name in data['data'] and data['data'][field.name]:
try:
field.validate(data['data'][field.name])
except InvalidField as e:
errors[field.name] = str(e)
else:
if field.required:
errors[field.name] = 'This field is required'
if len(errors):
raise InvalidField(json.dumps(errors))
else:
raise InvalidField('Invalid Widget Data')
except InvalidField as e:
raise e
except Exception as e:
raise InvalidField('Problem with widget data %s' % str(e))
if data.get('widget') is not None:
try:
widget = ThemeManager.Widgets.get(data['widget'])
except WidgetNotFound as e:
errors['widget'] = str(e)
else:
for field in widget.fields:
field_data = data['data'].get(field.name)
if field_data is not None:
try:
field.validate(field_data)
except InvalidField as e:
errors[field.name] = str(e)
elif field.required:
errors[field.name] = '%s is required' % field.label
if errors:
raise ValidationError(errors)
return data
def __init__(self, label, many=False, min_value=None, max_value=None, required=False):
self.min_value = min_value
self.max_value = max_value
if min_value is not None and max_value is not None:
if min_value > max_value:
raise InvalidField('IntegerField: min_value must be less than max_value')
super(IntegerField, self).__init__(label=label, many=many, required=required)