Skip to content

Instantly share code, notes, and snippets.

@fredkingham
Last active June 22, 2016 14:02
Show Gist options
  • Save fredkingham/175f549aa4db97b165efce44e43cd6ed to your computer and use it in GitHub Desktop.
Save fredkingham/175f549aa4db97b165efce44e43cd6ed to your computer and use it in GitHub Desktop.
tag cleaner.py
from opal.models import Tagging, Team
from django.db import transaction
COMMIT = False
def spread_by_time(episodes):
result = defaultdict(int)
for i in episodes:
if i.end:
timestamp = "{0} - {1}".format(i.end.year, i.end.month)
result[timestamp] += 1
return result
@transaction.atomic()
def figure_out_flawed_lists():
""" get patients that have an archived = True parent tag
this should never be correct if there's a subtag
"""
all_tags = Tagging.objects.all()
parent_teams = Team.objects.values_list("parent_id", flat=True).distinct()
parent_teams = Team.objects.filter(id__in=parent_teams)
all_tags = all_tags.filter(team__in=parent_teams)
all_tags = all_tags.filter(archived=True)
all_episodes = set([])
for all_tag in all_tags:
episode = all_tag.episode
for tag in episode.tagging_set.all():
if tag.team.parent == all_tag.team:
if not tag.archived:
tag.archived = True
if COMMIT:
tag.save()
all_episodes.add(episode)
if episode in all_episodes:
all_tag.archived = False
if COMMIT:
all_tag.save()
return all_episodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment