remove gevent monkey-patch from feed-downloader
[mygpo.git] / mygpo / core / tasks.py
blob6c97441520a461e170d8dc1a249dc556f8865455
1 from datetime import datetime
3 from django.utils.translation import ugettext as _
4 from django.conf import settings
6 from mygpo.cel import celery
7 from mygpo.data.feeddownloader import PodcastUpdater
8 from mygpo.utils import get_timestamp
9 from mygpo.users.models import EpisodeAction
10 from mygpo.flattr import Flattr
11 from mygpo.db.couchdb.podcast import podcast_by_id
12 from mygpo.db.couchdb.episode import episode_by_id
13 from mygpo.db.couchdb.episode_state import episode_state_for_user_episode, \
14 add_episode_actions
17 @celery.task(max_retries=5, default_retry_delay=60)
18 def flattr_thing(user, thing_id, domain, is_secure, thing_type):
19 """ Task to flattr a thing """
21 flattr = Flattr(user, domain, is_secure)
23 if thing_type == 'Podcast':
24 thing = podcast_by_id(thing_id)
26 elif thing_type == 'Episode':
27 thing = episode_by_id(thing_id)
29 else:
30 raise NotImplemented(_("Can't flattr a '%s'") % thing_type)
33 if not thing.flattr_url:
34 return False, _('No Payment URL available')
36 try:
37 success, msg = flattr.flattr_url(thing.flattr_url)
39 if settings.FLATTR_MYGPO_THING:
40 flattr.flattr_url(settings.FLATTR_MYGPO_THING)
42 except Exception as ex:
43 raise flattr_thing.retry(exc=ex)
45 return success, msg
48 @celery.task(max_retries=5, default_retry_delay=60)
49 def auto_flattr_episode(user, episode_id):
50 """ Task to auto-flattr an episode
52 In addition to the flattring itself, it also records the event """
54 success, msg = flattr_thing(user, episode_id, None, False, 'Episode')
56 if not success:
57 return False
59 episode = episode_by_id(episode_id)
60 state = episode_state_for_user_episode(user, episode)
62 action = EpisodeAction()
63 action.action = 'flattr'
64 action.upload_timestamp = get_timestamp(datetime.utcnow())
65 add_episode_actions(state, [action])
67 return True