Skip to content

Commit

Permalink
split request objects
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Nov 5, 2024
1 parent 67e6b17 commit 9c0463d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 27 deletions.
8 changes: 8 additions & 0 deletions codeforlife/request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
© Ocado Group
Created on 05/11/2024 at 14:40:32(+00:00).
"""

from .drf import BaseRequest, Request
from .http import BaseHttpRequest, HttpRequest
from .wsgi import BaseWSGIRequest, WSGIRequest
40 changes: 13 additions & 27 deletions codeforlife/request.py → codeforlife/request/drf.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
"""
© Ocado Group
Created on 19/02/2024 at 15:28:22(+00:00).
Created on 05/11/2024 at 14:41:58(+00:00).
Override default request objects.
Custom Request which hints to our custom types.
"""

import typing as t

from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
from django.core.handlers.wsgi import WSGIRequest as _WSGIRequest
from django.http import HttpRequest as _HttpRequest
from rest_framework.request import Request as _Request

from .types import JsonDict, JsonList
from ..types import JsonDict, JsonList

# pylint: disable-next=duplicate-code
if t.TYPE_CHECKING:
from .user.models import User
from .user.models.session import SessionStore
from ..user.models import User
from ..user.models.session import SessionStore

AnyUser = t.TypeVar("AnyUser", bound=User)
else:
Expand All @@ -26,18 +24,6 @@
AnyAbstractBaseUser = t.TypeVar("AnyAbstractBaseUser", bound=AbstractBaseUser)


# pylint: disable-next=missing-class-docstring
class WSGIRequest(_WSGIRequest, t.Generic[AnyUser]):
session: "SessionStore"
user: t.Union[AnyUser, AnonymousUser]


# pylint: disable-next=missing-class-docstring
class HttpRequest(_HttpRequest, t.Generic[AnyUser]):
session: "SessionStore"
user: t.Union[AnyUser, AnonymousUser]


# pylint: disable-next=missing-class-docstring,abstract-method
class BaseRequest(_Request, t.Generic[AnyAbstractBaseUser]):
data: t.Any
Expand Down Expand Up @@ -83,7 +69,7 @@ def user(self):
@user.setter
def user(self, value):
# pylint: disable-next=import-outside-toplevel
from .user.models import User
from ..user.models import User

if (
isinstance(value, User)
Expand All @@ -99,23 +85,23 @@ def user(self, value):
def teacher_user(self):
"""The authenticated teacher-user that made the request."""
# pylint: disable-next=import-outside-toplevel
from .user.models import TeacherUser
from ..user.models import TeacherUser

return self.auth_user.as_type(TeacherUser)

@property
def school_teacher_user(self):
"""The authenticated school-teacher-user that made the request."""
# pylint: disable-next=import-outside-toplevel
from .user.models import SchoolTeacherUser
from ..user.models import SchoolTeacherUser

return self.auth_user.as_type(SchoolTeacherUser)

@property
def admin_school_teacher_user(self):
"""The authenticated admin-school-teacher-user that made the request."""
# pylint: disable-next=import-outside-toplevel
from .user.models import AdminSchoolTeacherUser
from ..user.models import AdminSchoolTeacherUser

return self.auth_user.as_type(AdminSchoolTeacherUser)

Expand All @@ -125,30 +111,30 @@ def non_admin_school_teacher_user(self):
The authenticated non-admin-school-teacher-user that made the request.
"""
# pylint: disable-next=import-outside-toplevel
from .user.models import NonAdminSchoolTeacherUser
from ..user.models import NonAdminSchoolTeacherUser

return self.auth_user.as_type(NonAdminSchoolTeacherUser)

@property
def non_school_teacher_user(self):
"""The authenticated non-school-teacher-user that made the request."""
# pylint: disable-next=import-outside-toplevel
from .user.models import NonSchoolTeacherUser
from ..user.models import NonSchoolTeacherUser

return self.auth_user.as_type(NonSchoolTeacherUser)

@property
def student_user(self):
"""The authenticated student-user that made the request."""
# pylint: disable-next=import-outside-toplevel
from .user.models import StudentUser
from ..user.models import StudentUser

return self.auth_user.as_type(StudentUser)

@property
def indy_user(self):
"""The authenticated independent-user that made the request."""
# pylint: disable-next=import-outside-toplevel
from .user.models import IndependentUser
from ..user.models import IndependentUser

return self.auth_user.as_type(IndependentUser)
32 changes: 32 additions & 0 deletions codeforlife/request/http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
© Ocado Group
Created on 05/11/2024 at 14:41:58(+00:00).
Custom HttpRequest which hints to our custom types.
"""

import typing as t

from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
from django.http import HttpRequest as _HttpRequest

# pylint: disable-next=duplicate-code
if t.TYPE_CHECKING:
from ..user.models import User
from ..user.models.session import SessionStore

AnyUser = t.TypeVar("AnyUser", bound=User)
else:
AnyUser = t.TypeVar("AnyUser")

AnyAbstractBaseUser = t.TypeVar("AnyAbstractBaseUser", bound=AbstractBaseUser)


# pylint: disable-next=missing-class-docstring
class BaseHttpRequest(_HttpRequest, t.Generic[AnyAbstractBaseUser]):
user: t.Union[AnyAbstractBaseUser, AnonymousUser]


# pylint: disable-next=missing-class-docstring
class HttpRequest(BaseHttpRequest[AnyUser], t.Generic[AnyUser]):
session: "SessionStore"
32 changes: 32 additions & 0 deletions codeforlife/request/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
© Ocado Group
Created on 05/11/2024 at 14:41:58(+00:00).
Custom WSGIRequest which hints to our custom types.
"""

import typing as t

from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
from django.core.handlers.wsgi import WSGIRequest as _WSGIRequest

# pylint: disable-next=duplicate-code
if t.TYPE_CHECKING:
from ..user.models import User
from ..user.models.session import SessionStore

AnyUser = t.TypeVar("AnyUser", bound=User)
else:
AnyUser = t.TypeVar("AnyUser")

AnyAbstractBaseUser = t.TypeVar("AnyAbstractBaseUser", bound=AbstractBaseUser)


# pylint: disable-next=missing-class-docstring
class BaseWSGIRequest(_WSGIRequest, t.Generic[AnyAbstractBaseUser]):
user: t.Union[AnyAbstractBaseUser, AnonymousUser]


# pylint: disable-next=missing-class-docstring
class WSGIRequest(BaseWSGIRequest[AnyUser], t.Generic[AnyUser]):
session: "SessionStore"

0 comments on commit 9c0463d

Please sign in to comment.