diff --git a/app/reddit/tasks.py b/app/reddit/tasks.py index b089df4..7910ea5 100644 --- a/app/reddit/tasks.py +++ b/app/reddit/tasks.py @@ -83,11 +83,22 @@ def queue_account_updates(update_delay=604800, batch_size=50): update_account.delay(username=acc.pk) -@task(ignore_result=True) -def update_user_flair(username, character_name): - try: - ib = Flair(username=settings.REDDIT_USER, password=settings.REDDIT_PASSWORD) - ib.set_flair(settings.REDDIT_SUBREDDIT, username, character_name, '') - except LoginError, exc: - logger.error("Error logging into Reddit") +class update_user_flair(Task): + """ + Updates a user's flair on Reddit + """ + + default_retry_delay = 5 * 60 # retry in 5 minutes + ignore_result = True + + def run(self, username, character_name, **kwargs): + try: + ib = Flair(username=settings.REDDIT_USER, password=settings.REDDIT_PASSWORD) + ib.set_flair(settings.REDDIT_SUBREDDIT, username, character_name, '') + except (HTTPError, URLError), exc: + logger.error("Error updating flair, queueing for retry") + update_user_flair.retry(args=[username, character_name], kwargs=kwargs, exc=exc) + pass + except LoginError, exc: + logger.error("Error logging into Reddit") diff --git a/app/sso/views.py b/app/sso/views.py index a64fdd7..286385b 100644 --- a/app/sso/views.py +++ b/app/sso/views.py @@ -293,18 +293,22 @@ def primarychar_change(request): @login_required def toggle_reddit_tagging(request): profile = request.user.get_profile() - profile.tag_reddit_accounts = not profile.tag_reddit_accounts - profile.save() - if profile.tag_reddit_accounts: - tag = 'Enabled' - else: - tag = 'Disabled' - messages.add_message(request, messages.INFO, "Reddit account tagging is now %s" % tag) + if profile.primary_character: + profile.tag_reddit_accounts = not profile.tag_reddit_accounts + profile.save() + if profile.tag_reddit_accounts: + tag = 'Enabled' + else: + tag = 'Disabled' + messages.add_message(request, messages.INFO, "Reddit account tagging is now %s" % tag) - if profile.tag_reddit_accounts: - name = profile.primary_character.name + if profile.tag_reddit_accounts: + name = profile.primary_character.name + else: + name = '' + for acc in request.user.redditaccount_set.all(): + update_user_flair.delay(acc.username, name) else: - name = '' - for acc in request.user.redditaccount_set.all(): - update_user_flair.delay(acc.username, name) + messages.add_message(request, messages.ERROR, "You need to set a primary character before using this feature!") + return redirect('sso.views.profile')