raises tag length error in form context instead of in message queue
This commit is contained in:
parent
cea8f2b632
commit
909371cdce
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
import wtforms
|
import wtforms
|
||||||
|
from mediagoblin.util import tag_length_validator, TOO_LONG_TAG_WARNING
|
||||||
|
|
||||||
|
|
||||||
class EditForm(wtforms.Form):
|
class EditForm(wtforms.Form):
|
||||||
@ -25,7 +26,9 @@ class EditForm(wtforms.Form):
|
|||||||
slug = wtforms.TextField(
|
slug = wtforms.TextField(
|
||||||
'Slug')
|
'Slug')
|
||||||
description = wtforms.TextAreaField('Description of this work')
|
description = wtforms.TextAreaField('Description of this work')
|
||||||
tags = wtforms.TextField('Tags')
|
tags = wtforms.TextField(
|
||||||
|
'Tags',
|
||||||
|
[tag_length_validator])
|
||||||
|
|
||||||
class EditProfileForm(wtforms.Form):
|
class EditProfileForm(wtforms.Form):
|
||||||
bio = wtforms.TextAreaField('Bio',
|
bio = wtforms.TextAreaField('Bio',
|
||||||
|
@ -55,6 +55,7 @@ def edit_media(request, media):
|
|||||||
else:
|
else:
|
||||||
media['title'] = request.POST['title']
|
media['title'] = request.POST['title']
|
||||||
media['description'] = request.POST.get('description')
|
media['description'] = request.POST.get('description')
|
||||||
|
media['tags'] = convert_to_tag_list(request.POST.get('tags'))
|
||||||
|
|
||||||
md = markdown.Markdown(
|
md = markdown.Markdown(
|
||||||
safe_mode = 'escape')
|
safe_mode = 'escape')
|
||||||
@ -63,9 +64,6 @@ def edit_media(request, media):
|
|||||||
media['description']))
|
media['description']))
|
||||||
|
|
||||||
media['slug'] = request.POST['slug']
|
media['slug'] = request.POST['slug']
|
||||||
|
|
||||||
# Process the user's folksonomy "tags"
|
|
||||||
media['tags'] = convert_to_tag_list(request)
|
|
||||||
media.save()
|
media.save()
|
||||||
|
|
||||||
return redirect(request, "mediagoblin.user_pages.media_home",
|
return redirect(request, "mediagoblin.user_pages.media_home",
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
import wtforms
|
import wtforms
|
||||||
|
from mediagoblin.util import tag_length_validator, TOO_LONG_TAG_WARNING
|
||||||
|
|
||||||
|
|
||||||
class SubmitStartForm(wtforms.Form):
|
class SubmitStartForm(wtforms.Form):
|
||||||
@ -24,4 +25,6 @@ class SubmitStartForm(wtforms.Form):
|
|||||||
[wtforms.validators.Length(min=0, max=500)])
|
[wtforms.validators.Length(min=0, max=500)])
|
||||||
description = wtforms.TextAreaField('Description of this work')
|
description = wtforms.TextAreaField('Description of this work')
|
||||||
file = wtforms.FileField('File')
|
file = wtforms.FileField('File')
|
||||||
tags = wtforms.TextField('Tags')
|
tags = wtforms.TextField(
|
||||||
|
'Tags',
|
||||||
|
[tag_length_validator])
|
||||||
|
@ -24,6 +24,7 @@ import urllib
|
|||||||
from math import ceil
|
from math import ceil
|
||||||
from string import strip
|
from string import strip
|
||||||
import copy
|
import copy
|
||||||
|
import wtforms
|
||||||
|
|
||||||
from babel.localedata import exists
|
from babel.localedata import exists
|
||||||
import jinja2
|
import jinja2
|
||||||
@ -374,14 +375,13 @@ TAGS_DELIMITER = u' '
|
|||||||
TAGS_CASE_SENSITIVE = False
|
TAGS_CASE_SENSITIVE = False
|
||||||
TAGS_MAX_LENGTH = 50
|
TAGS_MAX_LENGTH = 50
|
||||||
|
|
||||||
def convert_to_tag_list(request):
|
def convert_to_tag_list(tag_string):
|
||||||
"""
|
"""
|
||||||
Filter input from any "tags" field in the session,
|
Filter input from incoming string containing user tags,
|
||||||
|
|
||||||
Strips trailing, leading, and internal whitespace, and also converts
|
Strips trailing, leading, and internal whitespace, and also converts
|
||||||
the "tags" text into an array of tags
|
the "tags" text into an array of tags
|
||||||
"""
|
"""
|
||||||
tag_string = request.POST.get('tags')
|
|
||||||
taglist = []
|
taglist = []
|
||||||
if tag_string:
|
if tag_string:
|
||||||
|
|
||||||
@ -394,17 +394,6 @@ def convert_to_tag_list(request):
|
|||||||
# Do not permit duplicate tags
|
# Do not permit duplicate tags
|
||||||
if tag.strip() and tag not in taglist:
|
if tag.strip() and tag not in taglist:
|
||||||
|
|
||||||
# Enforce maximum tag length
|
|
||||||
if len(tag) > TAGS_MAX_LENGTH:
|
|
||||||
tag = tag[:TAGS_MAX_LENGTH] + u'...'
|
|
||||||
messages.add_message(
|
|
||||||
request, messages.WARNING, \
|
|
||||||
u'Tag truncated to ' + unicode(TAGS_MAX_LENGTH) + \
|
|
||||||
u' characters.')
|
|
||||||
messages.add_message(
|
|
||||||
request, messages.INFO, \
|
|
||||||
u'Why the long tag? Seriously.')
|
|
||||||
|
|
||||||
if TAGS_CASE_SENSITIVE:
|
if TAGS_CASE_SENSITIVE:
|
||||||
taglist.append(tag.strip())
|
taglist.append(tag.strip())
|
||||||
else:
|
else:
|
||||||
@ -412,6 +401,24 @@ def convert_to_tag_list(request):
|
|||||||
return taglist
|
return taglist
|
||||||
|
|
||||||
|
|
||||||
|
TOO_LONG_TAG_WARNING = \
|
||||||
|
u'Tags must be shorter than %s characters. Tags that are too long: %s'
|
||||||
|
|
||||||
|
def tag_length_validator(form, field):
|
||||||
|
"""
|
||||||
|
Make sure tags do not exceed the maximum tag length.
|
||||||
|
"""
|
||||||
|
tags = convert_to_tag_list(field.data)
|
||||||
|
too_long_tags = [
|
||||||
|
tag for tag in tags
|
||||||
|
if len(tag) > TAGS_MAX_LENGTH]
|
||||||
|
|
||||||
|
if too_long_tags:
|
||||||
|
raise wtforms.ValidationError(
|
||||||
|
TOO_LONG_TAG_WARNING % (
|
||||||
|
TAGS_MAX_LENGTH, ', '.join(too_long_tags)))
|
||||||
|
|
||||||
|
|
||||||
MARKDOWN_INSTANCE = markdown.Markdown(safe_mode='escape')
|
MARKDOWN_INSTANCE = markdown.Markdown(safe_mode='escape')
|
||||||
|
|
||||||
def cleaned_markdown_conversion(text):
|
def cleaned_markdown_conversion(text):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user