Skip to content

change target_tag_name of TagSynonym to target_tag (foreign key to Tag m... #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions askbot/management/commands/create_tag_synonyms.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,30 @@ def handle(self, *args, **options):
tag_synonym_tmp = models.TagSynonym.objects.get(source_tag_name = target_tag_name)
if not options.get('is_force', False):
prompt = """There exists a TagSynonym %s ==> %s,
hence we will create a tag synonym %s ==> %s instead. Proceed?""" % (tag_synonym_tmp.source_tag_name, tag_synonym_tmp.target_tag_name,
source_tag_name, tag_synonym_tmp.target_tag_name)
hence we will create a tag synonym %s ==> %s instead. Proceed?""" % (tag_synonym_tmp.source_tag_name, tag_synonym_tmp.target_tag.name,
source_tag_name, tag_synonym_tmp.target_tag.name)
choice = console.choice_dialog(prompt, choices=('yes', 'no'))
if choice == 'no':
print 'Cancled'
sys.exit()
target_tag_name = tag_synonym_tmp.target_tag_name
target_tag_name = tag_synonym_tmp.target_tag.name
options['to'] = target_tag_name
except models.TagSynonym.DoesNotExist:
pass

try:
models.Tag.objects.get(name=target_tag_name)
target_tag=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,
target_tag=models.Tag.objects.create(name=target_tag_name,
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,
target_tag_name = target_tag_name,
target_tag = target_tag,
owned_by = admin
)

Expand All @@ -128,7 +128,7 @@ def handle(self, *args, **options):
# When source_tag_name is a target_tag_name of already existing TagSynonym.
# ie. if tag1->tag2 exists when user asked tag2->tag3
# we are going to convert all tag1->tag2 to tag1->tag3 as well
existing_tag_synonyms = models.TagSynonym.objects.filter(target_tag_name=source_tag_name)
existing_tag_synonyms = models.TagSynonym.objects.filter(target_tag__name=source_tag_name)
for existing_tag_synonym in existing_tag_synonyms:
new_options = options.copy()
new_options['from'] = existing_tag_synonym.source_tag_name
Expand Down
20 changes: 17 additions & 3 deletions askbot/management/commands/rename_tags_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def format_tag_name_list(tag_list):
name_list = get_tag_names(tag_list)
return u', '.join(name_list)

def prompt_tag_name(from_tag_name, to_tag_names, formatted_to_tag_names):
while True:
prompt = '%s ---> ???, please choose a tag_name from %s' % (from_tag_name, formatted_to_tag_names)
choice = console.choice_dialog(prompt, choices=to_tag_names)
if choice in to_tag_names:
return choice

class Command(BaseCommand):
"The command object itself"

Expand Down Expand Up @@ -143,7 +150,7 @@ def handle(self, *args, **options):
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))
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

Expand Down Expand Up @@ -190,5 +197,12 @@ def handle(self, *args, **options):
# 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)
if len(to_tags) > 1:
to_tag_name = prompt_tag_name(from_tag_name, to_tag_names, formatted_to_tag_names)
to_tag = models.Tag.objects.get(name=to_tag_name)
else:
to_tag = to_tags[0]

models.TagSynonym.objects.filter(target_tag__name = from_tag_name).update(target_tag = to_tag)


Loading