From 51cb1c42fe54b6c6f1f8443cadb5f0c6639ebbb8 Mon Sep 17 00:00:00 2001 From: Jason McBrayer Date: Wed, 25 Apr 2018 20:51:12 -0400 Subject: [PATCH] Refactor some more common stuff for views --- brutaldon/views.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/brutaldon/views.py b/brutaldon/views.py index 6f52fe1..a0771ab 100644 --- a/brutaldon/views.py +++ b/brutaldon/views.py @@ -5,17 +5,20 @@ from brutaldon.models import Client, Account from mastodon import Mastodon from urllib import parse -def timeline(request, timeline='home', timeline_name='Home'): +class NotLoggedInException(Exception): + pass + +def get_mastodon(request): if not (request.session.has_key('instance') and request.session.has_key('username')): - return redirect(login) + raise NotLoggedInException() try: client = Client.objects.get(api_base_id=request.session['instance']) user = Account.objects.get(username=request.session['username']) except (Client.DoesNotExist, Client.MultipleObjectsReturned, Account.DoesNotExist, Account.MultipleObjectsReturned): - return redirect(login) + raise NotLoggedInException() mastodon = Mastodon( client_id = client.client_id, @@ -23,6 +26,13 @@ def timeline(request, timeline='home', timeline_name='Home'): access_token = user.access_token, api_base_url = client.api_base_id, ratelimit_method="pace") + return mastodon + +def timeline(request, timeline='home', timeline_name='Home'): + try: + mastodon = get_mastodon(request) + except NotLoggedInException: + return redirect(login) data = mastodon.timeline(timeline) if request.session.has_key('fullbrutalism'):