def validate(form, field): if field.data == '': return if min is not None and len(field.data) < min: raise ValidationError("Minimum length is " + str(min)) if max is not None and len(field.data) > max: raise ValidationError("Maximum length is " + str(min))
def validate_password(form, field): user = UserModel().get_by_username(form.username.data) if user is None: raise ValidationError("auth error") entry_password = get_password_hash(settings.SECRET_KEY, user.username, field.data) if entry_password != user.password: raise ValidationError("auth error") form.user = user
def validate(form, field): try: datetime.strptime(field.data, format) except ValueError, e: if format in str(e): raise ValidationError(message) else: raise ValidationError(str(e))
def __call__(self, form, field): if not isinstance(field.data, Decimal): try: dec_data = Decimal(field.data) field.data = dec_data except InvalidOperation: raise ValidationError(self.message) if field.data.as_tuple.exponent > self.places: raise ValidationError(self.message) elif field.data.as_tuple.digits[0] > size: raise ValidationError(self.message)
def validate_parents(form, field): if len(field.data) > 3: raise ValidationError("You must reply to three or less posts.") if len(field.data) < 1: raise ValidationError("You must reply to at least one post.") parents = set(graph.posts.get(parent) for parent in field.data) for parent in parents: ancestor = parent.has_ancestor_any(parents - set((parent, ))) if ancestor: raise ValidationError("%s is an ancestor of %s." % (ancestor, parent))
def validate_enrollment_subjects(form, field): subjects = [] for key, value in form.data.items(): if key.startswith('subject_'): if value == True: subjects.append(key.split('_')[1]) if not 'de' in subjects: raise ValidationError(u'Eine Prüfung in Deutsch ist Pflicht.') if len(subjects) > 3: raise ValidationError( u'Sie können sich in maximal drei Sprachen (inkl. Deutsch) prüfen lassen.' ) if len(subjects) < 2: raise ValidationError( u'Sie müssen in mindestens einer Fremdsprache den Eignungsprüfungstest ablegen.' )
def valid_mailing_list_name(form, mailing_list): if not mailing_list.data: return if not mailing_list.data.startswith(form.project_name.data + '-'): raise ValidationError("Mailing lists must start with the project " "name and a dash, e.g. '%s-users'" % (form.project_name.data))
def validate_on_submit(self, *args, **kwargs): if self.is_submitted(): referrer = request.environ.get('HTTP_REFERER', '') if not referrer.startswith(request.host_url): raise ValidationError('Invalid referrer') return super(ReferrerCheckForm, self).validate_on_submit(*args, **kwargs)
def validate(form, field): if field.data == '': return if '@' not in field.data: raise ValidationError("Invalid e-mail address.") v = validators.Email() v(form, field)
def __call__(self, form, field): check = self.session.query( self.model).filter(self.field == field.data).first() if 'id' in form: id = form.id.data else: id = None if check and (id is None or id != check.id): raise ValidationError(self.message)
def phone_is_unique(form, field): try: user = cdw.users.with_fields(origin="web", phoneNumber=field.data).first() except: return if user != None: raise ValidationError( 'Phone number is associated with an account already')
def validate(form, field): if field.data == '': return ret = FascistCheck(field.data, form.username.data) if ret is not None: error = "Password rejected: %s." % ret raise ValidationError(error) else: return
def tags_check(form, field): if not field.data: return True if len(field.data.split(' ')) > 3: raise ValidationError(u'标签不能超过3个.')
def check_if_username_exists(form, field): try: cdw.users.with_username(field.data) except: return raise ValidationError('Username %s exists' % field.data)
def email_is_unique(form, field): try: cdw.users.with_email(field.data) except: return raise ValidationError('Email is already associated with an account')
def check_if_category_exists(form, field): try: cdw.categories.with_id(field.data) except: raise ValidationError('Invalid category ID')
def check_id(uid): try: cdw.users.with_id(uid) except: raise ValidationError('Invalid user ID')
def check_if_post_exists(form, field): try: cdw.posts.with_id(field.data) except: raise ValidationError("Invalid post ID")
def check_if_thread_exists(form, field): try: cdw.threads.with_id(field.data) except: raise ValidationError("Invalid thread ID")
def does_not_have_bad_words(form, field): if has_bad_words(field.data): raise ValidationError('Content contains bad words')
def email_unique(form, field): if len(db_session.query(User).filter_by(email=field.data).all()) > 0: raise ValidationError(u'这个邮件地址已经有人注册了.')
def __call__(self, form, field): if any(c in field.data for c in self.not_valid_chars): raise ValidationError(self.message)
def unique_user(form, field): """ Check if user's pseudo is not already used. """ if count_user_by_pseudo(field.data) > 0: raise ValidationError("Pseudo unavailable.")
def __call__(self, form, field): if not self.model.query.filter(self.field == field.data).first(): raise ValidationError(self.message)
def __call__(self, form, field): user = User.query.filter(User.email == form.email.data).first() if not user: return if not user.check_password(field.data): raise ValidationError(self.message)
def validate_phonenumber(form, field): try: normalize_phonenumber(field.data) except InvalidPhoneNumberException, e: raise ValidationError("Invalid phone number: %s" % field.data)
def user_exists(form, field): if count_user_by_pseudo(field.data) == 0: raise ValidationError("Does not exists.")
def valid_question(form, field): try: cdw.questions.with_id(field.data) except: raise ValidationError("Invalid Question ID")
def validate_S3_url(form, field): """Checks that field is a valid S3 url. Needs improvement. :) """ if field.data == "s3://": raise ValidationError(u'This field must be an S3 url.')
def existing_category(form, field): try: cdw.categories.with_id(field.data) except: raise ValidationError("Invalid category")