in pulseapi/tags/helpers.py [0:0]
def remove_tags_with_commas(app, schema_editor):
"""
This function analyses any tag for whether it contains
commas. If it does, it splits on comma, trims the result,
and then either creates a new tag, or uses an existing tag,
to retag any entry that had a comma-encumbered tag (or tags).
"""
tags_with_commas = Tag.objects.filter(name__contains=',')
for tag in tags_with_commas:
# Get all entries that use this tag right now, because
# we're going to need to change what it points to.
entries = Entry.objects.filter(tags__name=tag.name)
# Create a list of plain string "tags" that we get by
# resolving the comma(s) in our comma-laden tag.
trimmed = tag.name.strip()
splitlist = re.split(r'\s*,\s*', trimmed)
filtered = list(filter(None, splitlist))
# For each tag string we now have, update the entries that
# used this tag so that they're tagged with the right thing
# instead of our comma-encumbered tag.
for tag_string in filtered:
(retag, created) = Tag.objects.get_or_create(name=tag_string)
for entry in entries:
if retag in entry.tags.all():
continue
else:
entry.tags.add(retag)
# After updating all entries to use the new (set of) tag(s),
# we can safely delete this offending comma-encumbered tag.
tag.delete()