replace django.core.urlresolvers w/ django.urls
[mygpo.git] / mygpo / core / tasks.py
blobae45f78e776bc41af9972a617f27aad4aead0da0
1 from datetime import datetime
3 from django.utils.translation import ugettext as _
4 from django.conf import settings
5 from django.contrib.auth import get_user_model
7 from mygpo.podcasts.models import Podcast, Episode
8 from mygpo.celery import celery
9 from mygpo.history.models import HistoryEntry
10 from mygpo.flattr import Flattr
11 from mygpo.history.models import EpisodeHistoryEntry
14 User = get_user_model()
17 @celery.task(max_retries=5, default_retry_delay=60)
18 def flattr_thing(user_id, thing_id, domain, is_secure, thing_type):
19 """ Task to flattr a thing """
21 user = User.objects.get(pk=user_id)
22 flattr = Flattr(user, domain, is_secure)
24 if thing_type == 'Podcast':
25 thing = Podcast.objects.get(id=thing_id)
26 episode, podcast = None, thing
28 elif thing_type == 'Episode':
29 query = Episode.objects.filter(id=thing_id).select_related('podcast')
30 thing = query.get()
31 episode, podcast = thing, thing.podcast
33 else:
34 raise NotImplemented(_("Can't flattr a '%s'") % thing_type)
37 if not thing.flattr_url:
38 return False, _('No Payment URL available')
40 try:
41 success, msg = flattr.flattr_url(thing.flattr_url)
43 if settings.FLATTR_MYGPO_THING:
44 flattr.flattr_url(settings.FLATTR_MYGPO_THING)
46 except Exception as ex:
47 raise flattr_thing.retry(exc=ex)
49 if success:
50 HistoryEntry.objects.create(
51 timestamp=datetime.utcnow(),
52 podcast=podcast,
53 episode=episode,
54 user=user,
55 client=None,
56 action=HistoryEntry.FLATTR,
60 return success, msg
63 @celery.task(max_retries=5, default_retry_delay=60)
64 def auto_flattr_episode(user_id, episode_id):
65 """ Task to auto-flattr an episode
67 In addition to the flattring itself, it also records the event """
69 success, msg = flattr_thing(user_id, episode_id, None, False, 'Episode')
71 if not success:
72 return False
74 episode = Episode.objects.get(id=episode_id)
76 user = User.objects.get(pk=user_id)
77 EpisodeHistoryEntry.create_entry(user, episode, EpisodeHistoryEntry.FLATTR)
78 return True