2 bug fixes in editor views

* `WTForms` instances get `__init__`-ed with `defaults` as `kwargs`.
  The first arg is a `request.form` (which is what one must supply if
  this is a `POST` and must *not* supply otherwise).
  The content of that form (empty on `GET`) has higher priority than
  the defaults (which makes the user get an empty form).

* Fix `edit_profile()` to allow changing `location` from a non-blank
  value to blank (i.e. removing the location).

(cherry picked from commit 75f3e23b92392b9bd309fab4c1a52fd38d453627)
This commit is contained in:
ĎÚβĨŐÚŚ Dod 2018-06-26 09:28:29 -04:00 committed by Boris Bobrov
parent f1e79d68e1
commit 3dfc8c9b4b

View File

@ -1,4 +1,4 @@
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
@ -70,7 +70,7 @@ def edit_media(request, media):
license=media.license) license=media.license)
form = forms.EditForm( form = forms.EditForm(
request.form, request.method=='POST' and request.form or None,
**defaults) **defaults)
if request.method == 'POST' and form.validate(): if request.method == 'POST' and form.validate():
@ -219,7 +219,8 @@ def edit_profile(request, url_user=None):
else: else:
location = user.get_location.name location = user.get_location.name
form = forms.EditProfileForm(request.form, form = forms.EditProfileForm(
request.method == 'POST' and request.form or None,
url=user.url, url=user.url,
bio=user.bio, bio=user.bio,
location=location) location=location)
@ -235,6 +236,8 @@ def edit_profile(request, url_user=None):
location = user.get_location location = user.get_location
location.name = six.text_type(form.location.data) location.name = six.text_type(form.location.data)
location.save() location.save()
else:
user.location = None
user.save() user.save()
@ -260,7 +263,8 @@ EMAIL_VERIFICATION_TEMPLATE = (
@require_active_login @require_active_login
def edit_account(request): def edit_account(request):
user = request.user user = request.user
form = forms.EditAccountForm(request.form, form = forms.EditAccountForm(
request.method == 'POST' and request.form or None,
wants_comment_notification=user.wants_comment_notification, wants_comment_notification=user.wants_comment_notification,
license_preference=user.license_preference, license_preference=user.license_preference,
wants_notifications=user.wants_notifications) wants_notifications=user.wants_notifications)
@ -358,7 +362,7 @@ def edit_collection(request, collection):
description=collection.description) description=collection.description)
form = forms.EditCollectionForm( form = forms.EditCollectionForm(
request.form, request.method == 'POST' and request.form or None,
**defaults) **defaults)
if request.method == 'POST' and form.validate(): if request.method == 'POST' and form.validate():
@ -454,7 +458,8 @@ def verify_email(request):
@require_active_login @require_active_login
def change_email(request): def change_email(request):
""" View to change the user's email """ """ View to change the user's email """
form = forms.ChangeEmailForm(request.form) form = forms.ChangeEmailForm(
request.method == 'POST' and request.form or None)
user = request.user user = request.user
# If no password authentication, no need to enter a password # If no password authentication, no need to enter a password
@ -511,7 +516,8 @@ def edit_metadata(request, media):
if not media.state == u'processed': if not media.state == u'processed':
return render_404(request) return render_404(request)
form = forms.EditMetaDataForm(request.form) form = forms.EditMetaDataForm(
request.method == 'POST' and request.form or None)
if request.method == "POST" and form.validate(): if request.method == "POST" and form.validate():
metadata_dict = dict([(row['identifier'],row['value']) metadata_dict = dict([(row['identifier'],row['value'])
for row in form.media_metadata.data]) for row in form.media_metadata.data])