init defaults completed
This commit is contained in:
@@ -8,13 +8,6 @@ from datetime import timedelta
|
||||
from fastapi.exceptions import HTTPException
|
||||
from fastapi import status
|
||||
|
||||
from databases import (
|
||||
Users,
|
||||
People,
|
||||
Companies,
|
||||
UsersTokens,
|
||||
MongoQueryIdentity,
|
||||
)
|
||||
from databases.no_sql_models.validations import (
|
||||
PasswordHistoryViaUser,
|
||||
AccessHistoryViaUser,
|
||||
@@ -46,7 +39,7 @@ class AuthModule(PasswordModule):
|
||||
|
||||
@classmethod
|
||||
def check_user_exits(cls, access_key, domain):
|
||||
found_user: Users = cls.filter_one(
|
||||
found_user = cls.filter_one(
|
||||
or_(
|
||||
cls.email == str(access_key).lower(),
|
||||
cls.phone_number == str(access_key).replace(" ", ""),
|
||||
@@ -72,6 +65,9 @@ class AuthModule(PasswordModule):
|
||||
return self.generate_token(Auth.ACCESS_TOKEN_LENGTH)
|
||||
|
||||
def remove_refresher_token(self, domain, disconnect: bool = False):
|
||||
from databases import (
|
||||
UsersTokens,
|
||||
)
|
||||
if disconnect:
|
||||
registered_tokens = UsersTokens.filter_all(
|
||||
UsersTokens.user_id == self.id, system=True
|
||||
@@ -110,6 +106,9 @@ class AuthModule(PasswordModule):
|
||||
)
|
||||
|
||||
def create_password(self, password, password_token=None):
|
||||
from databases import (
|
||||
MongoQueryIdentity,
|
||||
)
|
||||
if self.password_token:
|
||||
replace_day = 0
|
||||
try:
|
||||
@@ -161,6 +160,9 @@ class AuthModule(PasswordModule):
|
||||
self.save()
|
||||
|
||||
def generate_refresher_token(self, domain: str, remember_me=False):
|
||||
from databases import (
|
||||
UsersTokens,
|
||||
)
|
||||
if remember_me:
|
||||
refresh_token = self.generate_token(Auth.REFRESHER_TOKEN_LENGTH)
|
||||
if already_token := UsersTokens.find_one(
|
||||
@@ -200,7 +202,11 @@ class UserLoginModule(AuthModule):
|
||||
|
||||
@classmethod
|
||||
def login_user_with_credentials(cls, data, request):
|
||||
|
||||
from databases import (
|
||||
Users,
|
||||
People,
|
||||
MongoQueryIdentity,
|
||||
)
|
||||
found_user = Users.check_user_exits(
|
||||
access_key=data.access_key, domain=data.domain
|
||||
)
|
||||
|
||||
@@ -40,8 +40,8 @@ class MongoQuery:
|
||||
|
||||
def __init__(self, table_name: str, database_name: str):
|
||||
database = MongoClient(MongoConfig.url)[database_name]
|
||||
if table_name not in database.collection_names():
|
||||
database.create_collection(name=table_name)
|
||||
# if table_name not in database.collection_names():
|
||||
# database.create_collection(name=table_name)
|
||||
self.table: Collection = database[table_name]
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -438,7 +438,7 @@ class BuildDecisionBookPerson(CrudCollection):
|
||||
if build_living_space_id:
|
||||
related_service = Services.filter_by_one(
|
||||
related_responsibility=str(occupant_type.occupant_code),
|
||||
*Services.valid_record_dict,
|
||||
**Services.valid_record_dict,
|
||||
).data
|
||||
if not related_service:
|
||||
raise HTTPException(
|
||||
|
||||
@@ -194,14 +194,14 @@ class Companies(CrudCollection, SelectAction):
|
||||
parent_id = mapped_column(Integer, nullable=True)
|
||||
workplace_no: Mapped[str] = mapped_column(String, nullable=True)
|
||||
|
||||
official_address_id: Mapped[int] = mapped_column(ForeignKey("addresses.id"))
|
||||
official_address_uu_id = mapped_column(
|
||||
official_address_id: Mapped[int] = mapped_column(ForeignKey("addresses.id"), nullable=True)
|
||||
official_address_uu_id: Mapped[str] = mapped_column(
|
||||
String, nullable=True, comment="Official Address UUID"
|
||||
)
|
||||
top_responsible_company_id = mapped_column(
|
||||
top_responsible_company_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("companies.id"), nullable=True
|
||||
)
|
||||
top_responsible_company_uu_id = mapped_column(
|
||||
top_responsible_company_uu_id: Mapped[str] = mapped_column(
|
||||
String, nullable=True, comment="Top Responsible Company UUID"
|
||||
)
|
||||
|
||||
|
||||
@@ -90,6 +90,7 @@ class Duties(CrudCollection):
|
||||
active_row = dict(
|
||||
is_confirmed=True, active=True, deleted=False, is_notification_send=True
|
||||
)
|
||||
list_of_created = []
|
||||
|
||||
execution = Departments.find_or_create(
|
||||
department_name="Execution Office",
|
||||
@@ -98,6 +99,7 @@ class Duties(CrudCollection):
|
||||
company_uu_id=str(company_uu_id),
|
||||
**active_row,
|
||||
)
|
||||
list_of_created.append(execution)
|
||||
it_dept = Departments.find_or_create(
|
||||
department_name="IT Department",
|
||||
department_code="ITD001",
|
||||
@@ -105,39 +107,46 @@ class Duties(CrudCollection):
|
||||
company_uu_id=str(company_uu_id),
|
||||
**active_row,
|
||||
)
|
||||
list_of_created.append(it_dept)
|
||||
bm_duty = Duty.find_or_create(
|
||||
duty_name="Business Manager",
|
||||
duty_code="BM0001",
|
||||
duty_description="Business Manager",
|
||||
**active_row,
|
||||
)
|
||||
list_of_created.append(bm_duty)
|
||||
it_duty = Duty.find_or_create(
|
||||
duty_name="IT Manager",
|
||||
duty_code="IT0001",
|
||||
duty_description="IT Manager",
|
||||
**active_row,
|
||||
)
|
||||
list_of_created.append(it_duty)
|
||||
bulk_duty = Duty.find_or_create(
|
||||
duty_name="BULK",
|
||||
duty_code="BULK",
|
||||
duty_description="BULK RECORDS OF THE COMPANY",
|
||||
**active_row,
|
||||
)
|
||||
list_of_created.append(bulk_duty)
|
||||
occu_duty = Duty.find_or_create(
|
||||
duty_name="OCCUPANT",
|
||||
duty_code="OCCUPANT",
|
||||
duty_description="OCCUPANT RECORDS OF THE COMPANY",
|
||||
**active_row,
|
||||
)
|
||||
cls.find_or_create(
|
||||
list_of_created.append(occu_duty)
|
||||
duties_created_bm = cls.find_or_create(
|
||||
company_id=company_id,
|
||||
company_uu_id=str(company_uu_id),
|
||||
duties_id=bm_duty.id,
|
||||
duties_uu_id=str(bm_duty.uu_id),
|
||||
department_id=execution.id,
|
||||
department_uu_id=str(execution.uu_id),
|
||||
**active_row,
|
||||
)
|
||||
cls.find_or_create(
|
||||
list_of_created.append(duties_created_bm)
|
||||
duties_created_it = cls.find_or_create(
|
||||
company_id=company_id,
|
||||
company_uu_id=str(company_uu_id),
|
||||
duties_id=it_duty.id,
|
||||
@@ -146,7 +155,8 @@ class Duties(CrudCollection):
|
||||
department_uu_id=str(it_dept.uu_id),
|
||||
**active_row,
|
||||
)
|
||||
cls.find_or_create(
|
||||
list_of_created.append(duties_created_it)
|
||||
duties_created__ex = cls.find_or_create(
|
||||
company_id=company_id,
|
||||
company_uu_id=str(company_uu_id),
|
||||
duties_id=bulk_duty.id,
|
||||
@@ -155,7 +165,8 @@ class Duties(CrudCollection):
|
||||
department_uu_id=str(execution.uu_id),
|
||||
**active_row,
|
||||
)
|
||||
cls.find_or_create(
|
||||
list_of_created.append(duties_created__ex)
|
||||
duties_created_at = cls.find_or_create(
|
||||
company_id=company_id,
|
||||
company_uu_id=str(company_uu_id),
|
||||
duties_id=occu_duty.id,
|
||||
@@ -164,6 +175,9 @@ class Duties(CrudCollection):
|
||||
department_uu_id=str(execution.uu_id),
|
||||
**active_row,
|
||||
)
|
||||
list_of_created.append(duties_created_at)
|
||||
return list_of_created
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_bulk_duties_of_a_company(cls, company_id):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import datetime
|
||||
from decimal import Decimal
|
||||
from typing import Union
|
||||
|
||||
from sqlalchemy import (
|
||||
TIMESTAMP,
|
||||
@@ -24,8 +23,6 @@ from sqlalchemy_mixins.repr import ReprMixin
|
||||
from sqlalchemy_mixins.smartquery import SmartQueryMixin
|
||||
|
||||
from api_library.date_time_actions.date_functions import DateTimeLocal, client_arrow
|
||||
from api_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
from api_objects.auth.token_objects import Credentials
|
||||
|
||||
from databases.sql_models.sql_operations import FilterAttributes
|
||||
from databases.sql_models.postgres_database import Base
|
||||
@@ -80,7 +77,7 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
"created_by_id",
|
||||
]
|
||||
|
||||
creds: Credentials = None # The credentials to use in the model.
|
||||
creds = None # The credentials to use in the model.
|
||||
client_arrow: DateTimeLocal = None # The arrow to use in the model.
|
||||
valid_record_dict: dict = {"active": True, "deleted": False}
|
||||
valid_record_args = lambda class_: [class_.active == True, class_.deleted == False]
|
||||
@@ -94,7 +91,7 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
|
||||
@classmethod
|
||||
def set_user_define_properties(
|
||||
cls, token: Union[EmployeeTokenObject, OccupantTokenObject]
|
||||
cls, token
|
||||
):
|
||||
cls.creds = token.credentials
|
||||
cls.client_arrow = DateTimeLocal(is_client=True, timezone=token.timezone)
|
||||
@@ -149,7 +146,6 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
formatted_date = client_arrow.get(str(val)).format(
|
||||
"DD-MM-YYYY HH:mm:ss"
|
||||
)
|
||||
print(key, "isinstance(value_type, datetime) | ", formatted_date)
|
||||
return str(formatted_date) if val else None
|
||||
elif isinstance(value_type, bool):
|
||||
return bool(val) if val else None
|
||||
@@ -175,7 +171,7 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
"""
|
||||
check_kwargs = cls.extract_system_fields(kwargs)
|
||||
cls.pre_query = cls.query.filter(cls.expiry_ends < system_arrow.now().date())
|
||||
already_record = cls.filter_by_one(**check_kwargs, system=True).data
|
||||
already_record = cls.filter_by_one(system=True, **check_kwargs).data
|
||||
cls.pre_query = None
|
||||
if already_record:
|
||||
if already_record.deleted:
|
||||
@@ -202,9 +198,10 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
created_record = cls()
|
||||
for key, value in check_kwargs.items():
|
||||
setattr(created_record, key, value)
|
||||
if getattr(cls.creds, 'person_id', None) and getattr(cls.creds, 'person_name', None):
|
||||
cls.created_by_id = cls.creds.person_id
|
||||
cls.created_by = cls.creds.person_name
|
||||
created_record.flush()
|
||||
cls.created_by_id = cls.creds.person_id
|
||||
cls.created_by = cls.creds.person_name
|
||||
return created_record
|
||||
|
||||
def update(self, **kwargs):
|
||||
@@ -223,11 +220,13 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
setattr(self, key, value)
|
||||
|
||||
if is_confirmed_argument:
|
||||
self.confirmed_by_id = self.creds.person_id
|
||||
self.confirmed_by = self.creds.person_name
|
||||
if getattr(self.creds, 'person_id', None) and getattr(self.creds, 'person_name', None):
|
||||
self.confirmed_by_id = self.creds.person_id
|
||||
self.confirmed_by = self.creds.person_name
|
||||
else:
|
||||
self.updated_by_id = self.creds.person_id
|
||||
self.updated_by = self.creds.person_name
|
||||
if getattr(self.creds, 'person_id', None) and getattr(self.creds, 'person_name', None):
|
||||
self.updated_by_id = self.creds.person_id
|
||||
self.updated_by = self.creds.person_name
|
||||
self.flush()
|
||||
return self
|
||||
|
||||
@@ -252,8 +251,6 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
exclude.extend(
|
||||
list(set(self.__exclude__fields__ or []).difference(exclude))
|
||||
)
|
||||
for i in self.__system_default_model__:
|
||||
print("i", str(i)[-2:])
|
||||
exclude.extend(
|
||||
[
|
||||
element
|
||||
@@ -278,27 +275,27 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
if value_of_database is not None:
|
||||
return_dict[key] = value_of_database
|
||||
|
||||
all_arguments = [
|
||||
record
|
||||
for record in self.__class__.__dict__
|
||||
if "_" not in record[0] and "id" not in record[-2:]
|
||||
]
|
||||
|
||||
for all_argument in all_arguments:
|
||||
column = getattr(self.__class__, all_argument)
|
||||
is_populate = isinstance(column, InstrumentedAttribute) and not hasattr(
|
||||
column, "foreign_keys"
|
||||
)
|
||||
if is_populate and all_argument in include_joins or []:
|
||||
populate_arg = getattr(self, all_argument, None)
|
||||
if isinstance(populate_arg, list):
|
||||
return_dict[all_argument] = [
|
||||
arg.get_dict() if arg else [] for arg in populate_arg
|
||||
]
|
||||
elif getattr(populate_arg, "get_dict", None):
|
||||
return_dict[all_argument] = (
|
||||
populate_arg.get_dict() if populate_arg else []
|
||||
)
|
||||
# all_arguments = [
|
||||
# record
|
||||
# for record in self.__class__.__dict__
|
||||
# if "_" not in record[0] and "id" not in record[-2:]
|
||||
# ]
|
||||
#
|
||||
# for all_argument in all_arguments:
|
||||
# column = getattr(self.__class__, all_argument)
|
||||
# is_populate = isinstance(column, InstrumentedAttribute) and not hasattr(
|
||||
# column, "foreign_keys"
|
||||
# )
|
||||
# if is_populate and all_argument in include_joins or []:
|
||||
# populate_arg = getattr(self, all_argument, None)
|
||||
# if isinstance(populate_arg, list):
|
||||
# return_dict[all_argument] = [
|
||||
# arg.get_dict() if arg else [] for arg in populate_arg
|
||||
# ]
|
||||
# elif getattr(populate_arg, "get_dict", None):
|
||||
# return_dict[all_argument] = (
|
||||
# populate_arg.get_dict() if populate_arg else []
|
||||
# )
|
||||
return dict(sorted(return_dict.items(), reverse=False))
|
||||
|
||||
|
||||
|
||||
@@ -36,7 +36,9 @@ class UsersTokens(CrudCollection):
|
||||
token_type: Mapped[str] = mapped_column(String(16), server_default="RememberMe")
|
||||
token: Mapped[str] = mapped_column(String, server_default="")
|
||||
domain: Mapped[str] = mapped_column(String, server_default="")
|
||||
expires_at = mapped_column(TIMESTAMP, default=str(system_arrow.shift(days=3)))
|
||||
expires_at = mapped_column(
|
||||
TIMESTAMP, default=str(system_arrow.shift(date=system_arrow.now(),days=3))
|
||||
)
|
||||
|
||||
# users = relationship("Users", back_populates="tokens", foreign_keys=[user_id])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user