gitscope
https://dev.kronoz.win/r/marzneshin.git
app/db/__init__.py · v0.1.1
Source
1 a47eb4c Dawsh 2y ago from sqlalchemy.exc import SQLAlchemyError
2 a47eb4c Dawsh 2y ago from sqlalchemy.orm import Session
3 a47eb4c Dawsh 2y ago
4 a47eb4c Dawsh 2y ago from .base import Base, SessionLocal, engine # noqa
5 a47eb4c Dawsh 2y ago
6 f7af2b3 Dawsh 2y ago from .crud import (create_admin, create_notification_reminder, # noqa
7 f7af2b3 Dawsh 2y ago create_user, delete_notification_reminder, get_admin,
8 f7af2b3 Dawsh 2y ago get_admins, get_jwt_secret_key, get_notification_reminder,
9 f7af2b3 Dawsh 2y ago assure_node_inbounds, get_system_usage,
10 f7af2b3 Dawsh 2y ago get_tls_certificate, get_user, get_user_by_id, get_users,
11 f7af2b3 Dawsh 2y ago get_users_count, remove_admin, remove_user, revoke_user_sub,
12 f7af2b3 Dawsh 2y ago set_owner, update_admin, update_user, update_user_status,
13 f7af2b3 Dawsh 2y ago update_user_sub, start_user_expire)
14 f7af2b3 Dawsh 2y ago
15 f7af2b3 Dawsh 2y ago from .models import JWT, System, User # noqa
16 a47eb4c Dawsh 2y ago
17 c13a1aa Dawsh 2y ago
18 a47eb4c Dawsh 2y ago class GetDB: # Context Manager
19 a47eb4c Dawsh 2y ago def __init__(self):
20 a47eb4c Dawsh 2y ago self.db = SessionLocal()
21 a47eb4c Dawsh 2y ago
22 a47eb4c Dawsh 2y ago def __enter__(self):
23 a47eb4c Dawsh 2y ago return self.db
24 a47eb4c Dawsh 2y ago
25 a47eb4c Dawsh 2y ago def __exit__(self, _, exc_value, traceback):
26 a47eb4c Dawsh 2y ago if isinstance(exc_value, SQLAlchemyError):
27 a47eb4c Dawsh 2y ago self.db.rollback() # rollback on exception
28 a47eb4c Dawsh 2y ago
29 a47eb4c Dawsh 2y ago self.db.close()
30 a47eb4c Dawsh 2y ago
31 a47eb4c Dawsh 2y ago
32 a47eb4c Dawsh 2y ago __all__ = [
33 a47eb4c Dawsh 2y ago "get_user",
34 a47eb4c Dawsh 2y ago "get_user_by_id",
35 a47eb4c Dawsh 2y ago "get_users",
36 a47eb4c Dawsh 2y ago "get_users_count",
37 a47eb4c Dawsh 2y ago "create_user",
38 a47eb4c Dawsh 2y ago "remove_user",
39 a47eb4c Dawsh 2y ago "update_user",
40 a47eb4c Dawsh 2y ago "update_user_status",
41 a47eb4c Dawsh 2y ago "start_user_expire",
42 a47eb4c Dawsh 2y ago "update_user_sub",
43 a47eb4c Dawsh 2y ago "revoke_user_sub",
44 a47eb4c Dawsh 2y ago "set_owner",
45 a47eb4c Dawsh 2y ago "get_system_usage",
46 a47eb4c Dawsh 2y ago "get_jwt_secret_key",
47 a47eb4c Dawsh 2y ago "get_tls_certificate",
48 a47eb4c Dawsh 2y ago "get_admin",
49 a47eb4c Dawsh 2y ago "create_admin",
50 a47eb4c Dawsh 2y ago "update_admin",
51 a47eb4c Dawsh 2y ago "remove_admin",
52 a47eb4c Dawsh 2y ago "get_admins",
53 a47eb4c Dawsh 2y ago
54 a47eb4c Dawsh 2y ago "create_notification_reminder",
55 a47eb4c Dawsh 2y ago "get_notification_reminder",
56 a47eb4c Dawsh 2y ago "delete_notification_reminder",
57 a47eb4c Dawsh 2y ago
58 a47eb4c Dawsh 2y ago "GetDB",
59 a47eb4c Dawsh 2y ago
60 a47eb4c Dawsh 2y ago "User",
61 a47eb4c Dawsh 2y ago "System",
62 a47eb4c Dawsh 2y ago "JWT",
63 a47eb4c Dawsh 2y ago
64 a47eb4c Dawsh 2y ago "Base",
65 a47eb4c Dawsh 2y ago "Session",
66 a47eb4c Dawsh 2y ago ]