1 from django
.core
.cache
import cache
3 from couchdbkit
.ext
.django
.schema
import *
5 from mygpo
.podcasts
.models
import Podcast
6 from mygpo
.utils
import iterate_together
7 from mygpo
.core
.proxy
import DocumentABCMeta
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
):
22 Merges some entries into the current category.
25 key
= lambda e
: e
.podcast
27 podcasts
= sorted(podcasts
, key
=key
)
28 self
.podcasts
= sorted(self
.podcasts
, key
=key
)
32 for e1
, e2
in iterate_together([self
.podcasts
, podcasts
], key
):
34 new_entries
.append(e2
)
37 new_entries
.append(e1
)
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
)
57 ids
= self
.podcasts
[start
:end
]
58 podcasts
= Podcast
.objects
.filter(id__in
=ids
)
59 cache
.set(cache_id
, podcasts
)
65 return getattr(self
, '_weight', len(self
.podcasts
))
69 return self
.spellings
+ [self
.label
]
72 return '%s (+%d variants)' % (self
.label
, len(self
.spellings
))
75 class ExamplePodcasts(Document
):
76 podcast_ids
= StringListProperty()
77 updated
= DateTimeProperty()