[History] store episode history in Django ORM
[mygpo.git] / mygpo / core / tasks.py
blob3bde5011dce8a114784ec44c268c103a2c6d87d2
1 from datetime import datetime
3 from django.utils.translation import ugettext as _
4 from django.conf import settings
6 from mygpo.podcasts.models import Podcast, Episode
7 from mygpo.celery import celery
8 from mygpo.history.models import HistoryEntry
9 from mygpo.data.feeddownloader import PodcastUpdater
10 from mygpo.utils import get_timestamp
11 from mygpo.users.models import EpisodeAction
12 from mygpo.flattr import Flattr
13 from mygpo.history.models import EpisodeHistoryEntry
16 @celery.task(max_retries=5, default_retry_delay=60)
17 def flattr_thing(user, thing_id, domain, is_secure, thing_type):
18 """ Task to flattr a thing """
20 flattr = Flattr(user, domain, is_secure)
22 if thing_type == 'Podcast':
23 thing = Podcast.objects.get(id=thing_id)
24 episode, podcast = None, thing
26 elif thing_type == 'Episode':
27 query = Episode.objects.filter(id=thing_id).select_related('podcast')
28 thing = query.get()
29 episode, podcast = thing, thing.podcast
31 else:
32 raise NotImplemented(_("Can't flattr a '%s'") % thing_type)
35 if not thing.flattr_url:
36 return False, _('No Payment URL available')
38 try:
39 success, msg = flattr.flattr_url(thing.flattr_url)
41 if settings.FLATTR_MYGPO_THING:
42 flattr.flattr_url(settings.FLATTR_MYGPO_THING)
44 except Exception as ex:
45 raise flattr_thing.retry(exc=ex)
47 if success:
48 HistoryEntry.objects.create(
49 timestamp=datetime.utcnow(),
50 podcast=podcast,
51 episode=episode,
52 user=user,
53 client=None,
54 action=HistoryEntry.FLATTR,
58 return success, msg
61 @celery.task(max_retries=5, default_retry_delay=60)
62 def auto_flattr_episode(user, episode_id):
63 """ Task to auto-flattr an episode
65 In addition to the flattring itself, it also records the event """
67 success, msg = flattr_thing(user, episode_id, None, False, 'Episode')
69 if not success:
70 return False
72 episode = Episode.objects.get(id=episode_id)
74 EpisodeHistoryEntry.objects.create(
75 episode = episode,
76 action = EpisodeHistoryEntry.FLATTR,
77 timestamp = datetime.utcnow(),
78 user = user,
81 return True