diff --git a/codeforlife/request/__init__.py b/codeforlife/request/__init__.py new file mode 100644 index 0000000..a8ed8a0 --- /dev/null +++ b/codeforlife/request/__init__.py @@ -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 diff --git a/codeforlife/request.py b/codeforlife/request/drf.py similarity index 77% rename from codeforlife/request.py rename to codeforlife/request/drf.py index 39cd13f..4c30da0 100644 --- a/codeforlife/request.py +++ b/codeforlife/request/drf.py @@ -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: @@ -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 @@ -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) @@ -99,7 +85,7 @@ 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) @@ -107,7 +93,7 @@ def teacher_user(self): 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) @@ -115,7 +101,7 @@ def school_teacher_user(self): 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) @@ -125,7 +111,7 @@ 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) @@ -133,7 +119,7 @@ def non_admin_school_teacher_user(self): 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) @@ -141,7 +127,7 @@ def non_school_teacher_user(self): 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) @@ -149,6 +135,6 @@ def student_user(self): 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) diff --git a/codeforlife/request/http.py b/codeforlife/request/http.py new file mode 100644 index 0000000..70678f8 --- /dev/null +++ b/codeforlife/request/http.py @@ -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" diff --git a/codeforlife/request/wsgi.py b/codeforlife/request/wsgi.py new file mode 100644 index 0000000..b9764c8 --- /dev/null +++ b/codeforlife/request/wsgi.py @@ -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"