Include profile context var for loggued users.

This commit is contained in:
Santiago Lamora 2019-12-10 13:04:04 +01:00
parent c464c7bb28
commit 5abdcd56db
4 changed files with 22 additions and 6 deletions

View File

@ -4,6 +4,9 @@ import urllib.parse
from django.conf import settings
from django.urls.exceptions import NoReverseMatch
from .models import UserAccount
DOMAINS_PATH = 'domains/'
TOKEN_PATH = '/api-token-auth/'
@ -75,9 +78,12 @@ class Orchestra(object):
_, output = self.request("GET", pattern_name)
return output
def retreve_profile(self):
_, output = self.request("GET", 'my-account')
return output
def retrieve_profile(self):
status, output = self.request("GET", 'my-account')
if status >= 400:
raise PermissionError("Cannot retrieve profile of an anonymous user.")
return UserAccount.new_from_json(output[0])
def verify_credentials(self):
"""

View File

@ -46,6 +46,11 @@ class ExtendedPaginationMixin:
class UserTokenRequiredMixin(UserPassesTestMixin):
"""
Checks that the request has a token that authenticates him/her.
If the user is logged adds context variable 'profile' with its information.
"""
def test_func(self):
"""Check that the user has an authorized token."""
token = self.request.session.get(SESSION_KEY_TOKEN, None)
@ -60,3 +65,10 @@ class UserTokenRequiredMixin(UserPassesTestMixin):
return False
return True
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'profile': self.orchestra.retrieve_profile(),
})
return context

View File

@ -59,7 +59,7 @@
<div class="dropdown dropright">
<button type="button" class="btn btn-primary nav-link text-light w-100" data-toggle="dropdown">
<img id="user-avatar" class="float-right" width="64" height="64" src="{% static "musician/images/default-profile-picture.png" %}" alt="user-profile-picture"/>
<strong>{{ user.username|default:"Username" }}</strong><br/>
<strong>{{ profile.username }}</strong><br/>
<i class="fas fa-cog"></i> Settings
</button>
<div class="dropdown-menu">

View File

@ -60,14 +60,12 @@ class ProfileView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
json_data = self.orchestra.retreve_profile()
try:
pay_source = self.orchestra.retrieve_service_list(
PaymentSource.api_name)[0]
except IndexError:
pay_source = {}
context.update({
'profile': UserAccount.new_from_json(json_data[0]),
'payment': PaymentSource.new_from_json(pay_source)
})