7b2ab96b2d08017d6e83c7be9043351529e916ad
[mygpo.git] / mygpo / data / models.py
blob7b2ab96b2d08017d6e83c7be9043351529e916ad
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 PodcastTag(models.Model):
8 tag = models.CharField(max_length=100)
9 podcast = models.ForeignKey(Podcast)
10 source = models.CharField(max_length=100)
11 user = models.ForeignKey(User, null=True)
12 weight = models.IntegerField(default=1)
14 class Meta:
15 db_table = 'podcast_tags'
16 unique_together = ('podcast', 'source', 'user', 'tag')
19 class HistoricPodcastData(models.Model):
20 podcast = models.ForeignKey(Podcast)
21 date = models.DateField()
22 subscriber_count = models.IntegerField()
24 class Meta:
25 db_table = 'historic_podcast_data'
26 unique_together = ('podcast', 'date')
29 class BackendSubscription(models.Model):
30 """
31 Represents the data in the subscriptions table, which
32 contains all subscriptions, even those for currently deleted devices
33 """
34 device = models.ForeignKey(Device)
35 podcast = models.ForeignKey(Podcast)
36 user = models.ForeignKey(User)
37 subscribed_since = models.DateTimeField()
39 class Meta:
40 unique_together = ('device', 'podcast', 'user')
41 db_table = 'subscriptions'
44 class Listener(models.Model):
45 device = models.ForeignKey(Device)
46 user = models.ForeignKey(User)
47 episode = models.ForeignKey(Episode)
48 podcast = models.ForeignKey(Podcast)
49 first_listened = models.DateTimeField()
50 last_listened = models.DateTimeField()
52 class Meta:
53 db_table = 'listeners'
54 managed = False
57 class RelatedPodcast(models.Model):
58 ref_podcast = models.ForeignKey(Podcast, related_name='ref_podcast')
59 rel_podcast = models.ForeignKey(Podcast, related_name='rel_podcast')
60 priority = models.IntegerField()
62 class Meta:
63 db_table = 'related_podcasts'
66 class SuggestionBlacklist(models.Model):
67 user = models.ForeignKey(User)
68 podcast = models.ForeignKey(Podcast)
70 class Meta:
71 db_table = 'suggestion_blacklist'
72 managed = False
75 class DirectoryEntryManager(models.Manager):
77 def top_tags(self, total):
78 tags = self.raw("select *, count(id) as entries from podcast_tags group by tag order by entries desc")[:total]
80 tags = filter(lambda x: not x.tag.startswith('http://'), tags)
82 excluded_tags = getattr(settings, 'DIRECTORY_EXCLUDED_TAGS', [])
83 return filter(lambda x: not x.tag in excluded_tags, tags)
85 def podcasts_for_tag(self, tag):
86 return DirectoryEntry.objects.filter(tag=tag).order_by('-ranking')
89 class DirectoryEntry(models.Model):
90 podcast = models.ForeignKey(Podcast, null=True)
91 podcast_group = models.ForeignKey(PodcastGroup, null=True)
92 tag = models.CharField(max_length=100)
93 ranking = models.FloatField()
95 objects = DirectoryEntryManager()
97 def get_item(self):
98 if self.podcast:
99 return self.podcast
100 else:
101 return self.podcast_group
103 def get_podcast(self):
105 Returns a podcast which is representative for this toplist-entry
106 If the entry is a non-grouped podcast, it is returned
107 If the entry is a podcast group, one of its podcasts is returned
109 if self.podcast:
110 return self.podcast
111 else:
112 return self.podcast_group.podcasts()[0]
114 class Meta:
115 db_table = 'directory_entries'
118 from mygpo.data.signals import update_podcast_tag_entry
119 from django.db.models.signals import post_save, pre_delete
121 post_save.connect(update_podcast_tag_entry, sender=PodcastTag)
122 pre_delete.connect(update_podcast_tag_entry, sender=PodcastTag)