use form.validate_on_submit() whenever possible

This commit is contained in:
Son NK 2019-08-16 10:32:38 +02:00
parent 40f8c610d2
commit aeb4108570
2 changed files with 34 additions and 36 deletions

View file

@ -34,38 +34,37 @@ def client_detail(client_id):
flash("you cannot see this client", "warning")
return redirect(url_for("developer.index"))
if request.method == "POST":
if form.validate():
client.name = form.name.data
client.home_url = form.home_url.data
if form.validate_on_submit():
client.name = form.name.data
client.home_url = form.home_url.data
if form.icon.data:
# todo: remove current icon if any
# todo: handle remove icon
file_path = random_string(30)
file = File.create(path=file_path)
if form.icon.data:
# todo: remove current icon if any
# todo: handle remove icon
file_path = random_string(30)
file = File.create(path=file_path)
s3.upload_from_bytesio(file_path, BytesIO(form.icon.data.read()))
db.session.commit()
LOG.d("upload file %s to s3", file)
client.icon_id = file.id
db.session.commit()
uris = request.form.getlist("uri")
# replace all uris. TODO: optimize this?
for redirect_uri in client.redirect_uris:
RedirectUri.delete(redirect_uri.id)
for uri in uris:
RedirectUri.create(client_id=client_id, uri=uri)
s3.upload_from_bytesio(file_path, BytesIO(form.icon.data.read()))
db.session.commit()
LOG.d("upload file %s to s3", file)
flash(f"client {client.name} has been updated", "success")
client.icon_id = file.id
db.session.commit()
return redirect(url_for("developer.client_detail", client_id=client.id))
uris = request.form.getlist("uri")
# replace all uris. TODO: optimize this?
for redirect_uri in client.redirect_uris:
RedirectUri.delete(redirect_uri.id)
for uri in uris:
RedirectUri.create(client_id=client_id, uri=uri)
db.session.commit()
flash(f"client {client.name} has been updated", "success")
return redirect(url_for("developer.client_detail", client_id=client.id))
return render_template("developer/client_detail.html", form=form, client=client)

View file

@ -18,16 +18,15 @@ class NewClientForm(FlaskForm):
def new_client():
form = NewClientForm()
if request.method == "POST":
if form.validate():
client = Client.create_new(form.name.data, current_user.id)
db.session.commit()
if form.validate_on_submit():
client = Client.create_new(form.name.data, current_user.id)
db.session.commit()
notify_admin(f"user {current_user} created new app {client.name}")
flash("Your app has been created", "success")
notify_admin(f"user {current_user} created new app {client.name}")
flash("Your app has been created", "success")
return redirect(
url_for("developer.handle_step", client_id=client.id, step="step-0")
)
return redirect(
url_for("developer.handle_step", client_id=client.id, step="step-0")
)
return render_template("developer/new_client.html", form=form)