Created
August 25, 2021 16:14
-
-
Save owenThurm/165575b923d8d6df043f06f95340a4e7 to your computer and use it in GitHub Desktop.
Email and SMS library tagging
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
from app.models.library import LibraryItem, LibraryTag, LibraryTagCategory, TaggedLibraryItem | |
flow_type = 0 | |
# Grab channel category | |
channel_category = LibraryTagCategory.objects.get(library_type=flow_type, name="Channel") | |
# Make new Email and SMS Tag | |
email_and_sms_tag = LibraryTag( | |
name="Email and SMS", | |
library_type=flow_type, | |
library_category=channel_category | |
) | |
email_and_sms_tag.save() | |
# Get all flows library items with both the existing email tag and the existing SMS tag | |
email_tag = LibraryTag.objects.get(library_type=flow_type, name="Email") | |
sms_tag = LibraryTag.objects.get(library_type=flow_type, name="SMS") | |
email_tagged_items = TaggedLibraryItem.objects.filter( | |
library_item__library_type=flow_type, | |
library_tag=email_tag, | |
) | |
email_items = list(map(lambda tagged_item: tagged_item.library_item, email_tagged_items)) | |
email_and_sms_tagged_items = TaggedLibraryItem.objects.filter( | |
library_item__in=email_items, | |
library_tag=sms_tag, | |
) | |
email_and_sms_items = list(map(lambda tagged_item: tagged_item.library_item, email_and_sms_tagged_items)) | |
# Add the new "Email and SMS" tag to the items that have both the email tag and sms tag. | |
for item in email_and_sms_items: | |
new_email_and_sms_tagged_item = TaggedLibraryItem( | |
library_item=item, | |
library_tag=email_and_sms_tag, | |
) | |
new_email_and_sms_tagged_item.save() | |
# Remove the Email tags and the SMS tags from the newly tagged items | |
old_email_and_sms_tagged_items = TaggedLibraryItem.objects.filter( | |
library_item__in=email_and_sms_items, | |
library_tag__in=[email_tag, sms_tag], | |
) | |
for item in old_email_and_sms_tagged_items: | |
item.delete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment