|
1 | 1 | from abc import ABC, abstractmethod |
2 | 2 | from collections.abc import Sequence |
3 | | -from typing import Any, ClassVar, TypeVar |
| 3 | +from typing import Any, TypeVar |
4 | 4 |
|
5 | 5 | import jwt |
6 | | -from fastapi import HTTPException, Request, status |
| 6 | +from fastapi import HTTPException, status |
7 | 7 | from fastapi_pagination import Page |
8 | 8 | from jwt import ExpiredSignatureError, InvalidSignatureError, InvalidTokenError |
9 | | -from pydantic import Field |
10 | | -from sqlalchemy import Result, Select, select |
| 9 | +from sqlalchemy import Select, select |
11 | 10 | from sqlalchemy.exc import NoResultFound |
12 | 11 | from sqlalchemy.ext.asyncio import AsyncSession |
13 | | -from sqlalchemy.orm import selectinload |
14 | | -from starlette.templating import _TemplateResponse |
15 | 12 |
|
16 | | -from futuramaapi.__version__ import __version__ |
17 | 13 | from futuramaapi.core import settings |
18 | 14 | from futuramaapi.helpers.pydantic import BaseModel |
19 | | -from futuramaapi.helpers.templates import templates |
20 | | -from futuramaapi.repositories.models import AuthSessionModel, UserModel |
| 15 | +from futuramaapi.repositories.models import UserModel |
21 | 16 | from futuramaapi.repositories.session import session_manager |
22 | | -from futuramaapi.utils import config, metadata |
23 | 17 |
|
24 | 18 | TResponse = TypeVar( |
25 | 19 | "TResponse", |
@@ -58,80 +52,6 @@ async def __call__(self, *args, **kwargs) -> TResponse: |
58 | 52 | return await self.process(*args, **kwargs) |
59 | 53 |
|
60 | 54 |
|
61 | | -class _ProjectContext(BaseModel): |
62 | | - version: str = Field( |
63 | | - default=__version__, |
64 | | - ) |
65 | | - g_tag: str = Field( |
66 | | - default=settings.g_tag, |
67 | | - alias="G_TAG", |
68 | | - ) |
69 | | - author: str | None = Field( |
70 | | - default=metadata.get("author", None), |
71 | | - ) |
72 | | - description: str = Field( |
73 | | - default=metadata["summary"], |
74 | | - ) |
75 | | - config: dict[str, Any] = config |
76 | | - |
77 | | - |
78 | | -_project_context: _ProjectContext = _ProjectContext() |
79 | | - |
80 | | - |
81 | | -class BaseTemplateService(BaseSessionService[_TemplateResponse], ABC): |
82 | | - template_name: ClassVar[str] |
83 | | - |
84 | | - _cookie_auth_key: ClassVar[str] = "Authorization" |
85 | | - |
86 | | - @abstractmethod |
87 | | - async def get_context(self, *args, **kwargs) -> dict[str, Any]: ... |
88 | | - |
89 | | - @property |
90 | | - def request(self) -> Request: |
91 | | - if self.context is None: |
92 | | - raise AttributeError("Request is not defined.") |
93 | | - |
94 | | - if "request" not in self.context: |
95 | | - raise AttributeError("Request is not defined.") |
96 | | - |
97 | | - return self.context["request"] |
98 | | - |
99 | | - @property |
100 | | - def __auth_session_statement(self) -> Select[tuple[AuthSessionModel]]: |
101 | | - statement: Select[tuple[AuthSessionModel]] = select(AuthSessionModel) |
102 | | - statement = statement.where(AuthSessionModel.key == self.request.cookies[self._cookie_auth_key]) |
103 | | - statement = statement.options(selectinload(AuthSessionModel.user)) |
104 | | - return statement |
105 | | - |
106 | | - async def _get_current_user(self) -> UserModel | None: |
107 | | - if self._cookie_auth_key not in self.request.cookies: |
108 | | - return None |
109 | | - |
110 | | - result: Result[tuple[AuthSessionModel]] = await self.session.execute(self.__auth_session_statement) |
111 | | - try: |
112 | | - auth_session: AuthSessionModel = result.scalars().one() |
113 | | - except NoResultFound: |
114 | | - return None |
115 | | - |
116 | | - if not auth_session.valid: |
117 | | - return None |
118 | | - |
119 | | - return auth_session.user |
120 | | - |
121 | | - async def _get_context(self) -> dict[str, Any]: |
122 | | - context: dict[str, Any] = await self.get_context() |
123 | | - context["_project"] = _project_context |
124 | | - context["current_user"] = await self._get_current_user() |
125 | | - return context |
126 | | - |
127 | | - async def process(self, *args, **kwargs) -> _TemplateResponse: |
128 | | - return templates.TemplateResponse( |
129 | | - self.request, |
130 | | - self.template_name, |
131 | | - context=await self._get_context(), |
132 | | - ) |
133 | | - |
134 | | - |
135 | 55 | class BaseUserAuthenticatedService[TResponse](BaseSessionService[TResponse], ABC): |
136 | 56 | token: str |
137 | 57 |
|
|
0 commit comments