project forms: Clarify relation between inheriting classes

The save() function is used to create a new project, so it belongs to the
form creation class.

Also clarify overriden form fields.
This commit is contained in:
Baptiste Jonglez 2021-07-04 13:15:36 +02:00 committed by zorun
parent f7ae02546e
commit 856412a70d

View file

@ -136,21 +136,6 @@ class EditProjectForm(FlaskForm):
else: else:
return LoggingMode.ENABLED return LoggingMode.ENABLED
def save(self):
"""Create a new project with the information given by this form.
Returns the created instance
"""
project = Project(
name=self.name.data,
id=self.id.data,
password=generate_password_hash(self.password.data),
contact_email=self.contact_email.data,
logging_preference=self.logging_preference,
default_currency=self.default_currency.data,
)
return project
def validate_default_currency(form, field): def validate_default_currency(form, field):
project = Project.query.get(form.id.data) project = Project.query.get(form.id.data)
if ( if (
@ -191,16 +176,30 @@ class UploadForm(FlaskForm):
class ProjectForm(EditProjectForm): class ProjectForm(EditProjectForm):
id = StringField(_("Project identifier"), validators=[DataRequired()]) id = StringField(_("Project identifier"), validators=[DataRequired()])
# This field overrides the one from EditProjectForm
password = PasswordField(_("Private code"), validators=[DataRequired()]) password = PasswordField(_("Private code"), validators=[DataRequired()])
submit = SubmitField(_("Create the project")) submit = SubmitField(_("Create the project"))
def save(self): def save(self):
"""Create a new project with the information given by this form.
Returns the created instance
"""
# WTForms Boolean Fields don't insert the default value when the # WTForms Boolean Fields don't insert the default value when the
# request doesn't include any value the way that other fields do, # request doesn't include any value the way that other fields do,
# so we'll manually do it here # so we'll manually do it here
self.project_history.data = LoggingMode.default() != LoggingMode.DISABLED self.project_history.data = LoggingMode.default() != LoggingMode.DISABLED
self.ip_recording.data = LoggingMode.default() == LoggingMode.RECORD_IP self.ip_recording.data = LoggingMode.default() == LoggingMode.RECORD_IP
return super().save() # Create project
project = Project(
name=self.name.data,
id=self.id.data,
password=generate_password_hash(self.password.data),
contact_email=self.contact_email.data,
logging_preference=self.logging_preference,
default_currency=self.default_currency.data,
)
return project
def validate_id(form, field): def validate_id(form, field):
form.id.data = slugify(field.data) form.id.data = slugify(field.data)