Merge remote-tracking branch 'JDShu/649_use_form_data_field'

* JDShu/649_use_form_data_field:
  Use WTForms data field in user_pages/views.py
  Use WTForms data field in auth/views.py
  auth: whitespace cleanup in views.py
  Use WTForms data field in plugins/oauth/views.py
  Use WTForms data field in submit/views.py
  Use WTForms data field in edit/views.py
This commit is contained in:
Elrond 2013-03-30 14:42:45 +01:00
commit 4924b93bac
5 changed files with 36 additions and 36 deletions

View File

@ -78,7 +78,7 @@ def register(request):
user.username = register_form.data['username'] user.username = register_form.data['username']
user.email = register_form.data['email'] user.email = register_form.data['email']
user.pw_hash = auth_lib.bcrypt_gen_password_hash( user.pw_hash = auth_lib.bcrypt_gen_password_hash(
request.form['password']) register_form.password.data)
user.verification_key = unicode(uuid.uuid4()) user.verification_key = unicode(uuid.uuid4())
user.save() user.save()
@ -116,7 +116,7 @@ def login(request):
if login_form.validate(): if login_form.validate():
user = User.query.filter_by(username=login_form.data['username']).first() user = User.query.filter_by(username=login_form.data['username']).first()
if user and user.check_login(request.form['password']): if user and user.check_login(login_form.password.data):
# set up login in session # set up login in session
request.session['user_id'] = unicode(user.id) request.session['user_id'] = unicode(user.id)
request.session.save() request.session.save()
@ -196,7 +196,7 @@ def resend_activation(request):
request, request,
messages.ERROR, messages.ERROR,
_('You must be logged in so we know who to send the email to!')) _('You must be logged in so we know who to send the email to!'))
return redirect(request, 'mediagoblin.auth.login') return redirect(request, 'mediagoblin.auth.login')
if request.user.email_verified: if request.user.email_verified:
@ -204,12 +204,12 @@ def resend_activation(request):
request, request,
messages.ERROR, messages.ERROR,
_("You've already verified your email address!")) _("You've already verified your email address!"))
return redirect(request, "mediagoblin.user_pages.user_home", user=request.user['username']) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user['username'])
request.user.verification_key = unicode(uuid.uuid4()) request.user.verification_key = unicode(uuid.uuid4())
request.user.save() request.user.save()
email_debug_message(request) email_debug_message(request)
send_verification_email(request.user, request) send_verification_email(request.user, request)
@ -241,11 +241,11 @@ def forgot_password(request):
# has been sanitized. Store if a user was found by email. We should # has been sanitized. Store if a user was found by email. We should
# not reveal if the operation was successful then as we don't want to # not reveal if the operation was successful then as we don't want to
# leak if an email address exists in the system. # leak if an email address exists in the system.
found_by_email = '@' in request.form['username'] found_by_email = '@' in fp_form.username.data
if found_by_email: if found_by_email:
user = User.query.filter_by( user = User.query.filter_by(
email = request.form['username']).first() email = fp_form.username.data).first()
# Don't reveal success in case the lookup happened by email address. # Don't reveal success in case the lookup happened by email address.
success_message=_("If that email address (case sensitive!) is " success_message=_("If that email address (case sensitive!) is "
"registered an email has been sent with instructions " "registered an email has been sent with instructions "
@ -253,7 +253,7 @@ def forgot_password(request):
else: # found by username else: # found by username
user = User.query.filter_by( user = User.query.filter_by(
username = request.form['username']).first() username = fp_form.username.data).first()
if user is None: if user is None:
messages.add_message(request, messages.add_message(request,
@ -317,7 +317,7 @@ def verify_forgot_password(request):
if request.method == 'POST' and cp_form.validate(): if request.method == 'POST' and cp_form.validate():
user.pw_hash = auth_lib.bcrypt_gen_password_hash( user.pw_hash = auth_lib.bcrypt_gen_password_hash(
request.form['password']) cp_form.password.data)
user.fp_verification_key = None user.fp_verification_key = None
user.fp_token_expire = None user.fp_token_expire = None
user.save() user.save()

View File

@ -26,7 +26,7 @@ from mediagoblin.auth import lib as auth_lib
from mediagoblin.edit import forms from mediagoblin.edit import forms
from mediagoblin.edit.lib import may_edit_media from mediagoblin.edit.lib import may_edit_media
from mediagoblin.decorators import (require_active_login, active_user_from_url, from mediagoblin.decorators import (require_active_login, active_user_from_url,
get_media_entry_by_id, get_media_entry_by_id,
user_may_alter_collection, get_user_collection) user_may_alter_collection, get_user_collection)
from mediagoblin.tools.response import render_to_response, redirect from mediagoblin.tools.response import render_to_response, redirect
from mediagoblin.tools.translate import pass_to_ugettext as _ from mediagoblin.tools.translate import pass_to_ugettext as _
@ -58,19 +58,19 @@ def edit_media(request, media):
if request.method == 'POST' and form.validate(): if request.method == 'POST' and form.validate():
# Make sure there isn't already a MediaEntry with such a slug # Make sure there isn't already a MediaEntry with such a slug
# and userid. # and userid.
slug = slugify(request.form['slug']) slug = slugify(form.slug.data)
slug_used = check_media_slug_used(media.uploader, slug, media.id) slug_used = check_media_slug_used(media.uploader, slug, media.id)
if slug_used: if slug_used:
form.slug.errors.append( form.slug.errors.append(
_(u'An entry with that slug already exists for this user.')) _(u'An entry with that slug already exists for this user.'))
else: else:
media.title = request.form['title'] media.title = form.title.data
media.description = request.form.get('description') media.description = form.description.data
media.tags = convert_to_tag_list_of_dicts( media.tags = convert_to_tag_list_of_dicts(
request.form.get('tags')) form.tags.data)
media.license = unicode(request.form.get('license', '')) or None media.license = unicode(form.license.data) or None
media.slug = slug media.slug = slug
media.save() media.save()
@ -142,7 +142,7 @@ def edit_attachments(request, media):
request.files['attachment_file'].stream.close() request.files['attachment_file'].stream.close()
media.attachment_files.append(dict( media.attachment_files.append(dict(
name=request.form['attachment_name'] \ name=form.attachment_name.data \
or request.files['attachment_file'].filename, or request.files['attachment_file'].filename,
filepath=attachment_public_filepath, filepath=attachment_public_filepath,
created=datetime.utcnow(), created=datetime.utcnow(),
@ -153,7 +153,7 @@ def edit_attachments(request, media):
messages.add_message( messages.add_message(
request, messages.SUCCESS, request, messages.SUCCESS,
_("You added the attachment %s!") \ _("You added the attachment %s!") \
% (request.form['attachment_name'] % (form.attachment_name.data
or request.files['attachment_file'].filename)) or request.files['attachment_file'].filename))
return redirect(request, return redirect(request,
@ -194,8 +194,8 @@ def edit_profile(request, url_user=None):
bio=user.bio) bio=user.bio)
if request.method == 'POST' and form.validate(): if request.method == 'POST' and form.validate():
user.url = unicode(request.form['url']) user.url = unicode(form.url.data)
user.bio = unicode(request.form['bio']) user.bio = unicode(form.bio.data)
user.save() user.save()
@ -309,25 +309,25 @@ def edit_collection(request, collection):
# Make sure there isn't already a Collection with such a slug # Make sure there isn't already a Collection with such a slug
# and userid. # and userid.
slug_used = check_collection_slug_used(request.db, collection.creator, slug_used = check_collection_slug_used(request.db, collection.creator,
request.form['slug'], collection.id) form.slug.data, collection.id)
# Make sure there isn't already a Collection with this title # Make sure there isn't already a Collection with this title
existing_collection = request.db.Collection.find_one({ existing_collection = request.db.Collection.find_one({
'creator': request.user.id, 'creator': request.user.id,
'title':request.form['title']}) 'title':form.title.data})
if existing_collection and existing_collection.id != collection.id: if existing_collection and existing_collection.id != collection.id:
messages.add_message( messages.add_message(
request, messages.ERROR, request, messages.ERROR,
_('You already have a collection called "%s"!') % \ _('You already have a collection called "%s"!') % \
request.form['title']) form.title.data)
elif slug_used: elif slug_used:
form.slug.errors.append( form.slug.errors.append(
_(u'A collection with that slug already exists for this user.')) _(u'A collection with that slug already exists for this user.'))
else: else:
collection.title = unicode(request.form['title']) collection.title = unicode(form.title.data)
collection.description = unicode(request.form.get('description')) collection.description = unicode(form.description.data)
collection.slug = unicode(request.form['slug']) collection.slug = unicode(form.slug.data)
collection.save() collection.save()

View File

@ -45,11 +45,11 @@ def register_client(request):
if request.method == 'POST' and form.validate(): if request.method == 'POST' and form.validate():
client = OAuthClient() client = OAuthClient()
client.name = unicode(request.form['name']) client.name = unicode(form.name.data)
client.description = unicode(request.form['description']) client.description = unicode(form.description.data)
client.type = unicode(request.form['type']) client.type = unicode(form.type.data)
client.owner_id = request.user.id client.owner_id = request.user.id
client.redirect_uri = unicode(request.form['redirect_uri']) client.redirect_uri = unicode(form.redirect_uri.data)
client.generate_identifier() client.generate_identifier()
client.generate_secret() client.generate_secret()

View File

@ -60,18 +60,18 @@ def submit_start(request):
entry = request.db.MediaEntry() entry = request.db.MediaEntry()
entry.media_type = unicode(media_type) entry.media_type = unicode(media_type)
entry.title = ( entry.title = (
unicode(request.form['title']) unicode(submit_form.title.data)
or unicode(splitext(filename)[0])) or unicode(splitext(filename)[0]))
entry.description = unicode(request.form.get('description')) entry.description = unicode(submit_form.description.data)
entry.license = unicode(request.form.get('license', "")) or None entry.license = unicode(submit_form.license.data) or None
entry.uploader = request.user.id entry.uploader = request.user.id
# Process the user's folksonomy "tags" # Process the user's folksonomy "tags"
entry.tags = convert_to_tag_list_of_dicts( entry.tags = convert_to_tag_list_of_dicts(
request.form.get('tags')) submit_form.tags.data)
# Generate a slug from the title # Generate a slug from the title
entry.generate_slug() entry.generate_slug()
@ -125,8 +125,8 @@ def add_collection(request, media=None):
try: try:
collection = request.db.Collection() collection = request.db.Collection()
collection.title = unicode(request.form['title']) collection.title = unicode(submit_form.title.data)
collection.description = unicode(request.form.get('description')) collection.description = unicode(submit_form.description.data)
collection.creator = request.user.id collection.creator = request.user.id
collection.generate_slug() collection.generate_slug()

View File

@ -204,7 +204,7 @@ def media_collect(request, media):
# If we are here, method=POST and the form is valid, submit things. # If we are here, method=POST and the form is valid, submit things.
# If the user is adding a new collection, use that: # If the user is adding a new collection, use that:
if request.form['collection_title']: if form.collection_title.data:
# Make sure this user isn't duplicating an existing collection # Make sure this user isn't duplicating an existing collection
existing_collection = Collection.query.filter_by( existing_collection = Collection.query.filter_by(
creator=request.user.id, creator=request.user.id,