Rename some session variables

This is in preparation for adding the session dictionary of accounts
This commit is contained in:
Jason McBrayer 2019-04-23 18:06:57 -04:00
parent f76600ffed
commit d911da51bc

View File

@ -25,8 +25,8 @@ class NotLoggedInException(Exception):
def get_usercontext(request): def get_usercontext(request):
if is_logged_in(request): if is_logged_in(request):
try: try:
client = Client.objects.get(api_base_id=request.session['instance']) client = Client.objects.get(api_base_id=request.session['active_instance'])
user = Account.objects.get(username=request.session['username']) user = Account.objects.get(username=request.session['active_username'])
except (Client.DoesNotExist, Client.MultipleObjectsReturned, except (Client.DoesNotExist, Client.MultipleObjectsReturned,
Account.DoesNotExist, Account.MultipleObjectsReturned): Account.DoesNotExist, Account.MultipleObjectsReturned):
raise NotLoggedInException() raise NotLoggedInException()
@ -41,7 +41,7 @@ def get_usercontext(request):
return None, None return None, None
def is_logged_in(request): def is_logged_in(request):
return request.session.has_key('user') return request.session.has_key('active_user')
def _notes_count(account, mastodon): def _notes_count(account, mastodon):
if not mastodon: if not mastodon:
@ -113,13 +113,13 @@ def user_search_inner(request, query):
account, mastodon = get_usercontext(request) account, mastodon = get_usercontext(request)
results = mastodon.search(query) results = mastodon.search(query)
return render(request, 'intercooler/users.html', return render(request, 'intercooler/users.html',
{'users': "\n".join([ user.acct for user in results.accounts ]), {'active_users': "\n".join([ user.acct for user in results.accounts ]),
'preferences': account.preferences }) 'preferences': account.preferences })
def timeline(request, timeline='home', timeline_name='Home', max_id=None, since_id=None, filter_context='home'): def timeline(request, timeline='home', timeline_name='Home', max_id=None, since_id=None, filter_context='home'):
account, mastodon = get_usercontext(request) account, mastodon = get_usercontext(request)
data = mastodon.timeline(timeline, limit=40, max_id=max_id, since_id=since_id) data = mastodon.timeline(timeline, limit=40, max_id=max_id, since_id=since_id)
form = PostForm(initial={'visibility': request.session['user'].source.privacy}) form = PostForm(initial={'visibility': request.session['active_user'].source.privacy})
try: try:
prev = data[0]._pagination_prev prev = data[0]._pagination_prev
if len(mastodon.timeline(since_id=prev['since_id'])) == 0: if len(mastodon.timeline(since_id=prev['since_id'])) == 0:
@ -149,7 +149,7 @@ def timeline(request, timeline='home', timeline_name='Home', max_id=None, since_
return render(request, 'main/%s_timeline.html' % timeline, return render(request, 'main/%s_timeline.html' % timeline,
{'toots': data, 'form': form, 'timeline': timeline, {'toots': data, 'form': form, 'timeline': timeline,
'timeline_name': timeline_name, 'timeline_name': timeline_name,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences, 'preferences': account.preferences,
'notifications': notifications, 'notifications': notifications,
'prev': prev, 'next': next}) 'prev': prev, 'next': next})
@ -213,7 +213,7 @@ def tag(request, tag):
notifications = _notes_count(account, mastodon) notifications = _notes_count(account, mastodon)
return render(request, 'main/timeline.html', return render(request, 'main/timeline.html',
{'toots': data, 'timeline_name': '#'+tag, {'toots': data, 'timeline_name': '#'+tag,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'notifications': notifications, 'notifications': notifications,
'preferences': account.preferences}) 'preferences': account.preferences})
@ -235,12 +235,12 @@ def login(request):
if tmp_base.netloc == '': if tmp_base.netloc == '':
api_base_url = parse.urlunparse(('https', tmp_base.path, api_base_url = parse.urlunparse(('https', tmp_base.path,
'','','','')) '','','',''))
request.session['instance_hostname'] = tmp_base.path request.session['active_instance_hostname'] = tmp_base.path
else: else:
api_base_url = api_base_url.lower() api_base_url = api_base_url.lower()
request.session['instance_hostname'] = tmp_base.netloc request.session['active_instance_hostname'] = tmp_base.netloc
request.session['instance'] = api_base_url request.session['active_instance'] = api_base_url
try: try:
client = Client.objects.get(api_base_id=api_base_url) client = Client.objects.get(api_base_id=api_base_url)
except (Client.DoesNotExist, Client.MultipleObjectsReturned): except (Client.DoesNotExist, Client.MultipleObjectsReturned):
@ -254,8 +254,8 @@ def login(request):
client_secret = client_secret) client_secret = client_secret)
client.save() client.save()
request.session['client_id'] = client.client_id request.session['active_client_id'] = client.client_id
request.session['client_secret'] = client.client_secret request.session['active_client_secret'] = client.client_secret
mastodon = Mastodon( mastodon = Mastodon(
client_id = client.client_id, client_id = client.client_id,
@ -272,9 +272,9 @@ def login(request):
@never_cache @never_cache
def oauth_callback(request): def oauth_callback(request):
code = request.GET.get('code', '') code = request.GET.get('code', '')
mastodon = Mastodon(client_id=request.session['client_id'], mastodon = Mastodon(client_id=request.session['active_client_id'],
client_secret=request.session['client_secret'], client_secret=request.session['active_client_secret'],
api_base_url=request.session['instance']) api_base_url=request.session['active_instance'])
redirect_uri = request.build_absolute_uri(reverse('oauth_callback')) redirect_uri = request.build_absolute_uri(reverse('oauth_callback'))
access_token = mastodon.log_in(code=code, access_token = mastodon.log_in(code=code,
redirect_uri=redirect_uri, redirect_uri=redirect_uri,
@ -283,7 +283,7 @@ def oauth_callback(request):
user = mastodon.account_verify_credentials() user = mastodon.account_verify_credentials()
try: try:
account = Account.objects.get(username=user.username + '@' + account = Account.objects.get(username=user.username + '@' +
request.session['instance_hostname']) request.session['active_instance_hostname'])
account.access_token = access_token account.access_token = access_token
if not account.preferences: if not account.preferences:
preferences = Preference(theme = Theme.objects.get(id=1)) preferences = Preference(theme = Theme.objects.get(id=1))
@ -295,13 +295,13 @@ def oauth_callback(request):
except (Account.DoesNotExist, Account.MultipleObjectsReturned): except (Account.DoesNotExist, Account.MultipleObjectsReturned):
preferences = Preference(theme = Theme.objects.get(id=1)) preferences = Preference(theme = Theme.objects.get(id=1))
preferences.save() preferences.save()
account = Account(username=user.username + '@' + request.session['instance_hostname'], account = Account(username=user.username + '@' + request.session['active_instance_hostname'],
access_token = access_token, access_token = access_token,
client = Client.objects.get(api_base_id=request.session['instance']), client = Client.objects.get(api_base_id=request.session['active_instance']),
preferences = preferences) preferences = preferences)
account.save() account.save()
request.session['user'] = user request.session['active_user'] = user
request.session['username'] = user.username + '@' + request.session['instance_hostname'] request.session['active_username'] = user.username + '@' + request.session['active_instance_hostname']
return redirect(home) return redirect(home)
@ -318,12 +318,12 @@ def old_login(request):
if tmp_base.netloc == '': if tmp_base.netloc == '':
api_base_url = parse.urlunparse(('https', tmp_base.path, api_base_url = parse.urlunparse(('https', tmp_base.path,
'','','','')) '','','',''))
request.session['instance_hostname'] = tmp_base.path request.session['active_instance_hostname'] = tmp_base.path
else: else:
api_base_url = api_base_url.lower() api_base_url = api_base_url.lower()
request.session['instance_hostname'] = tmp_base.netloc request.session['active_instance_hostname'] = tmp_base.netloc
request.session['instance'] = api_base_url request.session['active_instance'] = api_base_url
email = form.cleaned_data['email'] email = form.cleaned_data['email']
password = form.cleaned_data['password'] password = form.cleaned_data['password']
@ -360,9 +360,9 @@ def old_login(request):
scopes=['read', 'write', 'follow']) scopes=['read', 'write', 'follow'])
account.access_token = access_token account.access_token = access_token
user = mastodon.account_verify_credentials() user = mastodon.account_verify_credentials()
request.session['user'] = user request.session['active_user'] = user
request.session['username'] = user.username + '@' + request.session['instance_hostname'] request.session['active_username'] = user.username + '@' + request.session['active_instance_hostname']
account.username = request.session['username'] account.username = request.session['active_username']
request.session['timezone'] = account.preferences.timezone; request.session['timezone'] = account.preferences.timezone;
account.save() account.save()
return redirect(home) return redirect(home)
@ -413,7 +413,7 @@ def note(request, next=None, prev=None):
return render(request, 'main/notifications.html', return render(request, 'main/notifications.html',
{'notes': notes,'timeline': 'Notifications', {'notes': notes,'timeline': 'Notifications',
'timeline_name': 'Notifications', 'timeline_name': 'Notifications',
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences, 'preferences': account.preferences,
'prev': prev, 'next': next}) 'prev': prev, 'next': next})
@ -437,7 +437,7 @@ def thread(request, id):
{'context': context, 'toot': toot, {'context': context, 'toot': toot,
'ancestors': ancestors, 'ancestors': ancestors,
'descendants': descendants, 'descendants': descendants,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'notifications': notifications, 'notifications': notifications,
'preferences': account.preferences}) 'preferences': account.preferences})
@ -469,9 +469,9 @@ def user(request, username, prev=None, next=None):
except (IndexError, AttributeError): except (IndexError, AttributeError):
next = None next = None
return render(request, 'main/user.html', return render(request, 'main/user.html',
{'toots': data, 'user': user_dict, {'toots': data, 'active_user': user_dict,
'relationship': relationship, 'relationship': relationship,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences, 'preferences': account.preferences,
'notifications': notifications, 'notifications': notifications,
'prev': prev, 'next': next}) 'prev': prev, 'next': next})
@ -503,7 +503,7 @@ def settings(request):
# Update this here because it's a handy place to do it. # Update this here because it's a handy place to do it.
user_info = mastodon.account_verify_credentials() user_info = mastodon.account_verify_credentials()
request.session['user'] = user_info request.session['active_user'] = user_info
return redirect(home) return redirect(home)
@ -526,19 +526,19 @@ def toot(request, mention=None):
if mention: if mention:
if not mention.startswith('@'): if not mention.startswith('@'):
mention = '@'+mention mention = '@'+mention
form = PostForm(initial={'visibility': request.session['user'].source.privacy, form = PostForm(initial={'visibility': request.session['active_user'].source.privacy,
'status': mention + ' ' }) 'status': mention + ' ' })
else: else:
form = PostForm(initial={'visibility': request.session['user'].source.privacy}) form = PostForm(initial={'visibility': request.session['active_user'].source.privacy})
if request.GET.get('ic-request'): if request.GET.get('ic-request'):
return render(request, 'intercooler/post.html', return render(request, 'intercooler/post.html',
{'form': form, {'form': form,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
else: else:
return render(request, 'main/post.html', return render(request, 'main/post.html',
{'form': form, {'form': form,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
elif request.method == 'POST': elif request.method == 'POST':
form = PostForm(request.POST, request.FILES) form = PostForm(request.POST, request.FILES)
@ -554,7 +554,7 @@ def toot(request, mention=None):
+str(index), +str(index),
None))) None)))
if form.cleaned_data['visibility'] == '': if form.cleaned_data['visibility'] == '':
form.cleaned_data['visibility'] = request.session['user'].source.privacy form.cleaned_data['visibility'] = request.session['active_user'].source.privacy
try: try:
try: try:
mastodon.status_post(status=form.cleaned_data['status'], mastodon.status_post(status=form.cleaned_data['status'],
@ -573,13 +573,13 @@ def toot(request, mention=None):
+ len(form.cleaned_data['spoiler_text']))) + len(form.cleaned_data['spoiler_text'])))
return render(request, 'main/post.html', return render(request, 'main/post.html',
{'form': form, {'form': form,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
return redirect(home) return redirect(home)
else: else:
return render(request, 'main/post.html', return render(request, 'main/post.html',
{'form': form, {'form': form,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
else: else:
return redirect(toot) return redirect(toot)
@ -608,7 +608,7 @@ def redraft(request, id):
}) })
return render(request, 'main/redraft.html', return render(request, 'main/redraft.html',
{'toot': toot, 'form': form, 'redraft':True, {'toot': toot, 'form': form, 'redraft':True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
elif request.method == 'POST': elif request.method == 'POST':
form = PostForm(request.POST, request.FILES) form = PostForm(request.POST, request.FILES)
@ -625,7 +625,7 @@ def redraft(request, id):
+str(index), +str(index),
None))) None)))
if form.cleaned_data['visibility'] == '': if form.cleaned_data['visibility'] == '':
form.cleaned_data['visibility'] = request.session['user'].source.privacy form.cleaned_data['visibility'] = request.session['active_user'].source.privacy
try: try:
try: try:
mastodon.status_post(status=form.cleaned_data['status'], mastodon.status_post(status=form.cleaned_data['status'],
@ -647,13 +647,13 @@ def redraft(request, id):
+ len(form.cleaned_data['spoiler_text']))) + len(form.cleaned_data['spoiler_text'])))
return render(request, 'main/redraft.html', return render(request, 'main/redraft.html',
{'toot': toot, 'form': form, 'redraft': True, {'toot': toot, 'form': form, 'redraft': True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
return redirect(home) return redirect(home)
else: else:
return render(request, 'main/redraft.html', return render(request, 'main/redraft.html',
{'toot': toot, 'form': form, 'redraft': True, {'toot': toot, 'form': form, 'redraft': True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
else: else:
return redirect(redraft, id) return redirect(redraft, id)
@ -677,12 +677,12 @@ def reply(request, id):
toot = mastodon.status(id) toot = mastodon.status(id)
context = mastodon.status_context(id) context = mastodon.status_context(id)
notifications = _notes_count(account, mastodon) notifications = _notes_count(account, mastodon)
if toot.account.acct != request.session['user'].acct: if toot.account.acct != request.session['active_user'].acct:
initial_text = '@' + toot.account.acct + " " initial_text = '@' + toot.account.acct + " "
else: else:
initial_text = "" initial_text = ""
for mention in [x for x in toot.mentions for mention in [x for x in toot.mentions
if x.acct != request.session['user'].acct and if x.acct != request.session['active_user'].acct and
x.acct != toot.account.acct]: x.acct != toot.account.acct]:
initial_text +=('@' + mention.acct + " ") initial_text +=('@' + mention.acct + " ")
form = PostForm(initial={'status': initial_text, form = PostForm(initial={'status': initial_text,
@ -690,7 +690,7 @@ def reply(request, id):
'spoiler_text': toot.spoiler_text}) 'spoiler_text': toot.spoiler_text})
return render(request, 'main/reply.html', return render(request, 'main/reply.html',
{'context': context, 'toot': toot, 'form': form, 'reply':True, {'context': context, 'toot': toot, 'form': form, 'reply':True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'notifications': notifications, 'notifications': notifications,
'preferences': account.preferences}) 'preferences': account.preferences})
elif request.method == 'POST': elif request.method == 'POST':
@ -730,14 +730,14 @@ def reply(request, id):
+ len(form.cleaned_data['spoiler_text']))) + len(form.cleaned_data['spoiler_text'])))
return render(request, 'main/reply.html', return render(request, 'main/reply.html',
{'context': context, 'toot': toot, 'form': form, 'reply': True, {'context': context, 'toot': toot, 'form': form, 'reply': True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'notifications': notifications, 'notifications': notifications,
'preferences': account.preferences}) 'preferences': account.preferences})
return HttpResponseRedirect(reverse('thread', args=[id]) + "#toot-"+str(id)) return HttpResponseRedirect(reverse('thread', args=[id]) + "#toot-"+str(id))
else: else:
return render(request, 'main/reply.html', return render(request, 'main/reply.html',
{'context': context, 'toot': toot, 'form': form, 'reply': True, {'context': context, 'toot': toot, 'form': form, 'reply': True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
else: else:
return HttpResponseRedirect(reverse('reply', args=[id]) + "#toot-"+str(id)) return HttpResponseRedirect(reverse('reply', args=[id]) + "#toot-"+str(id))
@ -758,14 +758,14 @@ def fav(request, id):
toot['favourited'] = not toot['favourited'] toot['favourited'] = not toot['favourited']
return render(request, 'intercooler/fav.html', return render(request, 'intercooler/fav.html',
{"toot": toot, {"toot": toot,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
"preferences": account.preferences}) "preferences": account.preferences})
else: else:
return HttpResponseRedirect(reverse('thread', args=[id]) + "#toot-"+str(id)) return HttpResponseRedirect(reverse('thread', args=[id]) + "#toot-"+str(id))
else: else:
return render(request, 'main/fav.html', return render(request, 'main/fav.html',
{"toot": toot, {"toot": toot,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
"confirm_page": True, "confirm_page": True,
'preferences': account.preferences}) 'preferences': account.preferences})
@ -784,14 +784,14 @@ def boost(request, id):
toot['reblogged'] = not toot['reblogged'] toot['reblogged'] = not toot['reblogged']
return render(request, 'intercooler/boost.html', return render(request, 'intercooler/boost.html',
{"toot": toot, {"toot": toot,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
"preferences": account.preferences}) "preferences": account.preferences})
else: else:
return HttpResponseRedirect(reverse('thread', args=[id]) + "#toot-"+str(id)) return HttpResponseRedirect(reverse('thread', args=[id]) + "#toot-"+str(id))
else: else:
return render(request, 'main/boost.html', return render(request, 'main/boost.html',
{"toot": toot, {"toot": toot,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'confirm_page': True, 'confirm_page': True,
"preferences": account.preferences}) "preferences": account.preferences})
@ -801,7 +801,7 @@ def delete(request, id):
account, mastodon = get_usercontext(request) account, mastodon = get_usercontext(request)
toot = mastodon.status(id) toot = mastodon.status(id)
if request.method == 'POST' or request.method == 'DELETE': if request.method == 'POST' or request.method == 'DELETE':
if toot.account.acct != request.session['user'].acct: if toot.account.acct != request.session['active_user'].acct:
return redirect('home') return redirect('home')
if not request.POST.get('cancel', None): if not request.POST.get('cancel', None):
mastodon.status_delete(id) mastodon.status_delete(id)
@ -811,7 +811,7 @@ def delete(request, id):
else: else:
return render(request, 'main/delete.html', return render(request, 'main/delete.html',
{"toot": toot, {"toot": toot,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'confirm_page': True, 'confirm_page': True,
"preferences": account.preferences}) "preferences": account.preferences})
@ -835,7 +835,7 @@ def follow(request, id):
relationship = mastodon.account_relationships(user_dict.id)[0] relationship = mastodon.account_relationships(user_dict.id)[0]
return render(request, 'intercooler/follow.html', return render(request, 'intercooler/follow.html',
{"user": user_dict, "relationship": relationship, {"user": user_dict, "relationship": relationship,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
else: else:
return redirect(user, user_dict.acct) return redirect(user, user_dict.acct)
@ -843,7 +843,7 @@ def follow(request, id):
return render(request, 'main/follow.html', return render(request, 'main/follow.html',
{"user": user_dict, "relationship": relationship, {"user": user_dict, "relationship": relationship,
"confirm_page": True, "confirm_page": True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
@never_cache @never_cache
@ -873,7 +873,7 @@ def block(request, id):
return render(request, 'main/block.html', return render(request, 'main/block.html',
{"user": user_dict, "relationship": relationship, {"user": user_dict, "relationship": relationship,
"confirm_page": True, "confirm_page": True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
@never_cache @never_cache
@ -903,7 +903,7 @@ def mute(request, id):
return render(request, 'main/mute.html', return render(request, 'main/mute.html',
{"user": user_dict, "relationship": relationship, {"user": user_dict, "relationship": relationship,
"confirm_page": True, "confirm_page": True,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'preferences': account.preferences}) 'preferences': account.preferences})
@br_login_required @br_login_required
@ -912,12 +912,12 @@ def search(request):
if request.GET.get('ic-request'): if request.GET.get('ic-request'):
return render(request, 'intercooler/search.html', return render(request, 'intercooler/search.html',
{"preferences": account.preferences, {"preferences": account.preferences,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
}) })
else: else:
return render(request, 'main/search.html', return render(request, 'main/search.html',
{"preferences": account.preferences, {"preferences": account.preferences,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
}) })
@br_login_required @br_login_required
@ -934,7 +934,7 @@ def search_results(request):
notifications = _notes_count(account, mastodon) notifications = _notes_count(account, mastodon)
return render(request, 'main/search_results.html', return render(request, 'main/search_results.html',
{"results": results, {"results": results,
'own_acct': request.session['user'], 'own_acct': request.session['active_user'],
'notifications': notifications, 'notifications': notifications,
"preferences": account.preferences}) "preferences": account.preferences})
@ -949,7 +949,7 @@ def about(request):
return render(request, 'about.html', return render(request, 'about.html',
{"preferences": preferences, {"preferences": preferences,
"version": version, "version": version,
'own_acct': request.session.get('user', None), 'own_acct': request.session.get('active_user', None),
}) })
@cache_page(60*30) @cache_page(60*30)
def privacy(request): def privacy(request):
@ -960,7 +960,7 @@ def privacy(request):
preferences = None preferences = None
return render(request, 'privacy.html', return render(request, 'privacy.html',
{"preferences": preferences, {"preferences": preferences,
'own_acct' : request.session.get('user', None)}) 'own_acct' : request.session.get('active_user', None)})
@cache_page(60 * 30) @cache_page(60 * 30)
@br_login_required @br_login_required
@ -972,7 +972,7 @@ def emoji_reference(request):
{"preferences": account.preferences, {"preferences": account.preferences,
"emojos": sorted(emojos, key=lambda x: x['shortcode']), "emojos": sorted(emojos, key=lambda x: x['shortcode']),
"notifications": notifications, "notifications": notifications,
'own_acct' : request.session['user']}) 'own_acct' : request.session['active_user']})
@br_login_required @br_login_required