def _to_python(self, value, state): if not value: return None if '\n' in value: raise Invalid(self.message('newline', state), value, state) elements = value.split(None, 2) if len(elements) != 3: raise Invalid(self.message('invalid', state), value, state) return elements
def validate_python(self, value, state): try: collection = Collection.by_simple_name(value) except InvalidRequestError: raise Invalid(self.message('no_collection', state, collection=value), value, state) if not self.eol and (collection.statuscode == STATUS['EOL']): raise Invalid(self.message('eol_collection', state, collection=value), value, state) return collection
def validate_python(self, field_dict, state): hidden = str(field_dict['captchahidden']) input_val = str(field_dict['captchainput']) try: payload = captcha_controller.model_from_payload(hidden) except: raise Invalid(self.message('incorrect', state), field_dict, state) if payload.plaintext != input_val: raise Invalid(self.message('incorrect', state), field_dict, state) elapsed = datetime.utcnow() - payload.created if elapsed.seconds > self.timeout * 60: raise Invalid(self.message('timeout', state), field_dict, state)
def validate_python(self, form_fields, state): id = form_fields.get('id') tag = form_fields['tag'] existing = RetentionTag.query.filter_by(tag=tag).first() if existing and (not id or existing.id != id): error = self.message('not_unique', state) raise Invalid(error, form_fields, state, error_dict={'tag': error})
def validate_python(self, value, state): '''Make sure the collection is in the database.''' #pylint:disable-msg=E1101 try: Collection.query.filter_by(name=value).first() except InvalidRequestError: raise Invalid(self.message('no_collection', state, collection=value), value, state)
def validate_python(self, value, state): if self.element_validator: try: value = map(self.element_validator.to_python, value) except Invalid: raise except: # Just in case the element validator doesn't throw an Invalid # exception raise Invalid(self.message('incorrect_value', state), value, state)
def validate_python(self, form_fields, state): user_id = form_fields['user_id'] user_name = form_fields['user_name'] existing_user = User.by_user_name(user_name) try: if not user_id: # New user if existing_user: # with a duplicate name raise ValueError else: if existing_user: current_user = User.by_id(user_id) if current_user != existing_user: raise ValueError except ValueError: error = {'user_name': self.message('not_unique', state)} raise Invalid('Login name is not unique', form_fields, state, error_dict=error)
def validate_python(self, form_fields, state): lc_id = form_fields.get('id', None) fqdn = form_fields['fqdn'] lusername = form_fields['lusername'] try: existing_lc_with_fqdn = LabController.by_name(fqdn) except NoResultFound: existing_lc_with_fqdn = None existing_user_with_lusername = User.by_user_name(lusername) if not lc_id: labcontroller = None luser = None else: labcontroller = LabController.by_id(lc_id) luser = labcontroller.user errors = {} if not labcontroller and existing_lc_with_fqdn: # New LC using duplicate FQDN errors['fqdn'] = self.message('fqdn_not_unique', state) elif (labcontroller and existing_lc_with_fqdn and labcontroller != existing_lc_with_fqdn): # Existing LC changing FQDN to a duplicate one errors['fqdn'] = self.message('fqdn_not_unique', state) if (not luser and existing_user_with_lusername and existing_user_with_lusername.lab_controller): # New LC using username that is already in use errors['lusername'] = self.message('username_in_use', state) if (luser and existing_user_with_lusername and luser != existing_user_with_lusername and existing_user_with_lusername.lab_controller): # Existing LC changing username to one that is already in use errors['lusername'] = self.message('username_in_use', state) if errors: raise Invalid('Validation failed', form_fields, state, error_dict=errors)
def validate_python(self, field_dict, state): '''Make sure the Collection with the given `name` and `version` exists. We want to allow for: 1) Neither to be set 2) Name to exist in the db and version unset 3) Name and version to exist in the db ''' if not field_dict: # It's okay for both to be none return errors = {} name = field_dict.get('name') version = field_dict.get('version') if (not name) and version: #pylint:disable-msg=E1101 errors['version'] = self.message('nameless_version', state) elif name and version: #pylint:disable-msg=E1101 try: Collection.query.filter_by(name=name, version=version).one() except InvalidRequestError: errors['version'] = self.message('no_version', state, name=name, version=version) elif name and not version: #pylint:disable-msg=E1101 try: Collection.query.filter_by(name=name).first() except InvalidRequestError: errors['name'] = self.message('no_collection', state, name=name) if errors: error_list = sorted(errors.iteritems()) error_message = '\n'.join([u'%s: %s' % (error, msg) for error, msg in error_list]) raise Invalid(error_message, field_dict, state, error_dict=errors)
def _to_python(self, value, state): try: system = System.by_fqdn(value, identity.current.user) except NoResultFound: raise Invalid('Invalid system', value, state) return system
def _to_python(self, value, state): try: recipe = Recipe.by_id(value) except NoResultFound: raise Invalid('Invalid recipe', value, state) return recipe
def _to_python(self, value, state): try: cracklib.VeryFascistCheck(value) return value except ValueError, msg: raise Invalid('Invalid password: %s' % str(msg), value, state)
def _to_python(self, value, state): try: system = System.by_fqdn(value, identity.current.user) except DatabaseLookupError: raise Invalid('Invalid system', value, state) return system
def validate_python(self, value, state): if not self.regex.match(value): raise Invalid(self.message('no_collection', state, collection=value), value, state)