diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2014-01-29 01:37:17 -0300 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2014-01-29 01:37:17 -0300 |
commit | 118ec452b18dbefdf5c1cf64502ceb83111c0e2d (patch) | |
tree | 84b4d01bba27a593892cee9ab6c9b8cc2e8bd399 | |
parent | a896781aba04d189890fde6f09f22b99ea60d08e (diff) | |
download | askbot-118ec452b18dbefdf5c1cf64502ceb83111c0e2d.tar.gz askbot-118ec452b18dbefdf5c1cf64502ceb83111c0e2d.tar.bz2 askbot-118ec452b18dbefdf5c1cf64502ceb83111c0e2d.zip |
fix_question_tags seems to work now
-rw-r--r-- | askbot/management/commands/find_bodyless_questions.py | 19 | ||||
-rw-r--r-- | askbot/management/commands/fix_question_tags.py | 14 | ||||
-rw-r--r-- | askbot/models/question.py | 30 |
3 files changed, 55 insertions, 8 deletions
diff --git a/askbot/management/commands/find_bodyless_questions.py b/askbot/management/commands/find_bodyless_questions.py index 75312620..b137ba54 100644 --- a/askbot/management/commands/find_bodyless_questions.py +++ b/askbot/management/commands/find_bodyless_questions.py @@ -2,19 +2,28 @@ that do not have revisions by creating a fake initial revision based on the content stored in the post itself """ -from django.core.management.base import NoArgsCommand +from django.core.management.base import BaseCommand from askbot import models from askbot import const from askbot.utils.console import ProgressBar +from optparse import make_option def print_results(items): template = 'id=%d, title=%s' for thread in items: print template % (thread.id, thread.title.encode('utf8')) -class Command(NoArgsCommand): +class Command(BaseCommand): """Command class for "fix_bodyless_questions" """ + option_list = BaseCommand.option_list + ( + make_option('--delete', + action='store_true', + dest='delete', + default=False, + help='Delete poll instead of closing it', + ), + ) def handle(self, *arguments, **options): """function that handles the command job """ @@ -39,6 +48,12 @@ class Command(NoArgsCommand): if len(bodyless): print '\nQuestions without body text:' print_results(bodyless) + if options['delete']: + for thread in bodyless: + thread.delete() if len(multi_body): print '\nQuestions with >1 instances of body text' print_results(multi_body) + if options['delete']: + for thread in multi_body: + thread.delete() diff --git a/askbot/management/commands/fix_question_tags.py b/askbot/management/commands/fix_question_tags.py index 50b1b855..3cb696b0 100644 --- a/askbot/management/commands/fix_question_tags.py +++ b/askbot/management/commands/fix_question_tags.py @@ -8,6 +8,7 @@ from askbot import const from askbot import models from askbot import forms from askbot.utils import console +from askbot.utils.slug import slugify_camelcase from askbot.models import signals from askbot.conf import settings as askbot_settings from askbot.management.commands.rename_tags import get_admin @@ -19,6 +20,7 @@ def get_valid_tag_name(tag): """ name = tag.name if askbot_settings.FORCE_LOWERCASE_TAGS: + #name = slugify_camelcase(name) name = name.lower() #if tag name starts with forbidden character, chop off that character #until no more forbidden chars are at the beginning @@ -122,13 +124,23 @@ class Command(NoArgsCommand): total_count = threads.count() print "Searching for questions with inconsistent copies of tag records:", for thread in threads: + #make sure that denormalized tag set is the same as normalized + #we just add both the tags together and try to apply them + #to the question tags = thread.tags.all() denorm_tag_set = set(thread.get_tag_names()) norm_tag_set = set(thread.tags.values_list('name', flat=True)) + if norm_tag_set != denorm_tag_set: + denorm_tag_set.update(norm_tag_set) + cleaned_tag_set = set( + models.Tag.objects.filter( + name__in=denorm_tag_set + ).values_list('name', flat=True) + ) self.admin.retag_question( question=thread._question_post(), - tags=' '.join(norm_tag_set) + tags=' '.join(cleaned_tag_set) ) transaction.commit() diff --git a/askbot/models/question.py b/askbot/models/question.py index 51a4cc2b..987ea434 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -132,6 +132,7 @@ class ThreadManager(BaseQuerySetManager): # TODO: Some of this code will go to Post.objects.create_new language = language or get_language() + tagnames = self.clean_tagnames(tagnames) thread = super( ThreadManager, @@ -1410,19 +1411,38 @@ class Thread(models.Model): silent=silent ) + + def clean_tagnames(self, tagnames): + """Cleans tagnames string so that the field fits the constraint of the + database. + TODO: remove this when the Thread.tagnames field is converted into + text_field + """ + original = tagnames + tagnames = tagnames.strip().split() + #see if the tagnames field fits into 125 bytes + while True: + encoded_tagnames = ' '.join(tagnames).encode('utf-8') + length = len(encoded_tagnames) + if length == 0: + return '' + elif length <= 125: + return ' '.join(tagnames) + else: + tagnames.pop() + + def retag(self, retagged_by=None, retagged_at=None, tagnames=None, silent=False): """changes thread tags""" if None in (retagged_by, retagged_at, tagnames): raise Exception('arguments retagged_at, retagged_by and tagnames are required') - if len(tagnames) > 125:#todo: remove magic number!!! - raise django_exceptions.ValidationError('tagnames value too long') + tagnames = self.clean_tagnames(tagnames) + self.tagnames = tagnames + self.save() thread_question = self._question_post() - self.tagnames = tagnames.strip() - self.save() - # Update the Question itself if silent == False: thread_question.last_edited_at = retagged_at |