Refactor some more common stuff for views

This commit is contained in:
Jason McBrayer 2018-04-25 20:51:12 -04:00
parent e6f2734208
commit 51cb1c42fe
1 changed files with 13 additions and 3 deletions

View File

@ -5,17 +5,20 @@ from brutaldon.models import Client, Account
from mastodon import Mastodon from mastodon import Mastodon
from urllib import parse 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 if not (request.session.has_key('instance') and
request.session.has_key('username')): request.session.has_key('username')):
return redirect(login) raise NotLoggedInException()
try: try:
client = Client.objects.get(api_base_id=request.session['instance']) client = Client.objects.get(api_base_id=request.session['instance'])
user = Account.objects.get(username=request.session['username']) user = Account.objects.get(username=request.session['username'])
except (Client.DoesNotExist, Client.MultipleObjectsReturned, except (Client.DoesNotExist, Client.MultipleObjectsReturned,
Account.DoesNotExist, Account.MultipleObjectsReturned): Account.DoesNotExist, Account.MultipleObjectsReturned):
return redirect(login) raise NotLoggedInException()
mastodon = Mastodon( mastodon = Mastodon(
client_id = client.client_id, client_id = client.client_id,
@ -23,6 +26,13 @@ def timeline(request, timeline='home', timeline_name='Home'):
access_token = user.access_token, access_token = user.access_token,
api_base_url = client.api_base_id, api_base_url = client.api_base_id,
ratelimit_method="pace") 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) data = mastodon.timeline(timeline)
if request.session.has_key('fullbrutalism'): if request.session.has_key('fullbrutalism'):