diff options
author | Eugene Kim <p.compassion@gmail.com> | 2013-05-27 15:07:36 +0900 |
---|---|---|
committer | Eugene Kim <p.compassion@gmail.com> | 2013-05-27 15:07:36 +0900 |
commit | 2ca555c59517bca146ff617371292b8dbb5bd54e (patch) | |
tree | 6175089f0f3e5f0b14aeedcbc874ffd3ca85b1c3 | |
parent | 5776e61d76aa44801a4dc451b41c3a01e23aa8a4 (diff) | |
download | askbot-2ca555c59517bca146ff617371292b8dbb5bd54e.tar.gz askbot-2ca555c59517bca146ff617371292b8dbb5bd54e.tar.bz2 askbot-2ca555c59517bca146ff617371292b8dbb5bd54e.zip |
If tagsynonym tag1->tag2 exists when a user asks to rename tag3->tag1, abort
If tagsynonym tag1->tag2 exists when a user wants to rename tag2->tag3, update tagsynonym table
When create_tag_synonym creates a new target_tag, we copy source_tag's info to target_tag.
-rw-r--r-- | askbot/management/commands/create_tag_synonyms.py | 6 | ||||
-rw-r--r-- | askbot/management/commands/rename_tags_id.py | 28 | ||||
-rw-r--r-- | askbot/models/tag.py | 2 |
3 files changed, 28 insertions, 8 deletions
diff --git a/askbot/management/commands/create_tag_synonyms.py b/askbot/management/commands/create_tag_synonyms.py index ccbb7e0a..e4324639 100644 --- a/askbot/management/commands/create_tag_synonyms.py +++ b/askbot/management/commands/create_tag_synonyms.py @@ -110,8 +110,12 @@ remove source_tag""" try: models.Tag.objects.get(name=target_tag_name) except models.Tag.DoesNotExist: + # we are creating a target tag, let's copy source tag's info + # used_count are updated later models.Tag.objects.create(name=target_tag_name, - created_by = admin + created_by = admin, + status = source_tag.status, + tag_wiki = source_tag.tag_wiki ) tag_synonym_tmp, created = models.TagSynonym.objects.get_or_create(source_tag_name = source_tag_name, diff --git a/askbot/management/commands/rename_tags_id.py b/askbot/management/commands/rename_tags_id.py index c584ba3a..1da22868 100644 --- a/askbot/management/commands/rename_tags_id.py +++ b/askbot/management/commands/rename_tags_id.py @@ -123,23 +123,33 @@ or repost a bug, if that does not help""" for question in questions[:10]: print '* %s' % question.title.strip() - from_tag_names = format_tag_name_list(from_tags) - to_tag_names = format_tag_name_list(to_tags) + formatted_from_tag_names = format_tag_name_list(from_tags) + formatted_to_tag_names = format_tag_name_list(to_tags) if not options.get('is_force', False): - prompt = 'Rename tags %s --> %s?' % (from_tag_names, to_tag_names) + prompt = 'Rename tags %s --> %s?' % (formatted_from_tag_names, formatted_to_tag_names) choice = console.choice_dialog(prompt, choices=('yes', 'no')) if choice == 'no': print 'Canceled' sys.exit() else: - print 'Renaming tags %s --> %s' % (from_tag_names, to_tag_names) + print 'Renaming tags %s --> %s' % (formatted_from_tag_names, formatted_to_tag_names) sys.stdout.write('Processing:') + + from_tag_names = get_tag_names(from_tags) + to_tag_names = get_tag_names(to_tags) + + #if user provided tag1 as to_tag, and tagsynonym tag1->tag2 exists. + for to_tag_name in to_tag_names: + try: + tag_synonym = models.TagSynonym.objects.get(source_tag_name = to_tag_name) + raise CommandError(u'You gave %s as --to argument, but TagSynonym: %s -> %s exists, probably you want to provide %s as --to argument' % (to_tag_name, tag_synonym.source_tag_name, tag_synonym.target_tag_name, tag_synonym.target_tag_name)) + except models.TagSynonym.DoesNotExist: + pass + #actual processing stage, only after this point we start to #modify stuff in the database, one question per transaction - from_tag_names = get_tag_names(from_tags) - to_tag_names = get_tag_names(to_tags) i = 0 for question in questions: tag_names = set(question.get_tag_names()) @@ -176,3 +186,9 @@ or repost a bug, if that does not help""" # print "None found." #print "Done." #transaction.commit() + + # A user wants to rename tag2->tag3 and tagsynonym tag1->tag2 exists. + # we want to update tagsynonym (tag1->tag2) to (tag1->tag3) + for from_tag_name in from_tag_names: + # we need db_index for target_tag_name as well for this + models.TagSynonym.objects.filter(target_tag_name = from_tag_name).update(target_tag_name = to_tag_name) diff --git a/askbot/models/tag.py b/askbot/models/tag.py index 6de83412..bf054627 100644 --- a/askbot/models/tag.py +++ b/askbot/models/tag.py @@ -323,7 +323,7 @@ class MarkedTag(models.Model): class TagSynonym(models.Model): source_tag_name = models.CharField(max_length=255, unique=True) - target_tag_name = models.CharField(max_length=255) + target_tag_name = models.CharField(max_length=255, db_index=True) created_at = models.DateTimeField(auto_now_add=True) owned_by = models.ForeignKey(User, related_name='tag_synonyms') auto_rename_count = models.IntegerField(default=0) |