refactoring in publisher and Podcast model code
[mygpo.git] / mygpo / data / models.py
blobc047f4c6f92e7ef4087738acaff1d9a6980f2b1c
1 from django.db import models
2 from django.contrib.auth.models import User
3 from mygpo.api.models import Podcast, Episode, Device, ToplistEntry, PodcastGroup
4 from mygpo import settings
7 class PodcastTagManager(models.Manager):
9 def top_tags_for_podcast(self, podcast):
10 """
11 returns the most-assigned tags for the given podcast
12 """
13 tags = PodcastTag.objects.filter(podcast=podcast)
14 return self.top_tags(tags)
16 def top_tags(self, tags=None):
17 """
18 returns the most-assigned tags
20 If param tags is given, it is expected to be a PodcastTag-QuerySet.
21 The method is then restricted to this QuerySet, otherwise all tags
22 are considered
23 """
24 if not tags:
25 tags = PodcastTag.objects.all()
26 tags = tags.values('tag').annotate(count=models.Count('id'))
27 tags = sorted(tags, key=lambda x: x['count'], reverse=True)
28 return tags
31 class PodcastTag(models.Model):
32 tag = models.CharField(max_length=100)
33 podcast = models.ForeignKey(Podcast)
34 source = models.CharField(max_length=100)
35 user = models.ForeignKey(User, null=True)
36 weight = models.IntegerField(default=1)
38 objects = PodcastTagManager()
40 class Meta:
41 db_table = 'podcast_tags'
42 unique_together = ('podcast', 'source', 'user', 'tag')
45 class HistoricPodcastData(models.Model):
46 podcast = models.ForeignKey(Podcast)
47 date = models.DateField()
48 subscriber_count = models.IntegerField()
50 class Meta:
51 db_table = 'historic_podcast_data'
52 unique_together = ('podcast', 'date')
55 class BackendSubscription(models.Model):
56 """
57 Represents the data in the subscriptions table, which
58 contains all subscriptions, even those for currently deleted devices
59 """
60 device = models.ForeignKey(Device)
61 podcast = models.ForeignKey(Podcast)
62 user = models.ForeignKey(User)
63 subscribed_since = models.DateTimeField()
65 class Meta:
66 unique_together = ('device', 'podcast', 'user')
67 db_table = 'subscriptions'
70 class Listener(models.Model):
71 device = models.ForeignKey(Device)
72 user = models.ForeignKey(User)
73 episode = models.ForeignKey(Episode)
74 podcast = models.ForeignKey(Podcast)
75 first_listened = models.DateTimeField()
76 last_listened = models.DateTimeField()
78 class Meta:
79 db_table = 'listeners'
80 managed = False
83 class RelatedPodcast(models.Model):
84 ref_podcast = models.ForeignKey(Podcast, related_name='ref_podcast')
85 rel_podcast = models.ForeignKey(Podcast, related_name='rel_podcast')
86 priority = models.IntegerField()
88 class Meta:
89 db_table = 'related_podcasts'
92 class SuggestionBlacklist(models.Model):
93 user = models.ForeignKey(User)
94 podcast = models.ForeignKey(Podcast)
96 class Meta:
97 db_table = 'suggestion_blacklist'
98 managed = False
101 class DirectoryEntryManager(models.Manager):
103 def top_tags(self, total):
104 tags = self.raw("select *, count(id) as entries from podcast_tags group by tag order by entries desc")[:total]
106 tags = filter(lambda x: not x.tag.startswith('http://'), tags)
108 excluded_tags = getattr(settings, 'DIRECTORY_EXCLUDED_TAGS', [])
109 return filter(lambda x: not x.tag in excluded_tags, tags)
111 def podcasts_for_tag(self, tag):
112 return DirectoryEntry.objects.filter(tag=tag).order_by('-ranking')
115 class DirectoryEntry(models.Model):
116 podcast = models.ForeignKey(Podcast, null=True)
117 podcast_group = models.ForeignKey(PodcastGroup, null=True)
118 tag = models.CharField(max_length=100)
119 ranking = models.FloatField()
121 objects = DirectoryEntryManager()
123 def get_item(self):
124 if self.podcast:
125 return self.podcast
126 else:
127 return self.podcast_group
129 def get_podcast(self):
131 Returns a podcast which is representative for this toplist-entry
132 If the entry is a non-grouped podcast, it is returned
133 If the entry is a podcast group, one of its podcasts is returned
135 if self.podcast:
136 return self.podcast
137 else:
138 return self.podcast_group.podcasts()[0]
140 class Meta:
141 db_table = 'directory_entries'
144 from mygpo.data.signals import update_podcast_tag_entry
145 from django.db.models.signals import post_save, pre_delete
147 post_save.connect(update_podcast_tag_entry, sender=PodcastTag)
148 pre_delete.connect(update_podcast_tag_entry, sender=PodcastTag)