simplify SearchEntry instantiation
authorStefan Koegl <stefan@skoegl.net>
Fri, 17 Sep 2010 11:28:26 +0000 (17 14:28 +0300)
committerStefan Koegl <stefan@skoegl.net>
Fri, 17 Sep 2010 13:29:44 +0000 (17 16:29 +0300)
mygpo/api/models/__init__.py
mygpo/search/management/commands/build-search-index.py
mygpo/search/models.py
mygpo/search/signals.py
mygpo/search/util.py

index 073a801..9f40f98 100644 (file)
@@ -63,19 +63,7 @@ class Podcast(models.Model):
         """
         returns all public subscriptions to this podcast
         """
-        subscriptions = Subscription.objects.filter(podcast=self)
-
-        # remove users with private profiles
-        subscriptions = subscriptions.exclude(user__userprofile__public_profile=False)
-
-        # remove inactive (eg deleted) users
-        subscriptions = subscriptions.exclude(user__is_active=False)
-
-        # remove uers that have marked their subscription to this podcast as private
-        private_users = SubscriptionMeta.objects.filter(podcast=self, public=False).values('user')
-        subscriptions = subscriptions.exclude(user__in=private_users)
-
-        return subscriptions
+        return Subscription.objects.public_subscriptions([self])
 
 
     def subscription_count(self):
@@ -177,6 +165,23 @@ class PodcastGroup(models.Model):
     def podcasts(self):
         return Podcast.objects.filter(group=self)
 
+    def subscriptions(self):
+        """
+        returns the public subscriptions to podcasts in the group
+        """
+        return Subscription.objects.public_subscriptions(self.podcasts())
+
+    def subscription_count(self):
+        return self.subscriptions().count()
+
+    def subscriber_count(self):
+        """
+        Returns the number of public subscriptions to podcasts of this group
+        """
+        subscriptions = self.subscriptions()
+        return subscriptions.values('user').distinct().count()
+
+
     def __unicode__(self):
         return self.title
 
@@ -514,12 +519,37 @@ class EpisodeAction(models.Model):
         db_table = 'episode_log'
 
 
+class SubscriptionManager(models.Manager):
+
+    def public_subscriptions(self, podcasts=None):
+        """
+        Returns either all public subscriptions or those for the given podcasts
+        """
+
+        subscriptions = self.filter(podcast__in=podcasts) if podcasts else self.all()
+
+        # remove users with private profiles
+        subscriptions = subscriptions.exclude(user__userprofile__public_profile=False)
+
+        # remove inactive (eg deleted) users
+        subscriptions = subscriptions.exclude(user__is_active=False)
+
+        if podcasts:
+            # remove uers that have marked their subscription to this podcast as private
+            private_users = SubscriptionMeta.objects.filter(podcast__in=podcasts, public=False).values('user')
+            subscriptions = subscriptions.exclude(user__in=private_users)
+
+        return subscriptions
+
+
 class Subscription(models.Model):
     device = models.ForeignKey(Device, primary_key=True)
     podcast = models.ForeignKey(Podcast)
     user = models.ForeignKey(User)
     subscribed_since = models.DateTimeField()
 
+    objects = SubscriptionManager()
+
     def __unicode__(self):
         return '%s - %s on %s' % (self.device.user, self.podcast, self.device)
 
index 78c016b..34eb98b 100644 (file)
@@ -2,7 +2,6 @@ from django.core.management.base import BaseCommand
 from optparse import make_option
 from mygpo.search.models import SearchEntry
 from mygpo.api.models import ToplistEntry
-from mygpo.search.util import podcast_entry, podcast_group_entry
 
 class Command(BaseCommand):
 
@@ -14,10 +13,10 @@ class Command(BaseCommand):
         for e in toplist:
             entry = None
             if e.podcast_group:
-                entry = podcast_group_entry(e.podcast_group, e.subscriptions)
+                entry = SearchEntry.from_object(e.podcast_group, e.subscriptions)
 
             elif e.podcast:
-                entry = podcast_entry(e.podcast, e.subscriptions)
+                entry = SearchEntry.from_object(e.podcast, e.subscriptions)
 
             if entry and entry.text:
                 entry.save()
index 91b314e..f67798e 100644 (file)
@@ -27,6 +27,27 @@ class SearchEntry(models.Model):
 
     objects = SearchEntryManager()
 
