From cdf538bd6163a47b4c4a6326c943b6deaf2c495a Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Tue, 12 Jul 2011 20:06:17 -0500 Subject: adds filter function to parse and clean tags field input - for some reason the tags are showing up in the media edit form with u'..' and surrounded with []. I don't know why, grr --- mediagoblin/util.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index ab219df0..7ee0a2d5 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -22,6 +22,7 @@ import sys import re import urllib from math import ceil +from string import strip import copy from babel.localedata import exists @@ -369,8 +370,24 @@ def clean_html(html): return HTML_CLEANER.clean_html(html) -MARKDOWN_INSTANCE = markdown.Markdown(safe_mode='escape') +TAGS_DELIMITER = ' ' + +def convert_to_tag_list(tag_string): + """ + Filter input from a "tags" field, + Strips trailing, leading, and internal whitespace, and also converts + the user input into an array of tags + """ + if tag_string: + taglist = [] + stripped_tag_string = ' '.join(tag_string.strip().split()) + for tag in stripped_tag_string.split(TAGS_DELIMITER): + if tag.strip(): taglist.append(tag.strip()) + return taglist + + +MARKDOWN_INSTANCE = markdown.Markdown(safe_mode='escape') def cleaned_markdown_conversion(text): """ -- cgit v1.2.3 From 93e3468a2af92a623a659628a20605025cea9ca7 Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Tue, 12 Jul 2011 20:43:16 -0500 Subject: displays the tags on edit correctly now -before it was running the tags field through the submit filter. that was kind of dumb -removes the filter function from the edit form -adds unicode syntax in the filter function -uses split correctly when saving the edited tags to mongodb --- mediagoblin/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 7ee0a2d5..4421bec4 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -370,7 +370,7 @@ def clean_html(html): return HTML_CLEANER.clean_html(html) -TAGS_DELIMITER = ' ' +TAGS_DELIMITER = u' ' def convert_to_tag_list(tag_string): """ @@ -381,7 +381,7 @@ def convert_to_tag_list(tag_string): """ if tag_string: taglist = [] - stripped_tag_string = ' '.join(tag_string.strip().split()) + stripped_tag_string = u' '.join(tag_string.strip().split()) for tag in stripped_tag_string.split(TAGS_DELIMITER): if tag.strip(): taglist.append(tag.strip()) return taglist -- cgit v1.2.3 From 6f2e4585cc7475362205a9ddb0e69d6da2b6dc85 Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Tue, 12 Jul 2011 22:26:10 -0500 Subject: uses standard functions instead of form filters and fixes taglist default - seems simpler to use the same tag field processing procedures on media submit and edit, so now processing with a regular function instead of a form filter. Filters run on form load and post by default. - moved tags to sidebar - taglist defaults to [] instead of None - adds case sensitivity toggle --- mediagoblin/util.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 4421bec4..f2a2793b 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -371,6 +371,7 @@ def clean_html(html): TAGS_DELIMITER = u' ' +TAGS_CASE_SENSITIVE = False def convert_to_tag_list(tag_string): """ @@ -379,12 +380,16 @@ def convert_to_tag_list(tag_string): Strips trailing, leading, and internal whitespace, and also converts the user input into an array of tags """ + taglist = [] if tag_string: - taglist = [] stripped_tag_string = u' '.join(tag_string.strip().split()) for tag in stripped_tag_string.split(TAGS_DELIMITER): - if tag.strip(): taglist.append(tag.strip()) - return taglist + if tag.strip(): + if TAGS_CASE_SENSITIVE: + taglist.append(tag.strip()) + else: + taglist.append(tag.strip().lower()) + return taglist MARKDOWN_INSTANCE = markdown.Markdown(safe_mode='escape') -- cgit v1.2.3 From 4451219560a4d991a8c4d04e9dffa99fb092bd5b Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Tue, 12 Jul 2011 22:52:32 -0500 Subject: ensures no duplicate tags per media entry --- mediagoblin/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index f2a2793b..951bdd51 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -384,7 +384,7 @@ def convert_to_tag_list(tag_string): if tag_string: stripped_tag_string = u' '.join(tag_string.strip().split()) for tag in stripped_tag_string.split(TAGS_DELIMITER): - if tag.strip(): + if tag.strip() and tag not in taglist: if TAGS_CASE_SENSITIVE: taglist.append(tag.strip()) else: -- cgit v1.2.3 From cc7ff3c50513ae169abab196f32de97af30e6744 Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Tue, 12 Jul 2011 23:58:25 -0500 Subject: enforces maximum tag length with (in)appropriate messaging --- mediagoblin/util.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 951bdd51..44e64258 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -372,19 +372,39 @@ def clean_html(html): TAGS_DELIMITER = u' ' TAGS_CASE_SENSITIVE = False +TAGS_MAX_LENGTH = 50 -def convert_to_tag_list(tag_string): +def convert_to_tag_list(request): """ - Filter input from a "tags" field, + Filter input from any "tags" field in the session, Strips trailing, leading, and internal whitespace, and also converts - the user input into an array of tags + the "tags" text into an array of tags """ + tag_string = request.POST.get('tags') taglist = [] if tag_string: + + # Strip out internal, trailing, and leading whitespace stripped_tag_string = u' '.join(tag_string.strip().split()) + + # Split the tag string into a list of tags for tag in stripped_tag_string.split(TAGS_DELIMITER): + + # Do not permit duplicate tags 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: taglist.append(tag.strip()) else: -- cgit v1.2.3 From 909371cdceace162af880c275b9e6e70488e3029 Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Wed, 20 Jul 2011 23:54:32 -0500 Subject: raises tag length error in form context instead of in message queue --- mediagoblin/util.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 44e64258..a84e07c4 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -24,6 +24,7 @@ import urllib from math import ceil from string import strip import copy +import wtforms from babel.localedata import exists import jinja2 @@ -374,14 +375,13 @@ TAGS_DELIMITER = u' ' TAGS_CASE_SENSITIVE = False 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 the "tags" text into an array of tags """ - tag_string = request.POST.get('tags') taglist = [] if tag_string: @@ -394,17 +394,6 @@ def convert_to_tag_list(request): # Do not permit duplicate tags 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: taglist.append(tag.strip()) else: @@ -412,6 +401,24 @@ def convert_to_tag_list(request): 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') def cleaned_markdown_conversion(text): -- cgit v1.2.3 From 10d7496da2f147a30a70304e8be3a579b15fd093 Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Mon, 25 Jul 2011 23:46:36 -0500 Subject: use config_spec.ini to store tag parsing directives --- mediagoblin/util.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index a84e07c4..8bb90acf 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -371,10 +371,6 @@ def clean_html(html): return HTML_CLEANER.clean_html(html) -TAGS_DELIMITER = u' ' -TAGS_CASE_SENSITIVE = False -TAGS_MAX_LENGTH = 50 - def convert_to_tag_list(tag_string): """ Filter input from incoming string containing user tags, @@ -389,12 +385,13 @@ def convert_to_tag_list(tag_string): stripped_tag_string = u' '.join(tag_string.strip().split()) # Split the tag string into a list of tags - for tag in stripped_tag_string.split(TAGS_DELIMITER): + for tag in stripped_tag_string.split( + mg_globals.app_config['tags_delimiter']): # Do not permit duplicate tags if tag.strip() and tag not in taglist: - if TAGS_CASE_SENSITIVE: + if mg_globals.app_config['tags_case_sensitive']: taglist.append(tag.strip()) else: taglist.append(tag.strip().lower()) @@ -411,12 +408,12 @@ def tag_length_validator(form, field): tags = convert_to_tag_list(field.data) too_long_tags = [ tag for tag in tags - if len(tag) > TAGS_MAX_LENGTH] + if len(tag) > mg_globals.app_config['tags_max_length']] if too_long_tags: raise wtforms.ValidationError( - TOO_LONG_TAG_WARNING % ( - TAGS_MAX_LENGTH, ', '.join(too_long_tags))) + TOO_LONG_TAG_WARNING % (mg_globals.app_config['tags_max_length'], \ + ', '.join(too_long_tags))) MARKDOWN_INSTANCE = markdown.Markdown(safe_mode='escape') -- cgit v1.2.3 From 0712a06dc6d6b05cb78d4b10af12c051e8f765e3 Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Wed, 27 Jul 2011 14:42:09 -0500 Subject: changes tags to a list of dicts in the db, adding tag slugs - adds a function to convert the tag list of dicts to a text string properly delimited for loading into forms - tag string conversion function updated to generate list of dicts - updates all mentions of the conversion of the string to the tags db object - adds a tags template utility and updates the media template accordingly --- mediagoblin/util.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 8bb90acf..ab72b5c8 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -371,7 +371,7 @@ def clean_html(html): return HTML_CLEANER.clean_html(html) -def convert_to_tag_list(tag_string): +def convert_to_tag_list_of_dicts(tag_string): """ Filter input from incoming string containing user tags, @@ -389,15 +389,29 @@ def convert_to_tag_list(tag_string): mg_globals.app_config['tags_delimiter']): # Do not permit duplicate tags - if tag.strip() and tag not in taglist: + if tag.strip() and tag.strip() not in taglist: if mg_globals.app_config['tags_case_sensitive']: - taglist.append(tag.strip()) + taglist.append({'name': tag.strip(), + 'slug': slugify(tag.strip())}) else: - taglist.append(tag.strip().lower()) + taglist.append({'name': tag.strip().lower(), + 'slug': slugify(tag.strip().lower())}) return taglist +def media_tags_as_string(media_entry_tags): + """ + Generate a string from a media item's tags, stored as a list of dicts + + This is the opposite of convert_to_tag_list_of_dicts + """ + media_tag_string = '' + if media_entry_tags: + media_tag_string = mg_globals.app_config['tags_delimiter'].join( + [tag['name'] for tag in media_entry_tags]) + return media_tag_string + TOO_LONG_TAG_WARNING = \ u'Tags must be shorter than %s characters. Tags that are too long: %s' @@ -405,10 +419,10 @@ def tag_length_validator(form, field): """ Make sure tags do not exceed the maximum tag length. """ - tags = convert_to_tag_list(field.data) + tags = convert_to_tag_list_of_dicts(field.data) too_long_tags = [ - tag for tag in tags - if len(tag) > mg_globals.app_config['tags_max_length']] + tag['name'] for tag in tags + if len(tag['name']) > mg_globals.app_config['tags_max_length']] if too_long_tags: raise wtforms.ValidationError( -- cgit v1.2.3 From f99b5caeb68cb60d768f0e049388a6f4a8b68ac0 Mon Sep 17 00:00:00 2001 From: Caleb Forbes Davis V Date: Wed, 27 Jul 2011 23:57:43 -0500 Subject: modifies duplicate tag check for list of dict tag type change --- mediagoblin/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mediagoblin/util.py') diff --git a/mediagoblin/util.py b/mediagoblin/util.py index ab72b5c8..f051dc50 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -388,8 +388,8 @@ def convert_to_tag_list_of_dicts(tag_string): for tag in stripped_tag_string.split( mg_globals.app_config['tags_delimiter']): - # Do not permit duplicate tags - if tag.strip() and tag.strip() not in taglist: + # Ignore empty or duplicate tags + if tag.strip() and tag.strip() not in [t['name'] for t in taglist]: if mg_globals.app_config['tags_case_sensitive']: taglist.append({'name': tag.strip(), -- cgit v1.2.3