mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-06 05:01:48 +02:00
add validator for default_currency
This force the usage of project's id that must be provided to the form
This commit is contained in:
parent
45a4eab600
commit
867e84ae91
3 changed files with 24 additions and 2 deletions
|
@ -69,7 +69,7 @@ class ProjectHandler(Resource):
|
||||||
return "DELETED"
|
return "DELETED"
|
||||||
|
|
||||||
def put(self, project):
|
def put(self, project):
|
||||||
form = EditProjectForm(meta={"csrf": False})
|
form = EditProjectForm(id=project.id, meta={"csrf": False})
|
||||||
if form.validate() and current_app.config.get("ALLOW_PUBLIC_PROJECT_CREATION"):
|
if form.validate() and current_app.config.get("ALLOW_PUBLIC_PROJECT_CREATION"):
|
||||||
form.update(project)
|
form.update(project)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from re import match
|
from re import match
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
import email_validator
|
import email_validator
|
||||||
from flask import request
|
from flask import request
|
||||||
|
@ -110,6 +111,10 @@ class EditProjectForm(FlaskForm):
|
||||||
default_currency = SelectField(_("Default Currency"), validators=[DataRequired()])
|
default_currency = SelectField(_("Default Currency"), validators=[DataRequired()])
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
if not hasattr(self, "id"):
|
||||||
|
# We must access the project to validate the default currency.
|
||||||
|
# Mimics a StringField. Defaut to empty string to ensure that query run smoothly.
|
||||||
|
self.id = SimpleNamespace(data=kwargs.pop("id", ""))
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.default_currency.choices = [
|
self.default_currency.choices = [
|
||||||
(currency_name, render_localized_currency(currency_name, detailed=True))
|
(currency_name, render_localized_currency(currency_name, detailed=True))
|
||||||
|
@ -142,6 +147,23 @@ class EditProjectForm(FlaskForm):
|
||||||
)
|
)
|
||||||
return project
|
return project
|
||||||
|
|
||||||
|
def validate_default_currency(form, field):
|
||||||
|
project = Project.query.get(form.id.data)
|
||||||
|
if (
|
||||||
|
project is not None
|
||||||
|
and field.data == CurrencyConverter.no_currency
|
||||||
|
and project.has_multiple_currencies()
|
||||||
|
):
|
||||||
|
raise ValidationError(
|
||||||
|
_(
|
||||||
|
(
|
||||||
|
"This project already uses different currencies"
|
||||||
|
"and can't be set to 'No Currency'"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
pass
|
||||||
|
|
||||||
def update(self, project):
|
def update(self, project):
|
||||||
"""Update the project with the information from the form"""
|
"""Update the project with the information from the form"""
|
||||||
project.name = self.name.data
|
project.name = self.name.data
|
||||||
|
|
|
@ -399,7 +399,7 @@ def reset_password():
|
||||||
|
|
||||||
@main.route("/<project_id>/edit", methods=["GET", "POST"])
|
@main.route("/<project_id>/edit", methods=["GET", "POST"])
|
||||||
def edit_project():
|
def edit_project():
|
||||||
edit_form = EditProjectForm()
|
edit_form = EditProjectForm(id=g.project.id)
|
||||||
import_form = UploadForm()
|
import_form = UploadForm()
|
||||||
# Import form
|
# Import form
|
||||||
if import_form.validate_on_submit():
|
if import_form.validate_on_submit():
|
||||||
|
|
Loading…
Reference in a new issue