+    @classmethod
+    def from_object(cls, obj=None, subscriber_count=None):
+        """
+        Create a new SearchEntry from either a Podcast or a PodcastGroup
+        """
+        entry = SearchEntry()
+        entry.text = obj.title
+        entry.obj_id = obj.id
+        entry.priority = subscriber_count or obj.get_subscriber_count()
+
+        if isinstance(obj, Podcast):
+            entry.obj_type = 'podcast'
+            podcasts = Podcast.objects.filter(podcast=obj)
+        elif isinstance(obj, PodcastGroup):
+            entry.obj_type = 'podcast_group'
+            podcasts = Podcast.objects.filter(group=group)
+
+        entry.tags = tag_string(PodcastTag.objects.filter(podcast__in=podcasts))
+        return entry
+
+
     def __repr__(self):
         return "<%s '%s'>" % (self.__class__.__name__, self.text[:20])
 
index 6968606..1aa1089 100644 (file)
@@ -1,6 +1,5 @@
 from django.db.models.signals import post_save, pre_delete
 from mygpo.api.models import Podcast, PodcastGroup
-from mygpo.search.util import podcast_entry, podcast_group_entry
 
 
 def remove_podcast_entry(sender, instance=False, **kwargs):
@@ -16,7 +15,7 @@ def update_podcast_entry(sender, instance=False, **kwargs):
     # we don't want podcasts in groups to be indexed separately
     if instance and not instance.group:
         SearchEntry.objects.filter(obj_type='podcast', obj_id=instance.id).delete()
-        entry = podcast_entry(instance)
+        entry = SearchEntry.from_object(instance)
         entry.save()
 
 def update_podcast_group_entry(sender, instance=False, **kwargs):
@@ -26,7 +25,7 @@ def update_podcast_group_entry(sender, instance=False, **kwargs):
         SearchEntry.objects.filter(obj_type='podcast', obj_id=podcast.id).delete()
 
     SearchEntry.objects.filter(obj_type='podcast_group', obj_id=instance.id).delete()
-    entry = podcast_group_entry(instance)
+    entry = SearchEntry.from_object(instance)
     entry.save()
 
 
dissimilarity index 73%
index deba497..1e5c9e1 100644 (file)
@@ -1,51 +1,13 @@
-from django.db.models import Count
-from mygpo.api.models import Subscription, Podcast
-from mygpo.data.models import PodcastTag
-
-
-def podcast_group_entry(group, subscriber_count=None):
-    from mygpo.search.models import SearchEntry
-
-    if not subscriber_count:
-        subscriber_count = Subscription.objects.filter(podcast__group=group).values('user').distinct().count()
-
-    entry = SearchEntry()
-    entry.text = group.title
-    entry.obj_type = 'podcast_group'
-    entry.obj_id = group.id
-
-    podcasts = Podcast.objects.filter(group=group)
-    entry.tags = tag_string(PodcastTag.objects.filter(podcast__in=podcasts))
-
-    entry.priority = subscriber_count
-
-    return entry
-
-
-def podcast_entry(podcast, subscriber_count=None):
-    from mygpo.search.models import SearchEntry
-
-    if not subscriber_count:
-        subscriber_count = Subscription.objects.filter(podcast=podcast).values('user').distinct().count()
-
-    entry = SearchEntry()
-    entry.text = podcast.title
-    entry.obj_type = 'podcast'
-    entry.obj_id = podcast.id
-
-    entry.tags = tag_string(PodcastTag.objects.filter(podcast=podcast))
-
-    entry.priority = subscriber_count
-
-    return entry
-
-
-def tag_string(tags, max_length=200):
-    """
-    returns a string of the most-assigned tags
-
-    tags is expected to be a PodcastTag QuerySet
-    """
-    tags = PodcastTag.objects.top_tags(tags)
-    return ','.join([t['tag'] for t in tags])[:max_length]
-
+from django.db.models import Count
+from mygpo.api.models import Subscription, Podcast
+from mygpo.data.models import PodcastTag
+
+def tag_string(tags, max_length=200):
+    """
+    returns a string of the most-assigned tags
+
+    tags is expected to be a PodcastTag QuerySet
+    """
+    tags = PodcastTag.objects.top_tags(tags)
+    return ','.join([t['tag'] for t in tags])[:max_length]
+