fix typo
[mygpo.git] / mygpo / directory / models.py
blob7b5a7f3e4e59158b340efdbb97dee0d20c87e045
1 from django.core.cache import cache
3 from couchdbkit.ext.django.schema import *
5 from mygpo.utils import iterate_together
6 from mygpo.core.proxy import DocumentABCMeta
7 from mygpo.db.couchdb.podcast import podcasts_by_id
10 class Category(Document):
12 __metaclass__ = DocumentABCMeta
14 label = StringProperty(required=True)
15 updated = DateTimeProperty(required=True)
16 spellings = StringListProperty()
17 podcasts = ListProperty()
20 def merge_podcasts(self, podcasts):
21 """
22 Merges some entries into the current category.
23 """
25 key = lambda e: e.podcast
27 podcasts = sorted(podcasts, key=key)
28 self.podcasts = sorted(self.podcasts, key=key)
30 new_entries = []
32 for e1, e2 in iterate_together([self.podcasts, podcasts], key):
33 if e1 is None:
34 new_entries.append(e2)
36 elif e2 is None:
37 new_entries.append(e1)
39 else:
40 new_entries.append( max(e1, e2) )
42 self.podcasts = new_entries
45 # called from within a template where we can't pass parameters
46 def get_podcasts_more(self, start=0, end=40):
47 return self.get_podcasts(start, end)
50 def get_podcasts(self, start=0, end=10):
51 cache_id = 'category-%s-%d-%d' % (self._id, start, end)
53 podcasts = cache.get(cache_id)
54 if podcasts:
55 return podcasts
57 ids = self.podcasts[start:end]
58 podcasts = podcasts_by_id(ids)
59 cache.set(cache_id, podcasts)
61 return podcasts
64 def get_weight(self):
65 return getattr(self, '_weight', len(self.podcasts))
68 def get_tags(self):
69 return self.spellings + [self.label]
71 def __repr__(self):
72 return '%s (+%d variants)' % (self.label, len(self.spellings))
75 class ExamplePodcasts(Document):
76 podcast_ids = StringListProperty()
77 updated = DateTimeProperty()