Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
slugify all tags, to avoid duplicate tags with different capitalisation
- Loading branch information
Showing
4 changed files
with
45 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from django.core.management.base import NoArgsCommand | ||
|
||
from taggit.models import Tag, TaggedItem | ||
|
||
from demoscene.utils.text import slugify_tag | ||
|
||
class Command(NoArgsCommand): | ||
def handle_noargs(self, **options): | ||
for tag in Tag.objects.all(): | ||
new_name = slugify_tag(tag.name) | ||
try: | ||
existing_tag = Tag.objects.exclude(id=tag.id).get(name=new_name) | ||
# if tag with that name already exists, move all tagged items to existing_tag | ||
# and delete this tag | ||
TaggedItem.objects.filter(tag=tag).update(tag=existing_tag) | ||
tag.delete() | ||
except Tag.DoesNotExist: | ||
# keep this tag, just rewrite the name | ||
tag.name = new_name | ||
tag.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import re | ||
import unicodedata | ||
|
||
from django.utils.safestring import mark_safe | ||
|
||
def slugify_tag(value): | ||
""" | ||
A version of django.utils.text.slugify that lets '.' characters through. | ||
""" | ||
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') | ||
value = re.sub('[^\w\s\.-]', '', value).strip().lower() | ||
return mark_safe(re.sub('[-\s]+', '-', value)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters