|
| 1 | +""" |
| 2 | +Agnostic database assets |
| 3 | +
|
| 4 | +Put here your utils and class independent of |
| 5 | +the database solution. |
| 6 | +To know more about protocols and subtyping check out PEP-0544 |
| 7 | +""" |
| 8 | +import abc |
| 9 | + |
| 10 | +from flask import Flask |
| 11 | + |
| 12 | +from commons.data_access_layer.cosmos_db import CosmosDBDao |
| 13 | +from commons.data_access_layer.database import EventContext |
| 14 | +from time_tracker_api.security import current_user_id, current_user_tenant_id |
| 15 | + |
| 16 | + |
| 17 | +class CRUDDao(abc.ABC): |
| 18 | + @abc.abstractmethod |
| 19 | + def get_all(self, conditions: dict): |
| 20 | + raise NotImplementedError # pragma: no cover |
| 21 | + |
| 22 | + @abc.abstractmethod |
| 23 | + def get(self, id): |
| 24 | + raise NotImplementedError # pragma: no cover |
| 25 | + |
| 26 | + @abc.abstractmethod |
| 27 | + def create(self, project): |
| 28 | + raise NotImplementedError # pragma: no cover |
| 29 | + |
| 30 | + @abc.abstractmethod |
| 31 | + def update(self, id, data): |
| 32 | + raise NotImplementedError # pragma: no cover |
| 33 | + |
| 34 | + @abc.abstractmethod |
| 35 | + def delete(self, id): |
| 36 | + raise NotImplementedError # pragma: no cover |
| 37 | + |
| 38 | + |
| 39 | +class ApiEventContext(EventContext): |
| 40 | + def __init__(self, container_id: str, action: str, description: str = None, |
| 41 | + user_id: str = None, tenant_id: str = None, session_id: str = None): |
| 42 | + super(ApiEventContext, self).__init__(container_id, action, description) |
| 43 | + self._user_id = user_id |
| 44 | + self._tenant_id = tenant_id |
| 45 | + self._session_id = session_id |
| 46 | + |
| 47 | + @property |
| 48 | + def user_id(self) -> str: |
| 49 | + if self._user_id is None: |
| 50 | + self._user_id = current_user_id() |
| 51 | + return self._user_id |
| 52 | + |
| 53 | + @property |
| 54 | + def tenant_id(self) -> str: |
| 55 | + if self._tenant_id is None: |
| 56 | + self._tenant_id = current_user_tenant_id() |
| 57 | + return self._tenant_id |
| 58 | + |
| 59 | + @property |
| 60 | + def session_id(self) -> str: |
| 61 | + return self._session_id |
| 62 | + |
| 63 | + |
| 64 | +class APICosmosDBDao(CosmosDBDao): |
| 65 | + def create_event_context(self, action: str = None, description: str = None): |
| 66 | + return ApiEventContext(self.repository.container.id, action, |
| 67 | + description=description) |
| 68 | + |
| 69 | + |
| 70 | +def init_app(app: Flask) -> None: |
| 71 | + init_cosmos_db(app) |
| 72 | + |
| 73 | + |
| 74 | +def init_sql(app: Flask) -> None: |
| 75 | + from commons.data_access_layer.sql import init_app |
| 76 | + init_app(app) |
| 77 | + |
| 78 | + |
| 79 | +def init_cosmos_db(app: Flask) -> None: |
| 80 | + from commons.data_access_layer.cosmos_db import init_app |
| 81 | + init_app(app) |
0 commit comments