4cd52f4905825f0107a4aa1497fe422e9c5566da
[mygpo.git] / mygpo / data / management / commands / update-directory.py
blob4cd52f4905825f0107a4aa1497fe422e9c5566da
1 from django.core.management.base import BaseCommand
2 from optparse import make_option
3 from mygpo.api.models import Podcast, PodcastGroup
4 from mygpo.data.models import DirectoryEntry
5 from mygpo.data.directory import get_source_weights, get_weighted_tags, get_weighted_group_tags
7 class Command(BaseCommand):
9 def handle(self, *args, **options):
11 source_weights = get_source_weights()
13 if len(args) > 0:
14 podcasts = Podcast.objects.filter(url__in=args)
16 else:
17 podcasts = Podcast.objects.filter(group=None).order_by('id').only('id')
19 for podcast in podcasts.iterator():
21 print podcast.id
22 DirectoryEntry.objects.filter(podcast=podcast).delete()
24 for tag, weight in get_weighted_tags(podcast, source_weights).iteritems():
25 if weight == 0:
26 continue
28 DirectoryEntry.objects.create(podcast=podcast, tag=tag, ranking=weight)
30 groups = PodcastGroup.objects.all().order_by('id').only('id')
31 for group in groups.iterator():
33 print group.id
34 DirectoryEntry.objects.filter(podcast_group=group).delete()
36 for tag, weight in get_weighted_group_tags(group, source_weights).iteritems():
37 if weight == 0:
38 continue
40 DirectoryEntry.objects.create(podcast_group=group, tag=tag, ranking=weight)