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')
29 episode
, podcast
= thing
, thing
.podcast
32 raise NotImplemented(_("Can't flattr a '%s'") % thing_type
)
35 if not thing
.flattr_url
:
36 return False, _('No Payment URL available')
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
)
48 HistoryEntry
.objects
.create(
49 timestamp
=datetime
.utcnow(),
54 action
=HistoryEntry
.FLATTR
,
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')
72 episode
= Episode
.objects
.get(id=episode_id
)
74 EpisodeHistoryEntry
.objects
.create(
76 action
= EpisodeHistoryEntry
.FLATTR
,
77 timestamp
= datetime
.utcnow(),