diff --git a/canaille/app/forms.py b/canaille/app/forms.py index 6f571d08..e2b8b5e3 100644 --- a/canaille/app/forms.py +++ b/canaille/app/forms.py @@ -234,21 +234,6 @@ def process_formdata(self, valuelist): raise ValueError(self.gettext("Not a valid datetime value.")) -class ReadOnly: - """ - Set a field readonly. - Validation fails if the form data is different than the - field object data, or if unset, from the field default data. - """ - - def __init__(self): - self.field_flags = {"readonly": True} - - def __call__(self, form, field): - if field.data and field.object_data and field.data != field.object_data: - raise wtforms.ValidationError(_("This field cannot be edited")) - - def is_readonly(field): return field.render_kw and "readonly" in field.render_kw @@ -256,9 +241,11 @@ def is_readonly(field): def set_readonly(field): field.render_kw = field.render_kw or {} field.render_kw["readonly"] = True - field.validators = list(field.validators) + [ReadOnly()] + field.validators = list(field.validators) + [wtforms.validators.ReadOnly()] def set_writable(field): del field.render_kw["readonly"] - field.validators = [v for v in field.validators if not isinstance(v, ReadOnly)] + field.validators = [ + v for v in field.validators if not isinstance(v, wtforms.validators.ReadOnly) + ] diff --git a/canaille/core/forms.py b/canaille/core/forms.py index dd0afd09..bbc3c6c3 100644 --- a/canaille/core/forms.py +++ b/canaille/core/forms.py @@ -1,4 +1,5 @@ import wtforms.form +import wtforms.validators from canaille.app import models from canaille.app.forms import BaseForm from canaille.app.forms import DateTimeUTCField @@ -6,7 +7,6 @@ from canaille.app.forms import Form from canaille.app.forms import is_uri from canaille.app.forms import phone_number -from canaille.app.forms import ReadOnly from canaille.app.forms import set_readonly from canaille.app.forms import unique_values from canaille.app.i18n import lazy_gettext as _ @@ -337,7 +337,7 @@ class EditGroupForm(Form): _("Name"), validators=[ wtforms.validators.DataRequired(), - ReadOnly(), + wtforms.validators.ReadOnly(), ], render_kw={ "readonly": "true", @@ -400,7 +400,7 @@ class InvitationForm(Form): class EmailConfirmationForm(Form): old_emails = wtforms.FieldList( wtforms.EmailField( - validators=[ReadOnly()], + validators=[wtforms.validators.ReadOnly()], description=_( "This email will be used as a recovery address to reset the password if needed" ),