def __init__(self, require_note=False, ask_for_revalidation=False, *args, **kw): super().__init__(*args, **kw) if require_note: self["note"].validators = [required()] else: self["note"].validators = [optional()] if not ask_for_revalidation: del self["resoumission"]
class UserPreferencesForm(Form): password = StringField(_l("New Password"), widget=widgets.PasswordInput(autocomplete="off")) confirm_password = StringField( _l("Confirm new password"), widget=widgets.PasswordInput(autocomplete="off")) photo = fields.FileField(label=_l("Photo"), widget=widgets.ImageInput(width=55, height=55)) locale = fields.LocaleSelectField( label=_l("Preferred Language"), validators=[required()], default=lambda: get_default_locale(), ) timezone = fields.TimezoneField(label=_l("Time zone"), validators=(required(), ), default=babel.dates.LOCALTZ) def validate_password(self, field: StringField) -> None: pwd = field.data confirmed = self["confirm_password"].data if pwd != confirmed: raise ValidationError( _("Passwords differ. Ensure you have typed same password " 'in both "password" field and "confirm password" field.')) def validate_photo(self, field: FileField) -> None: data = request.form.get(field.name) if not data: return data = field.data filename = data.filename valid = any(filename.lower().endswith(ext) for ext in (".png", ".jpg", ".jpeg")) if not valid: raise ValidationError( _("Only PNG or JPG image files are accepted")) img_type = imghdr.what("ignored", data.read()) if img_type not in ("png", "jpeg"): raise ValidationError( _("Only PNG or JPG image files are accepted")) data.seek(0) try: # check this is actually an image file im = PIL.Image.open(data) im.load() except Exception: raise ValidationError(_("Could not decode image file")) # convert to jpeg # FIXME: better do this at model level? jpeg = BytesIO() im.convert("RGB").save(jpeg, "JPEG") field.data = jpeg.getvalue()
class FaqEditForm(Form): title = StringField("Titre", validators=[required()]) category = Select2Field("Catégorie", choices=[], validators=[required()]) body = StringField( "Contenu", validators=[required()], filters=(strip,), widget=RichTextWidget() )
class ConfirmerFinalisationForm(WorkflowForm): no_eotp = StringField("N° EOTP / référence DRI", validators=[required()])
class ConfirmerRecevabiliteForm(WorkflowForm): no_infolab = StringField("N° Infolab / référence DRI", validators=[required()])
class UserPreferencesForm(Form): password = StringField( _l('New Password'), widget=widgets.PasswordInput(autocomplete='off'), ) confirm_password = StringField( _l('Confirm new password'), widget=widgets.PasswordInput(autocomplete='off'), ) photo = fields.FileField( label=_l('Photo'), widget=widgets.ImageInput(width=55, height=55), ) locale = fields.LocaleSelectField( label=_l('Preferred Language'), validators=[required()], default=lambda: get_default_locale(), ) timezone = fields.TimezoneField( label=_l('Time zone'), validators=(required(), ), default=babel.dates.LOCALTZ, ) def validate_password(self, field): pwd = field.data confirmed = self['confirm_password'].data if pwd != confirmed: raise ValidationError( _( 'Passwords differ. Ensure you have typed same password in both' ' "password" field and "confirm password" field.', ), ) def validate_photo(self, field): data = request.form.get(field.name) if not data: return data = field.data filename = data.filename valid = any(filename.lower().endswith(ext) for ext in ('.png', '.jpg', '.jpeg')) if not valid: raise ValidationError( _('Only PNG or JPG image files are accepted'), ) img_type = imghdr.what('ignored', data.read()) if img_type not in ('png', 'jpeg'): raise ValidationError( _('Only PNG or JPG image files are accepted'), ) data.seek(0) try: # check this is actually an image file im = PIL.Image.open(data) im.load() except BaseException: raise ValidationError(_('Could not decode image file')) # convert to jpeg # FIXME: better do this at model level? jpeg = BytesIO() im.convert('RGB').save(jpeg, 'JPEG') field.data = jpeg.getvalue()