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 asgiref.sync import async_to_sync
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.http import HttpRequest
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from kanidm.exceptions import NoMatchingEntries
|
from kanidm.exceptions import NoMatchingEntries
|
||||||
from kanidm.models.person import Person
|
from kanidm.models.person import Person
|
||||||
|
@ -87,6 +88,14 @@ class User(AbstractUser):
|
||||||
)
|
)
|
||||||
# accepted_terms = models.ManyToManyField(TermsAndConditions)
|
# accepted_terms = models.ManyToManyField(TermsAndConditions)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_request(cls, request: HttpRequest) -> Self:
|
||||||
|
u = request.user
|
||||||
|
|
||||||
|
assert isinstance(u, cls)
|
||||||
|
|
||||||
|
return u
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
@async_to_sync
|
@async_to_sync
|
||||||
async def kanidm(self) -> Optional[KanidmProfile]:
|
async def kanidm(self) -> Optional[KanidmProfile]:
|
||||||
|
|
|
@ -61,8 +61,7 @@ class ProfileView(LoginRequiredMixin, TemplateView):
|
||||||
template_name = "dgsi/profile.html"
|
template_name = "dgsi/profile.html"
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
u = self.request.user
|
u = User.from_request(self.request)
|
||||||
assert isinstance(u, User)
|
|
||||||
|
|
||||||
return super().get_context_data(
|
return super().get_context_data(
|
||||||
displayname=f"{u.first_name} {u.last_name}",
|
displayname=f"{u.first_name} {u.last_name}",
|
||||||
|
|
Loading…
Reference in a new issue