rb-commons 0.1.16__py3-none-any.whl → 0.1.18__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rb_commons/configs/injections.py +1 -1
- rb_commons/http/__init__.py +0 -0
- rb_commons/http/exceptions.py +41 -0
- rb_commons/orm/managers.py +39 -8
- rb_commons/permissions/__init__.py +0 -1
- rb_commons/permissions/role_permissions.py +3 -2
- {rb_commons-0.1.16.dist-info → rb_commons-0.1.18.dist-info}/METADATA +1 -1
- rb_commons-0.1.18.dist-info/RECORD +17 -0
- rb_commons-0.1.16.dist-info/RECORD +0 -15
- {rb_commons-0.1.16.dist-info → rb_commons-0.1.18.dist-info}/WHEEL +0 -0
- {rb_commons-0.1.16.dist-info → rb_commons-0.1.18.dist-info}/top_level.txt +0 -0
rb_commons/configs/injections.py
CHANGED
File without changes
|
@@ -0,0 +1,41 @@
|
|
1
|
+
from fastapi import HTTPException, status
|
2
|
+
|
3
|
+
|
4
|
+
class CustomHTTPException(HTTPException):
|
5
|
+
def __init__(self, message: str, status: int, code: str = None, additional_info: dict = None):
|
6
|
+
super().__init__(status_code=status, detail=message)
|
7
|
+
|
8
|
+
self.code = code
|
9
|
+
self.message = message
|
10
|
+
self.status = status
|
11
|
+
self.additional_info = additional_info
|
12
|
+
|
13
|
+
|
14
|
+
class NotAuthorizedException(CustomHTTPException):
|
15
|
+
def __init__(self, message: str = "You are not authorized", status: int = 401, code: str = None,
|
16
|
+
additional_info: dict = None):
|
17
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
18
|
+
|
19
|
+
|
20
|
+
class ForbiddenException(CustomHTTPException):
|
21
|
+
def __init__(self, message: str = "Permission denied", status: int = 403, code: str = None,
|
22
|
+
additional_info: dict = None):
|
23
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
24
|
+
|
25
|
+
|
26
|
+
class BadRequestException(CustomHTTPException):
|
27
|
+
def __init__(self, message: str = "Bad request", status: int = 400, code: str = None,
|
28
|
+
additional_info: dict = None):
|
29
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
30
|
+
|
31
|
+
|
32
|
+
class NotFoundException(CustomHTTPException):
|
33
|
+
def __init__(self, message: str = "Not found", status: int = 404, code: str = None,
|
34
|
+
additional_info: dict = None):
|
35
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
36
|
+
|
37
|
+
|
38
|
+
class InternalException(CustomHTTPException):
|
39
|
+
def __init__(self, message: str = "Internal exception", status: int = 500, code: str = None,
|
40
|
+
additional_info: dict = None):
|
41
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
rb_commons/orm/managers.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
|
1
|
+
import uuid
|
2
|
+
from typing import TypeVar, Type, Generic, Optional, List, Dict, Literal, Union
|
2
3
|
from sqlalchemy import select, delete, update
|
3
4
|
from sqlalchemy.exc import IntegrityError, SQLAlchemyError, NoResultFound
|
4
5
|
from sqlalchemy.ext.asyncio import AsyncSession
|
5
6
|
from sqlalchemy.orm import declarative_base
|
7
|
+
|
8
|
+
from rb_commons.http.exceptions import NotFoundException
|
6
9
|
from rb_commons.orm.exceptions import DatabaseException, InternalException
|
7
10
|
|
8
11
|
ModelType = TypeVar('ModelType', bound=declarative_base())
|
@@ -12,27 +15,55 @@ class BaseManager(Generic[ModelType]):
|
|
12
15
|
|
13
16
|
def __init__(self, session: AsyncSession) -> None:
|
14
17
|
self.session = session
|
18
|
+
self.data = None
|
19
|
+
self.filters = {}
|
15
20
|
|
16
|
-
async def get(self,
|
21
|
+
async def get(self, pk: Union[str, int, uuid.UUID]) -> Optional[ModelType]:
|
17
22
|
"""
|
18
23
|
get object based on conditions
|
19
24
|
"""
|
20
|
-
query = select(self.model).filter_by(
|
25
|
+
query = select(self.model).filter_by(id=pk)
|
21
26
|
result = await self.session.execute(query)
|
22
|
-
|
27
|
+
instance = result.scalar_one_or_none()
|
28
|
+
|
29
|
+
if instance is None:
|
30
|
+
raise NotFoundException(
|
31
|
+
message="Object does not exist",
|
32
|
+
status=404,
|
33
|
+
code="0001",
|
34
|
+
)
|
35
|
+
|
36
|
+
return instance
|
23
37
|
|
24
|
-
async def filter(self, **kwargs) ->
|
38
|
+
async def filter(self, **kwargs) -> 'BaseManager[ModelType]':
|
25
39
|
"""
|
26
40
|
Filter objects based on conditions
|
27
41
|
"""
|
28
|
-
|
42
|
+
self.filters.update(kwargs)
|
43
|
+
return self
|
44
|
+
|
45
|
+
async def all(self) -> List[ModelType]:
|
46
|
+
"""Return all filtered results."""
|
47
|
+
query = select(self.model).filter_by(**self.filters)
|
29
48
|
result = await self.session.execute(query)
|
30
49
|
return list(result.scalars().all())
|
31
50
|
|
51
|
+
async def first(self) -> Optional[ModelType]:
|
52
|
+
"""Return the first matching object, or None."""
|
53
|
+
query = select(self.model).filter_by(**self.filters)
|
54
|
+
result = await self.session.execute(query)
|
55
|
+
return result.scalars().first()
|
56
|
+
|
57
|
+
async def count(self) -> int:
|
58
|
+
"""Return the count of matching records."""
|
59
|
+
query = select(self.model).filter_by(**self.filters)
|
60
|
+
result = await self.session.execute(query)
|
61
|
+
return len(result.scalars().all())
|
62
|
+
|
32
63
|
async def create(self, **kwargs) -> ModelType:
|
33
64
|
"""
|
34
65
|
Create a new object
|
35
|
-
|
66
|
+
"""
|
36
67
|
obj = self.model(**kwargs)
|
37
68
|
|
38
69
|
try:
|
@@ -52,7 +83,7 @@ class BaseManager(Generic[ModelType]):
|
|
52
83
|
raise InternalException(f"Unexpected error during creation: {str(e)}") from e
|
53
84
|
|
54
85
|
|
55
|
-
async def delete(self, id: Optional[int] = None, **filters)
|
86
|
+
async def delete(self, id: Optional[int] = None, **filters):
|
56
87
|
"""
|
57
88
|
Delete object(s) with flexible filtering options
|
58
89
|
|
@@ -1 +0,0 @@
|
|
1
|
-
from role_permissions import *
|
@@ -1,13 +1,14 @@
|
|
1
|
-
from fastapi import Depends
|
1
|
+
from fastapi import Depends
|
2
2
|
|
3
3
|
from rb_commons.configs.injections import get_claims
|
4
|
+
from rb_commons.http.exceptions import ForbiddenException
|
4
5
|
from rb_commons.schemes.jwt import Claims, UserRole
|
5
6
|
|
6
7
|
|
7
8
|
class BasePermission:
|
8
9
|
def __call__(self, claims: Claims = Depends(get_claims)):
|
9
10
|
if not self.has_permission(claims):
|
10
|
-
raise
|
11
|
+
raise ForbiddenException(message=f"Access denied", status=401, code="0000")
|
11
12
|
|
12
13
|
def has_permission(self, claims: Claims) -> bool:
|
13
14
|
return False
|
@@ -0,0 +1,17 @@
|
|
1
|
+
rb_commons/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
+
rb_commons/configs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
+
rb_commons/configs/config.py,sha256=tpqC1V9PXx88m0N5L13WqkoalPUk6SthjxgL_lmf-lE,1439
|
4
|
+
rb_commons/configs/injections.py,sha256=LcwYQWwmAKApID0xnpt9IUorpFXBo679b-ODrG19jvA,465
|
5
|
+
rb_commons/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
+
rb_commons/http/exceptions.py,sha256=EGRMr1cRgiJ9Q2tkfANbf0c6-zzXf1CD6J3cmCaT_FA,1885
|
7
|
+
rb_commons/orm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
8
|
+
rb_commons/orm/exceptions.py,sha256=1aMctiEwrPjyehoXVX1l6ML5ZOhmDkmBISzlTD5ey1Y,509
|
9
|
+
rb_commons/orm/managers.py,sha256=RDWDOJ0wGXbGErDi2clzJF_KDF0lPMZK_j0KQy_AMhg,7761
|
10
|
+
rb_commons/permissions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
11
|
+
rb_commons/permissions/role_permissions.py,sha256=XOv81nZpX8Iwj9A0eLfirPfwsha7TeVcnUiEDTS_Qfw,790
|
12
|
+
rb_commons/schemes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
13
|
+
rb_commons/schemes/jwt.py,sha256=F66JJDhholuOPPzlKeoC6f1TL4gXg4oRUrV5yheNpyo,1675
|
14
|
+
rb_commons-0.1.18.dist-info/METADATA,sha256=W415WB-6LHzNtSL0xYVeuWs-DCA1ZIU74HWzSCtVma0,6533
|
15
|
+
rb_commons-0.1.18.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
|
16
|
+
rb_commons-0.1.18.dist-info/top_level.txt,sha256=HPx_WAYo3_fbg1WCeGHsz3wPGio1ucbnrlm2lmqlJog,11
|
17
|
+
rb_commons-0.1.18.dist-info/RECORD,,
|
@@ -1,15 +0,0 @@
|
|
1
|
-
rb_commons/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
-
rb_commons/configs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
|
-
rb_commons/configs/config.py,sha256=tpqC1V9PXx88m0N5L13WqkoalPUk6SthjxgL_lmf-lE,1439
|
4
|
-
rb_commons/configs/injections.py,sha256=jOnabKVNj490nYqGuqw5jeSFFvq1lM8vfmx-R1NJobc,448
|
5
|
-
rb_commons/orm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
-
rb_commons/orm/exceptions.py,sha256=1aMctiEwrPjyehoXVX1l6ML5ZOhmDkmBISzlTD5ey1Y,509
|
7
|
-
rb_commons/orm/managers.py,sha256=s0MeGfuDmQA287IV57s8kk467AJwskAoG4HvgQxKTYc,6711
|
8
|
-
rb_commons/permissions/__init__.py,sha256=2AWODOWWC_p7ThC6kilzSFm4vZbekgk3Tk8hFOHzyTM,30
|
9
|
-
rb_commons/permissions/role_permissions.py,sha256=SSLToBcz6OLGBo2Jw9L8BcfZlGsTy_kB1VRrVTizKy0,735
|
10
|
-
rb_commons/schemes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
11
|
-
rb_commons/schemes/jwt.py,sha256=F66JJDhholuOPPzlKeoC6f1TL4gXg4oRUrV5yheNpyo,1675
|
12
|
-
rb_commons-0.1.16.dist-info/METADATA,sha256=k_eJ4IUYbN5y9wz4FC0uZDwtZ4DbQwsAofGLQO84wJI,6533
|
13
|
-
rb_commons-0.1.16.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
|
14
|
-
rb_commons-0.1.16.dist-info/top_level.txt,sha256=HPx_WAYo3_fbg1WCeGHsz3wPGio1ucbnrlm2lmqlJog,11
|
15
|
-
rb_commons-0.1.16.dist-info/RECORD,,
|
File without changes
|
File without changes
|