allow setting logo_url for PodcastGroup objects
[mygpo.git] / mygpo / core / models.py
blobfd876d112653fdcd2264807a503f6765af479865
1 from __future__ import division
3 import hashlib
4 import re
5 from random import random
7 from couchdbkit.ext.django.schema import *
8 from restkit.errors import Unauthorized
10 from django.core.urlresolvers import reverse
12 from mygpo.decorators import repeat_on_conflict
13 from mygpo import utils
14 from mygpo.cache import cache_result
15 from mygpo.core.proxy import DocumentABCMeta
16 from mygpo.core.slugs import SlugMixin
17 from mygpo.core.oldid import OldIdMixin
18 from mygpo.web.logo import CoverArt
21 class SubscriptionException(Exception):
22 pass
25 class MergedIdException(Exception):
26 """ raised when an object is accessed through one of its merged_ids """
28 def __init__(self, obj, current_id):
29 self.obj = obj
30 self.current_id = current_id
33 class Episode(Document, SlugMixin, OldIdMixin):
34 """
35 Represents an Episode. Can only be part of a Podcast
36 """
38 __metaclass__ = DocumentABCMeta
40 title = StringProperty()
41 guid = StringProperty()
42 description = StringProperty(default="")
43 content = StringProperty(default="")
44 link = StringProperty()
45 released = DateTimeProperty()
46 author = StringProperty()
47 duration = IntegerProperty()
48 filesize = IntegerProperty()
49 language = StringProperty()
50 last_update = DateTimeProperty()
51 outdated = BooleanProperty(default=False)
52 mimetypes = StringListProperty()
53 merged_ids = StringListProperty()
54 urls = StringListProperty()
55 podcast = StringProperty(required=True)
56 listeners = IntegerProperty()
57 content_types = StringListProperty()
58 flattr_url = StringProperty()
62 @property
63 def url(self):
64 return self.urls[0]
66 def __repr__(self):
67 return 'Episode %s' % self._id
71 def get_short_title(self, common_title):
72 if not self.title or not common_title:
73 return None
75 title = self.title.replace(common_title, '').strip()
76 title = re.sub(r'^[\W\d]+', '', title)
77 return title
80 def get_episode_number(self, common_title):
81 if not self.title or not common_title:
82 return None
84 title = self.title.replace(common_title, '').strip()
85 match = re.search(r'^\W*(\d+)', title)
86 if not match:
87 return None
89 return int(match.group(1))
92 def get_ids(self):
93 return set([self._id] + self.merged_ids)
96 def __eq__(self, other):
97 if other == None:
98 return False
99 return self._id == other._id
102 def __hash__(self):
103 return hash(self._id)
106 def __str__(self):
107 return '<{cls} {title} ({id})>'.format(cls=self.__class__.__name__,
108 title=self.title, id=self._id)
110 __repr__ = __str__
113 class SubscriberData(DocumentSchema):
114 timestamp = DateTimeProperty()
115 subscriber_count = IntegerProperty()
117 def __eq__(self, other):
118 if not isinstance(other, SubscriberData):
119 return False
121 return (self.timestamp == other.timestamp) and \
122 (self.subscriber_count == other.subscriber_count)
124 def __hash__(self):
125 return hash(frozenset([self.timestamp, self.subscriber_count]))
128 class PodcastSubscriberData(Document):
129 podcast = StringProperty()
130 subscribers = SchemaListProperty(SubscriberData)
133 def __repr__(self):
134 return 'PodcastSubscriberData for Podcast %s (%s)' % (self.podcast, self._id)
137 class Podcast(Document, SlugMixin, OldIdMixin):
139 __metaclass__ = DocumentABCMeta
141 id = StringProperty()
142 title = StringProperty()
143 urls = StringListProperty()
144 description = StringProperty()
145 link = StringProperty()
146 last_update = DateTimeProperty()
147 logo_url = StringProperty()
148 author = StringProperty()
149 merged_ids = StringListProperty()
150 group = StringProperty()
151 group_member_name = StringProperty()
152 related_podcasts = StringListProperty()
153 subscribers = SchemaListProperty(SubscriberData)
154 language = StringProperty()
155 content_types = StringListProperty()
156 tags = DictProperty()
157 restrictions = StringListProperty()
158 common_episode_title = StringProperty()
159 new_location = StringProperty()
160 latest_episode_timestamp = DateTimeProperty()
161 episode_count = IntegerProperty()
162 random_key = FloatProperty(default=random)
163 flattr_url = StringProperty()
167 def get_podcast_by_id(self, id, current_id=False):
168 if current_id and id != self.get_id():
169 raise MergedIdException(self, self.get_id())
171 return self
174 get_podcast_by_oldid = get_podcast_by_id
175 get_podcast_by_url = get_podcast_by_id
178 def get_id(self):
179 return self.id or self._id
181 def get_ids(self):
182 return set([self.get_id()] + self.merged_ids)
184 @property
185 def display_title(self):
186 return self.title or self.url
189 def group_with(self, other, grouptitle, myname, othername):
191 if self.group and (self.group == other.group):
192 # they are already grouped
193 return
195 group1 = PodcastGroup.get(self.group) if self.group else None
196 group2 = PodcastGroup.get(other.group) if other.group else None
198 if group1 and group2:
199 raise ValueError('both podcasts already are in different groups')
201 elif not (group1 or group2):
202 group = PodcastGroup(title=grouptitle)
203 group.save()
204 group.add_podcast(self, myname)
205 group.add_podcast(other, othername)
206 return group
208 elif group1:
209 group1.add_podcast(other, othername)
210 return group1
212 else:
213 group2.add_podcast(self, myname)
214 return group2
218 def get_common_episode_title(self, num_episodes=100):
220 if self.common_episode_title:
221 return self.common_episode_title
223 from mygpo.db.couchdb.episode import episodes_for_podcast
224 episodes = episodes_for_podcast(self, descending=True, limit=num_episodes)
226 # We take all non-empty titles
227 titles = filter(None, (e.title for e in episodes))
228 # get the longest common substring
229 common_title = utils.longest_substr(titles)
231 # but consider only the part up to the first number. Otherwise we risk
232 # removing part of the number (eg if a feed contains episodes 100-199)
233 common_title = re.search(r'^\D*', common_title).group(0)
235 if len(common_title.strip()) < 2:
236 return None
238 return common_title
241 def get_episode_before(self, episode):
242 if not episode.released:
243 return None
245 from mygpo.db.couchdb.episode import episodes_for_podcast
246 prevs = episodes_for_podcast(self, until=episode.released,
247 descending=True, limit=1)
249 return next(iter(prevs), None)
252 def get_episode_after(self, episode):
253 if not episode.released:
254 return None
256 from mygpo.db.couchdb.episode import episodes_for_podcast
257 from datetime import timedelta
258 nexts = episodes_for_podcast(self,
259 since=episode.released + timedelta(seconds=1), limit=1)
261 return next(iter(nexts), None)
264 @property
265 def url(self):
266 return self.urls[0]
269 def get_podcast(self):
270 return self
273 def get_logo_url(self, size):
274 if self.logo_url:
275 filename = hashlib.sha1(self.logo_url).hexdigest()
276 else:
277 filename = 'podcast-%d.png' % (hash(self.title) % 5, )
279 prefix = CoverArt.get_prefix(filename)
281 return reverse('logo', args=[size, prefix, filename])
284 def subscriber_change(self):
285 prev = self.prev_subscriber_count()
286 if prev <= 0:
287 return 0
289 return self.subscriber_count() / prev
292 def subscriber_count(self):
293 if not self.subscribers:
294 return 0
295 return self.subscribers[-1].subscriber_count
298 def prev_subscriber_count(self):
299 if len(self.subscribers) < 2:
300 return 0
301 return self.subscribers[-2].subscriber_count
305 @repeat_on_conflict()
306 def subscribe(self, user, device):
307 from mygpo.db.couchdb.podcast_state import podcast_state_for_user_podcast
308 state = podcast_state_for_user_podcast(user, self)
309 state.subscribe(device)
310 try:
311 state.save()
312 user.sync_all()
313 except Unauthorized as ex:
314 raise SubscriptionException(ex)
317 @repeat_on_conflict()
318 def unsubscribe(self, user, device):
319 from mygpo.db.couchdb.podcast_state import podcast_state_for_user_podcast
320 state = podcast_state_for_user_podcast(user, self)
321 state.unsubscribe(device)
322 try:
323 state.save()
324 user.sync_all()
325 except Unauthorized as ex:
326 raise SubscriptionException(ex)
329 def subscribe_targets(self, user):
331 returns all Devices and SyncGroups on which this podcast can be subsrbied. This excludes all
332 devices/syncgroups on which the podcast is already subscribed
334 targets = []
336 subscriptions_by_devices = user.get_subscriptions_by_device()
338 for group in user.get_grouped_devices():
340 if group.is_synced:
342 dev = group.devices[0]
344 if not self.get_id() in subscriptions_by_devices[dev.id]:
345 targets.append(group.devices)
347 else:
348 for device in group.devices:
349 if not self.get_id() in subscriptions_by_devices[device.id]:
350 targets.append(device)
352 return targets
355 def __hash__(self):
356 return hash(self.get_id())
359 def __repr__(self):
360 if not self._id:
361 return super(Podcast, self).__repr__()
362 elif self.oldid:
363 return '%s %s (%s)' % (self.__class__.__name__, self.get_id(), self.oldid)
364 else:
365 return '%s %s' % (self.__class__.__name__, self.get_id())
368 def save(self):
369 group = getattr(self, 'group', None)
370 if group: #we are part of a PodcastGroup
371 group = PodcastGroup.get(group)
372 podcasts = list(group.podcasts)
374 if not self in podcasts:
375 # the podcast has not been added to the group correctly
376 group.add_podcast(self)
378 else:
379 i = podcasts.index(self)
380 podcasts[i] = self
381 group.podcasts = podcasts
382 group.save()
384 i = podcasts.index(self)
385 podcasts[i] = self
386 group.podcasts = podcasts
387 group.save()
389 else:
390 super(Podcast, self).save()
393 def delete(self):
394 group = getattr(self, 'group', None)
395 if group:
396 group = PodcastGroup.get(group)
397 podcasts = list(group.podcasts)
399 if self in podcasts:
400 i = podcasts.index(self)
401 del podcasts[i]
402 group.podcasts = podcasts
403 group.save()
405 else:
406 super(Podcast, self).delete()
409 def __eq__(self, other):
410 if not self.get_id():
411 return self == other
413 if other == None:
414 return False
416 return self.get_id() == other.get_id()
420 class PodcastGroup(Document, SlugMixin, OldIdMixin):
421 title = StringProperty()
422 podcasts = SchemaListProperty(Podcast)
424 def get_id(self):
425 return self._id
428 def get_podcast_by_id(self, id, current_id=False):
429 for podcast in self.podcasts:
430 if podcast.get_id() == id:
431 return podcast
433 if id in podcast.merged_ids:
434 if current_id:
435 raise MergedIdException(podcast, podcast.get_id())
437 return podcast
440 def get_podcast_by_oldid(self, oldid):
441 for podcast in list(self.podcasts):
442 if podcast.oldid == oldid:
443 return podcast
446 def get_podcast_by_url(self, url):
447 for podcast in self.podcasts:
448 if url in list(podcast.urls):
449 return podcast
452 def subscriber_change(self):
453 prev = self.prev_subscriber_count()
454 if not prev:
455 return 0
457 return self.subscriber_count() / prev
460 def subscriber_count(self):
461 return sum([p.subscriber_count() for p in self.podcasts])
464 def prev_subscriber_count(self):
465 return sum([p.prev_subscriber_count() for p in self.podcasts])
467 @property
468 def display_title(self):
469 return self.title
472 def get_podcast(self):
473 # return podcast with most subscribers (bug 1390)
474 return sorted(self.podcasts, key=Podcast.subscriber_count,
475 reverse=True)[0]
478 @property
479 def logo_url(self):
480 return utils.first(p.logo_url for p in self.podcasts)
482 @logo_url.setter
483 def logo_url(self, value):
484 self.podcasts[0].logo_url = value
487 def get_logo_url(self, size):
488 if self.logo_url:
489 filename = hashlib.sha1(self.logo_url).hexdigest()
490 else:
491 filename = 'podcast-%d.png' % (hash(self.title) % 5, )
493 prefix = CoverArt.get_prefix(filename)
495 return reverse('logo', args=[size, prefix, filename])
498 def add_podcast(self, podcast, member_name):
500 if not self._id:
501 raise ValueError('group has to have an _id first')
503 if not podcast._id:
504 raise ValueError('podcast needs to have an _id first')
506 if not podcast.id:
507 podcast.id = podcast._id
509 podcast.delete()
510 podcast.group = self._id
511 podcast.group_member_name = member_name
512 self.podcasts = sorted(self.podcasts + [podcast],
513 key=Podcast.subscriber_count, reverse=True)
514 self.save()
517 def __repr__(self):
518 if not self._id:
519 return super(PodcastGroup, self).__repr__()
520 elif self.oldid:
521 return '%s %s (%s)' % (self.__class__.__name__, self._id[:10], self.oldid)
522 else:
523 return '%s %s' % (self.__class__.__name__, self._id[:10])
527 class SanitizingRule(Document):
528 slug = StringProperty()
529 applies_to = StringListProperty()
530 search = StringProperty()
531 replace = StringProperty()
532 priority = IntegerProperty()
533 description = StringProperty()
536 def __repr__(self):
537 return 'SanitizingRule %s' % self._id