Make account switching work

This commit is contained in:
Jason McBrayer 2019-04-23 22:29:47 -04:00
parent 3289adde09
commit 372422f9a3
2 changed files with 48 additions and 7 deletions

View File

@ -1,18 +1,18 @@
<article class="media box">
<figure class="media-left">
<p class="image is-64x64">
<img src="{{ account.avatar_static }}"
alt="{{ account.acct }}">
<img src="{{ account.user.avatar_static }}"
alt="{{ account.user.acct }}">
</p>
</figure>
<div class="media-content">
<strong>{{ account.display_name }}</strong> ({{ account.username }})
<strong>{{ account.user.display_name }}</strong> ({{ account.user.username }})
</div>
<div class="media-right">
<form method="POST" action="{% url "accounts" account.id %}">
<form method="POST" action="{% url "accounts" account.account_id %}">
{% csrf_token %}
<button class="button" name="activate">Activate</button>
<button class="button" name="forget">Forget</button>
<button class="button" name="activate" value="1">Activate</button>
<button class="button" name="forget" value="1">Forget</button>
</form>
</div>
</article>

View File

@ -195,8 +195,31 @@ def switch_accounts(request, new_account):
return False
request.session['active_user'] = accounts_dict[new_account]['user']
request.session['active_username'] = account.username
request.session['active_instance'] = account.client.api_base_id
return True
def forget_account(request, account_name):
"""Forget that you were logged into an account. If it's the last one,
log out entirely. Sets up session variables. Returns boolean success
code"""
accounts_dict = request.session.get("accounts_dict")
if not accounts_dict or not account_name in accounts_dict.keys():
return False
del accounts_dict[account_name]
if len(accounts) == 0:
request.session.flush()
return True
else:
key = accounts_dict.keys()[0]
request.session['active_user'] = accounts_dict[key]['user']
try:
account = Account.objects.get(id=accounts_dict[key]['account_id'])
request.session['active_username'] = account.username
except:
request.session.flush()
return True
###
### View functions
###
@ -1136,9 +1159,27 @@ def follow_requests(request, id=None):
def accounts(request, id=None):
active_account, mastodon = get_usercontext(request)
if request.method == 'GET':
accounts = [x['user'] for x in request.session.get('accounts_dict').values()]
accounts = [x for x in request.session.get('accounts_dict').values()]
return render(request, 'accounts/list.html',
{'active_account': active_account,
"own_acct": request.session["active_user"],
'accounts': accounts,
'preferences': active_account.preferences})
if request.method == 'POST':
if request.POST.get('activate'):
to_account = Account.objects.get(id=id).username
if switch_accounts(request, to_account):
return redirect(home)
else:
return redirect(accounts)
elif request.POST.get('forget'):
account = Account.objects.get(id=id).username
forget_account(request, account)
return redirect(accounts)
else:
accounts = [x for x in request.session.get('accounts_dict').values()]
return render(request, 'accounts/list.html',
{'active_account': active_account,
"own_acct": request.session["active_user"],
'accounts': accounts,
'preferences': active_account.preferences})