feat(user): Add a type guard to extract the user from a request
This commit is contained in:
parent
a9d369d55d
commit
e370977aac
2 changed files with 10 additions and 2 deletions
|
@ -5,6 +5,7 @@ from typing import Optional, Self
|
|||
from asgiref.sync import async_to_sync
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
from django.http import HttpRequest
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from kanidm.exceptions import NoMatchingEntries
|
||||
from kanidm.models.person import Person
|
||||
|
@ -87,6 +88,14 @@ class User(AbstractUser):
|
|||
)
|
||||
# accepted_terms = models.ManyToManyField(TermsAndConditions)
|
||||
|
||||
@classmethod
|
||||
def from_request(cls, request: HttpRequest) -> Self:
|
||||
u = request.user
|
||||
|
||||
assert isinstance(u, cls)
|
||||
|
||||
return u
|
||||
|
||||
@cached_property
|
||||
@async_to_sync
|
||||
async def kanidm(self) -> Optional[KanidmProfile]:
|
||||
|
|
|
@ -61,8 +61,7 @@ class ProfileView(LoginRequiredMixin, TemplateView):
|
|||
template_name = "dgsi/profile.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
u = self.request.user
|
||||
assert isinstance(u, User)
|
||||
u = User.from_request(self.request)
|
||||
|
||||
return super().get_context_data(
|
||||
displayname=f"{u.first_name} {u.last_name}",
|
||||
|
|
Loading…
Reference in a new issue