migrator functions updated
This commit is contained in:
@@ -44,12 +44,13 @@ class AuthModule(PasswordModule):
|
||||
|
||||
@classmethod
|
||||
def check_user_exits(cls, access_key, domain):
|
||||
found_user = cls.filter_one(
|
||||
from databases import Users
|
||||
found_user = Users.query.filter(
|
||||
or_(
|
||||
cls.email == str(access_key).lower(),
|
||||
cls.phone_number == str(access_key).replace(" ", ""),
|
||||
Users.email == str(access_key).lower(),
|
||||
Users.phone_number == str(access_key).replace(" ", ""),
|
||||
),
|
||||
).data
|
||||
).first()
|
||||
if not found_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
@@ -73,6 +74,7 @@ class AuthModule(PasswordModule):
|
||||
from databases import (
|
||||
UsersTokens,
|
||||
)
|
||||
|
||||
if disconnect:
|
||||
registered_tokens = UsersTokens.filter_all(
|
||||
UsersTokens.user_id == self.id, system=True
|
||||
@@ -88,19 +90,12 @@ class AuthModule(PasswordModule):
|
||||
|
||||
def check_password(self, password):
|
||||
main_domain = self.get_main_domain_and_other_domains(get_main_domain=True)
|
||||
print('check_password', dict(
|
||||
domain=main_domain,
|
||||
id_=str(self.uu_id),
|
||||
password_hashed=self.hash_password,
|
||||
password=password,
|
||||
))
|
||||
if check_password := self.check_hashed_password(
|
||||
domain=main_domain,
|
||||
id_=str(self.uu_id),
|
||||
password_hashed=self.hash_password,
|
||||
password=password,
|
||||
):
|
||||
print('check_password', check_password)
|
||||
return check_password
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
@@ -120,6 +115,7 @@ class AuthModule(PasswordModule):
|
||||
@staticmethod
|
||||
def create_password(found_user, password, password_token=None):
|
||||
from databases import MongoQueryIdentity
|
||||
|
||||
if found_user.password_token:
|
||||
replace_day = 0
|
||||
try:
|
||||
@@ -141,9 +137,9 @@ class AuthModule(PasswordModule):
|
||||
)
|
||||
query_engine = MongoQueryIdentity(company_uuid=found_user.related_company)
|
||||
|
||||
domain_via_user = query_engine.get_domain_via_user(user_uu_id=str(found_user.uu_id))[
|
||||
"main_domain"
|
||||
]
|
||||
domain_via_user = query_engine.get_domain_via_user(
|
||||
user_uu_id=str(found_user.uu_id)
|
||||
)["main_domain"]
|
||||
new_password_dict = {
|
||||
"password": found_user.create_hashed_password(
|
||||
domain=domain_via_user, id_=str(found_user.uu_id), password=password
|
||||
@@ -167,18 +163,21 @@ class AuthModule(PasswordModule):
|
||||
@staticmethod
|
||||
def reset_password_token(found_user):
|
||||
found_user.password_expiry_begins = str(system_arrow.now())
|
||||
found_user.password_token = found_user.generate_token(127)
|
||||
found_user.password_token = found_user.generate_token(
|
||||
Auth.REFRESHER_TOKEN_LENGTH
|
||||
)
|
||||
found_user.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(
|
||||
user_id=self.id, token_type="RememberMe", domain=domain
|
||||
):
|
||||
if already_token := UsersTokens.filter_by_one(
|
||||
system=True, user_id=self.id, token_type="RememberMe", domain=domain
|
||||
).data:
|
||||
already_token.update(token=refresh_token)
|
||||
already_token.expires_at = system_arrow.shift(days=3)
|
||||
already_token.save()
|
||||
@@ -194,16 +193,13 @@ class AuthModule(PasswordModule):
|
||||
return None
|
||||
|
||||
def remainder_day(self):
|
||||
join_list = [
|
||||
_ for _ in str(self.password_expires_day).split(",")[0] if _.isdigit()
|
||||
]
|
||||
return float(
|
||||
timedelta(
|
||||
days=int(
|
||||
"".join(
|
||||
[
|
||||
_
|
||||
for _ in str(self.password_expires_day).split(",")[0]
|
||||
if _.isdigit()
|
||||
]
|
||||
)
|
||||
"".join(join_list)
|
||||
)
|
||||
).seconds
|
||||
)
|
||||
@@ -218,13 +214,13 @@ class UserLoginModule(AuthModule):
|
||||
People,
|
||||
MongoQueryIdentity,
|
||||
)
|
||||
|
||||
found_user = Users.check_user_exits(
|
||||
access_key=data.access_key, domain=data.domain
|
||||
)
|
||||
access_token = found_user.generate_access_token()
|
||||
query_engine = MongoQueryIdentity(company_uuid=found_user.related_company)
|
||||
if found_user.check_password(password=data.password):
|
||||
print('before access_object_to_redis')
|
||||
access_object_to_redis = save_access_token_to_redis(
|
||||
request=request,
|
||||
found_user=found_user,
|
||||
@@ -270,7 +266,9 @@ class UserLoginModule(AuthModule):
|
||||
no_address_validates = mongo_db.mongo_engine.get_all()[0] == 0
|
||||
record_id = uuid.uuid4().__str__()
|
||||
notice_link = ApiStatic.blacklist_login(record_id=record_id)
|
||||
found_people = People.find_one(id=found_user.person_id)
|
||||
found_people = People.filter_one(
|
||||
People.id == found_user.person_id, *People.valid_record_args(People)
|
||||
).data
|
||||
access_via_user = query_engine.update_access_history_via_user(
|
||||
AccessHistoryViaUser(
|
||||
**{
|
||||
|
||||
@@ -33,7 +33,6 @@ class MongoQueryIdentity:
|
||||
table_name=self.mongo_collection_name, database_name="mongo_database"
|
||||
)
|
||||
|
||||
|
||||
def create_domain_via_user(self, payload: DomainViaUser):
|
||||
self.use_collection("Domain")
|
||||
return self.mongo_engine.insert(
|
||||
|
||||
@@ -45,9 +45,7 @@ class AccountBooks(CrudCollection):
|
||||
# )
|
||||
|
||||
__table_args__ = (
|
||||
Index(
|
||||
"account_companies_book_ndx_00", company_id, "expiry_starts"
|
||||
),
|
||||
Index("account_companies_book_ndx_00", company_id, "expiry_starts"),
|
||||
{"comment": "Account Book Information"},
|
||||
)
|
||||
|
||||
|
||||
@@ -478,6 +478,7 @@ class BuildParts(CrudCollection):
|
||||
@classmethod
|
||||
def create_action(cls, data: InsertBuildParts, token):
|
||||
from databases import ApiEnumDropdown
|
||||
|
||||
data_dict = data.dump()
|
||||
build_from_duty = Build.select_action(
|
||||
employee_id=token.selected_company.employee_id,
|
||||
|
||||
@@ -28,7 +28,6 @@ from api_validations.validations_request import (
|
||||
from databases.sql_models.core_mixin import CrudCollection
|
||||
|
||||
|
||||
|
||||
class BuildDecisionBook(CrudCollection):
|
||||
"""
|
||||
Builds class based on declarative_base and BaseMixin via session
|
||||
@@ -99,6 +98,7 @@ class BuildDecisionBook(CrudCollection):
|
||||
from databases import (
|
||||
Build,
|
||||
)
|
||||
|
||||
related_build = Build.find_one(id=cls.build_id)
|
||||
related_date = system_arrow.get(related_build.build_date)
|
||||
date_processed = related_date.replace(
|
||||
@@ -119,7 +119,7 @@ class BuildDecisionBook(CrudCollection):
|
||||
build_id=str(related_build.uu_id),
|
||||
build_name=related_build.build_name,
|
||||
decision_type="RBM",
|
||||
)
|
||||
),
|
||||
)
|
||||
return book
|
||||
return
|
||||
@@ -130,13 +130,14 @@ class BuildDecisionBook(CrudCollection):
|
||||
Build,
|
||||
Companies,
|
||||
)
|
||||
|
||||
related_companies = Companies.select_action(duty_id_list=[int(duty_id)])
|
||||
related_companies_ids = list(
|
||||
related_.id for related_ in related_companies.all()
|
||||
)
|
||||
related_building = Build.filter_all(
|
||||
Build.company_id.in_(related_companies_ids)
|
||||
*Build.valid_record_args(Build),
|
||||
* Build.valid_record_args(Build),
|
||||
)
|
||||
related_building_ids = list(related_.id for related_ in related_building.data)
|
||||
return cls.filter_all(cls.build_id.in_(related_building_ids)).query
|
||||
@@ -147,6 +148,7 @@ class BuildDecisionBook(CrudCollection):
|
||||
Build,
|
||||
Companies,
|
||||
)
|
||||
|
||||
data_dict = data.model_dump()
|
||||
if building := Build.find_one(uu_id=data.build_uu_id):
|
||||
data_dict["build_id"] = building.id
|
||||
@@ -217,6 +219,7 @@ class BuildDecisionBook(CrudCollection):
|
||||
from databases import (
|
||||
BuildIbans,
|
||||
)
|
||||
|
||||
if all(
|
||||
[True if letter in str(bank_date) else False for letter in ["-", " ", ":"]]
|
||||
):
|
||||
@@ -493,7 +496,9 @@ class BuildDecisionBookPerson(CrudCollection):
|
||||
def get_occupant_types(self):
|
||||
if occupants := BuildDecisionBookPersonOccupants.filter_all(
|
||||
BuildDecisionBookPersonOccupants.build_decision_book_person_id == self.id,
|
||||
*BuildDecisionBookPersonOccupants.valid_record_args(BuildDecisionBookPersonOccupants)
|
||||
*BuildDecisionBookPersonOccupants.valid_record_args(
|
||||
BuildDecisionBookPersonOccupants
|
||||
),
|
||||
).data:
|
||||
return occupants
|
||||
return
|
||||
@@ -606,6 +611,7 @@ class BuildDecisionBookItems(CrudCollection):
|
||||
Build,
|
||||
Companies,
|
||||
)
|
||||
|
||||
related_companies = Companies.select_action(duty_id=duty_id)
|
||||
related_companies_ids = list(
|
||||
related_.id for related_ in related_companies.all()
|
||||
@@ -651,6 +657,7 @@ class BuildDecisionBookItems(CrudCollection):
|
||||
People,
|
||||
OccupantTypes,
|
||||
)
|
||||
|
||||
active_invite = (
|
||||
BuildDecisionBookInvitations.check_invites_are_ready_for_meeting(
|
||||
selected_decision_book=decision_book,
|
||||
@@ -1059,18 +1066,18 @@ class BuildDecisionBookProjects(CrudCollection):
|
||||
Build,
|
||||
Companies,
|
||||
)
|
||||
|
||||
related_companies = Companies.select_action(duty_id_list=[duty_id])
|
||||
related_companies_ids = list(
|
||||
related_.id for related_ in related_companies.all()
|
||||
)
|
||||
related_building = Build.filter_all(
|
||||
Build.company_id.in_(related_companies_ids),
|
||||
*Build.valid_record_args(Build)
|
||||
Build.company_id.in_(related_companies_ids), *Build.valid_record_args(Build)
|
||||
)
|
||||
related_building_ids = list(related_.id for related_ in related_building.data)
|
||||
related_decision_books = BuildDecisionBook.filter_all(
|
||||
BuildDecisionBook.build_id.in_(related_building_ids),
|
||||
*BuildDecisionBook.valid_record_args(BuildDecisionBook)
|
||||
*BuildDecisionBook.valid_record_args(BuildDecisionBook),
|
||||
).data
|
||||
related_decision_books_ids = list(
|
||||
related_.id for related_ in related_decision_books
|
||||
@@ -1079,14 +1086,14 @@ class BuildDecisionBookProjects(CrudCollection):
|
||||
BuildDecisionBookItems.build_decision_book_id.in_(
|
||||
related_decision_books_ids
|
||||
),
|
||||
*BuildDecisionBookItems.valid_record_args(BuildDecisionBookItems)
|
||||
*BuildDecisionBookItems.valid_record_args(BuildDecisionBookItems),
|
||||
).data
|
||||
related_decision_books_items_ids = list(
|
||||
related_.id for related_ in related_decision_books_items
|
||||
)
|
||||
return cls.filter_all(
|
||||
cls.build_decision_book_item_id.in_(related_decision_books_items_ids),
|
||||
*cls.valid_record_args(cls)
|
||||
*cls.valid_record_args(cls),
|
||||
).query
|
||||
|
||||
@classmethod
|
||||
@@ -1095,11 +1102,14 @@ class BuildDecisionBookProjects(CrudCollection):
|
||||
People,
|
||||
Companies,
|
||||
)
|
||||
|
||||
data_dict = data.dump()
|
||||
BuildDecisionBookItems.pre_query = BuildDecisionBookItems.select_action(
|
||||
duty_id=token.duty_list["duty_id"]
|
||||
)
|
||||
People.pre_query = People.select_action(duty_id_list=[token.duty_list["duty_id"]])
|
||||
People.pre_query = People.select_action(
|
||||
duty_id_list=[token.duty_list["duty_id"]]
|
||||
)
|
||||
decision_book_project_item = BuildDecisionBookItems.find_one_or_abort(
|
||||
uu_id=data_dict.get("build_decision_book_item_uu_id")
|
||||
)
|
||||
|
||||
@@ -63,12 +63,19 @@ class RelationshipDutyCompany(CrudCollection):
|
||||
from databases import (
|
||||
Duties,
|
||||
)
|
||||
|
||||
token_duties_id, token_company_id = token.get("duty_id"), token.get(
|
||||
"company_id"
|
||||
)
|
||||
send_duties, list_match_company_id = Duties.find_one(uu_id=data.duty_uu_id), []
|
||||
send_user_duties = Duties.find_one(
|
||||
duties_id=send_duties.id, company_id=token_duties_id
|
||||
list_match_company_id = []
|
||||
send_duties = Duties.filter_one(
|
||||
Duties.uu_id==data.duty_uu_id,
|
||||
*Duties.valid_record_args(Duties),
|
||||
)
|
||||
send_user_duties = Duties.filter_one(
|
||||
Duties.duties_id==send_duties.id,
|
||||
Duties.company_id==token_duties_id,
|
||||
*Duties.valid_record_args(Duties),
|
||||
)
|
||||
if not send_user_duties:
|
||||
raise Exception(
|
||||
@@ -76,11 +83,15 @@ class RelationshipDutyCompany(CrudCollection):
|
||||
)
|
||||
|
||||
for company_uu_id in list(data.match_company_uu_id):
|
||||
company = Companies.find_one(uu_id=company_uu_id)
|
||||
bulk_company = RelationshipDutyCompany.find_one(
|
||||
owner_id=token_company_id,
|
||||
relationship_type="Bulk",
|
||||
member_id=company.id,
|
||||
company = Companies.filter_one(
|
||||
Companies.uu_id==company_uu_id,
|
||||
*Companies.valid_record_args(Companies),
|
||||
)
|
||||
bulk_company = RelationshipDutyCompany.filter_one(
|
||||
RelationshipDutyCompany.owner_id==token_company_id,
|
||||
RelationshipDutyCompany.relationship_type=="Bulk",
|
||||
RelationshipDutyCompany.member_id==company.id,
|
||||
*RelationshipDutyCompany.valid_record_args(RelationshipDutyCompany),
|
||||
)
|
||||
if not bulk_company:
|
||||
raise Exception(
|
||||
@@ -104,12 +115,19 @@ class RelationshipDutyCompany(CrudCollection):
|
||||
from databases import (
|
||||
Duties,
|
||||
)
|
||||
|
||||
token_duties_id, token_company_id = token.get("duty_id"), token.get(
|
||||
"company_id"
|
||||
)
|
||||
send_duties, list_match_company_id = Duties.find_one(uu_id=data.duty_uu_id), []
|
||||
send_user_duties = Duties.find_one(
|
||||
duties_id=send_duties.id, company_id=token_duties_id
|
||||
list_match_company_id = []
|
||||
send_duties = Duties.filter_one(
|
||||
Duties.uu_id==data.duty_uu_id,
|
||||
*Duties.valid_record_args(Duties),
|
||||
)
|
||||
send_user_duties = Duties.filter_one(
|
||||
Duties.duties_id==send_duties.id,
|
||||
Duties.company_id==token_duties_id,
|
||||
*Duties.valid_record_args(Duties),
|
||||
)
|
||||
if not send_user_duties:
|
||||
raise Exception(
|
||||
@@ -117,11 +135,15 @@ class RelationshipDutyCompany(CrudCollection):
|
||||
)
|
||||
|
||||
for company_uu_id in list(data.match_company_uu_id):
|
||||
company = Companies.find_one(uu_id=company_uu_id)
|
||||
bulk_company = RelationshipDutyCompany.find_one(
|
||||
owner_id=token_company_id,
|
||||
relationship_type="Bulk",
|
||||
member_id=company.id,
|
||||
company = Companies.filter_one(
|
||||
Companies.uu_id==company_uu_id,
|
||||
*Companies.valid_record_args(Companies),
|
||||
)
|
||||
bulk_company = RelationshipDutyCompany.filter_one(
|
||||
RelationshipDutyCompany.owner_id==token_company_id,
|
||||
RelationshipDutyCompany.relationship_type=="Bulk",
|
||||
RelationshipDutyCompany.member_id==company.id,
|
||||
*RelationshipDutyCompany.valid_record_args(RelationshipDutyCompany),
|
||||
)
|
||||
if not bulk_company:
|
||||
raise Exception(
|
||||
@@ -194,7 +216,9 @@ 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"), nullable=True)
|
||||
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"
|
||||
)
|
||||
@@ -222,57 +246,45 @@ class Companies(CrudCollection, SelectAction):
|
||||
from databases import Addresses, Duties
|
||||
|
||||
data_dict = data.model_dump()
|
||||
if cls.filter_one(cls.tax_no == str(data.tax_no).strip()):
|
||||
raise Exception(
|
||||
"Company already exists. Please ask supervisor to make company visible for your duty."
|
||||
if cls.filter_one(cls.tax_no == str(data.tax_no).strip(), system=True).data:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Company already exists. Please ask supervisor to make company visible for your duty."
|
||||
)
|
||||
|
||||
official_address = Addresses.filter_one(
|
||||
Addresses.uu_id == data.official_address_uu_id
|
||||
)
|
||||
if not official_address:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Official address is not found. Please check address uuid and try again.",
|
||||
)
|
||||
Addresses.uu_id == data.official_address_uu_id,
|
||||
*Addresses.valid_record_args(Addresses),
|
||||
).data
|
||||
# if not official_address:
|
||||
# raise HTTPException(
|
||||
# status_code=400,
|
||||
# detail="Official address is not found. Please check address uuid and try again.",
|
||||
# )
|
||||
|
||||
bulk_duties = Duties.get_bulk_duties_of_a_company(
|
||||
company_id=token.selected_company.company_id
|
||||
)
|
||||
|
||||
if official_address:
|
||||
data_dict["official_address_id"] = official_address.id
|
||||
data_dict["official_address_uu_id"] = str(official_address.uu_id)
|
||||
|
||||
data_dict["parent_id"] = token.selected_company.company_id
|
||||
data_dict["official_address_id"] = official_address.id
|
||||
data_dict["official_address_uu_id"] = str(official_address.uu_id)
|
||||
data_dict["top_responsible_company_id"] = token.selected_company.company_id
|
||||
data_dict["top_responsible_company_uu_id"] = (
|
||||
token.selected_company.company_uu_id
|
||||
)
|
||||
|
||||
company_created = cls.find_or_create(**data_dict)
|
||||
if not company_created.is_found:
|
||||
RelationshipDutyCompany.find_or_create(
|
||||
owner_id=token.selected_company.company_id,
|
||||
duties_id=bulk_duties.id,
|
||||
member_id=company_created.id,
|
||||
parent_id=company_created.parent_id,
|
||||
relationship_type="Bulk",
|
||||
show_only=False,
|
||||
)
|
||||
if (
|
||||
not str(token.get("priority_code")) == "78"
|
||||
): # Company based configuration will be applied
|
||||
user_duties = Duties.find_one(
|
||||
duties_id=token.selected_company.duty_id,
|
||||
company_id=token.selected_company.company_id,
|
||||
)
|
||||
RelationshipDutyCompany.find_or_create(
|
||||
owner_id=token.selected_company.company_id,
|
||||
duties_id=user_duties.id,
|
||||
member_id=company_created.id,
|
||||
parent_id=company_created.parent_id,
|
||||
relationship_type="Commercial",
|
||||
show_only=False,
|
||||
)
|
||||
RelationshipDutyCompany.find_or_create(
|
||||
owner_id=token.selected_company.company_id,
|
||||
duties_id=bulk_duties.id,
|
||||
member_id=company_created.id,
|
||||
parent_id=company_created.parent_id,
|
||||
child_count=0,
|
||||
relationship_type="Bulk",
|
||||
show_only=False,
|
||||
)
|
||||
return company_created
|
||||
|
||||
@classmethod
|
||||
@@ -280,11 +292,15 @@ class Companies(CrudCollection, SelectAction):
|
||||
from databases import (
|
||||
Addresses,
|
||||
)
|
||||
|
||||
data_dict = data.excluded_dump()
|
||||
duty_id = token.get("duty_id")
|
||||
company_id = token.get("company_id")
|
||||
if data.official_address_uu_id:
|
||||
official_address = Addresses.find_one(uu_id=data.official_address_uu_id)
|
||||
official_address = Addresses.filter_one(
|
||||
Addresses.uu_id==data.official_address_uu_id,
|
||||
*Addresses.valid_record_args(Addresses),
|
||||
).data
|
||||
data_dict["official_address_id"] = official_address.id
|
||||
del data_dict["official_address_uu_id"], data_dict["company_uu_id"]
|
||||
company_to_update = cls.select_action(
|
||||
@@ -558,4 +574,3 @@ class Companies(CrudCollection, SelectAction):
|
||||
# "default_money_type": self.default_money_type,
|
||||
# "official_address_id": self.official_address_id,
|
||||
# }
|
||||
|
||||
|
||||
@@ -178,12 +178,12 @@ class Duties(CrudCollection):
|
||||
list_of_created.append(duties_created_at)
|
||||
return list_of_created
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_bulk_duties_of_a_company(cls, company_id):
|
||||
if bulk_duties := Duties.find_one(
|
||||
duties_id=Duty.find_one(duty_code="BULK").id, company_id=company_id
|
||||
):
|
||||
duties_id = Duty.filter_by_one(system=True, duty_code="BULK").data
|
||||
if bulk_duties := Duties.filter_by_one(
|
||||
duties_id=getattr(duties_id,'id', None), company_id=company_id, **Duties.valid_record_dict
|
||||
).data:
|
||||
return bulk_duties
|
||||
raise Exception("Bulk Duty not found. Please contact with supervisor.")
|
||||
|
||||
|
||||
@@ -90,9 +90,7 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def set_user_define_properties(
|
||||
cls, token
|
||||
):
|
||||
def set_user_define_properties(cls, token):
|
||||
cls.creds = token.credentials
|
||||
cls.client_arrow = DateTimeLocal(is_client=True, timezone=token.timezone)
|
||||
|
||||
@@ -126,39 +124,39 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
is_primary, value_type = key in cls.primary_keys, type(val)
|
||||
row_attr = bool(getattr(getattr(cls, key), "foreign_keys", None))
|
||||
if is_primary or row_attr and key_ == Mapped[int]:
|
||||
return None
|
||||
return False, None
|
||||
|
||||
if key_:
|
||||
if key_ == Mapped[int]:
|
||||
return int(val) if val else None
|
||||
return True, int(val) if val is not None else None
|
||||
elif key_ == Mapped[bool]:
|
||||
return bool(val) if val else None
|
||||
return True, bool(val) if val is not None else None
|
||||
elif key_ == Mapped[float] or key_ == Mapped[NUMERIC]:
|
||||
return round(float(val), 3) if val else None
|
||||
return True, round(float(val), 3) if val is not None else None
|
||||
elif key_ == Mapped[int]:
|
||||
return int(val) if val else None
|
||||
return True, int(val) if val is not None else None
|
||||
elif key_ == Mapped[TIMESTAMP]:
|
||||
formatted_date = client_arrow.get(str(val)).format(
|
||||
"DD-MM-YYYY HH:mm:ss"
|
||||
)
|
||||
return str(formatted_date) if val else None
|
||||
return True, str(formatted_date) if val is not None else None
|
||||
else:
|
||||
if isinstance(val, datetime.datetime):
|
||||
formatted_date = client_arrow.get(str(val)).format(
|
||||
"DD-MM-YYYY HH:mm:ss"
|
||||
)
|
||||
return str(formatted_date) if val else None
|
||||
return True, str(formatted_date) if val is not None else None
|
||||
elif isinstance(value_type, bool):
|
||||
return bool(val) if val else None
|
||||
return True, bool(val) if val is not None else None
|
||||
elif isinstance(value_type, float) or isinstance(value_type, Decimal):
|
||||
return round(float(val), 3) if val else None
|
||||
return True, round(float(val), 3) if val is not None else None
|
||||
elif isinstance(value_type, int):
|
||||
return int(val) if val else None
|
||||
return True, int(val) if val is not None else None
|
||||
elif isinstance(value_type, str):
|
||||
return str(val) if val else None
|
||||
return True, str(val) if val is not None else None
|
||||
elif isinstance(value_type, type(None)):
|
||||
return None
|
||||
|
||||
return str(val) if val else None
|
||||
return True, None
|
||||
return False, None
|
||||
|
||||
@classmethod
|
||||
def find_or_create(cls, **kwargs):
|
||||
@@ -198,9 +196,11 @@ 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
|
||||
if getattr(cls.creds, "person_id", None) and getattr(
|
||||
cls.creds, "person_name", None
|
||||
):
|
||||
cls.created_by_id = cls.creds.get('person_id', "Unknown")
|
||||
cls.created_by = cls.creds.get('person_name', "Unknown")
|
||||
created_record.flush()
|
||||
return created_record
|
||||
|
||||
@@ -220,13 +220,17 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
setattr(self, key, value)
|
||||
|
||||
if is_confirmed_argument:
|
||||
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
|
||||
if getattr(self.creds, "person_id", None) and getattr(
|
||||
self.creds, "person_name", None
|
||||
):
|
||||
self.confirmed_by_id = self.creds.get('person_id', "Unknown")
|
||||
self.confirmed_by = self.creds.get('person_name', "Unknown")
|
||||
else:
|
||||
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
|
||||
if getattr(self.creds, "person_id", None) and getattr(
|
||||
self.creds, "person_name", None
|
||||
):
|
||||
self.updated_by_id = self.creds.get('person_id', "Unknown")
|
||||
self.updated_by = self.creds.get('person_id', "Unknown")
|
||||
self.flush()
|
||||
return self
|
||||
|
||||
@@ -244,8 +248,8 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
columns_include_list.extend(["uu_id", "active"])
|
||||
for key in list(columns_include_list):
|
||||
val = getattr(self, key)
|
||||
value_of_database = self.iterate_over_variables(val, key)
|
||||
if value_of_database is not None:
|
||||
correct, value_of_database = self.iterate_over_variables(val, key)
|
||||
if correct:
|
||||
return_dict[key] = value_of_database
|
||||
elif exclude:
|
||||
exclude.extend(
|
||||
@@ -261,18 +265,19 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
columns_excluded_list = set(self.columns).difference(set(exclude))
|
||||
for key in list(columns_excluded_list):
|
||||
val = getattr(self, key)
|
||||
value_of_database = self.iterate_over_variables(val, key)
|
||||
if value_of_database is not None:
|
||||
correct, value_of_database = self.iterate_over_variables(val, key)
|
||||
if correct:
|
||||
return_dict[key] = value_of_database
|
||||
else:
|
||||
exclude_list = (
|
||||
self.__exclude__fields__ or [] + self.__system_default_model__
|
||||
)
|
||||
columns_list = list(set(self.columns).difference(set(exclude_list)))
|
||||
columns_list = [columns for columns in columns_list if str(columns)[-2:] != "id" and 'uu_id' not in str(columns)]
|
||||
for key in list(columns_list):
|
||||
val = getattr(self, key)
|
||||
value_of_database = self.iterate_over_variables(val, key)
|
||||
if value_of_database is not None:
|
||||
correct, value_of_database = self.iterate_over_variables(val, key)
|
||||
if correct:
|
||||
return_dict[key] = value_of_database
|
||||
|
||||
# all_arguments = [
|
||||
|
||||
@@ -69,8 +69,7 @@ class Modules(CrudCollection):
|
||||
|
||||
def retrieve_services(self):
|
||||
services = Services.filter_all(
|
||||
Services.module_id == self.id,
|
||||
*Services.valid_record_args(Services)
|
||||
Services.module_id == self.id, *Services.valid_record_args(Services)
|
||||
).data
|
||||
if not services:
|
||||
self.raise_http_exception(
|
||||
@@ -151,8 +150,7 @@ class Event2Employee(CrudCollection):
|
||||
)
|
||||
active_events_id = [event.event_id for event in active_events.data]
|
||||
active_events = Events.filter_all(
|
||||
Events.id.in_(active_events_id),
|
||||
*Events.valid_record_args(Events)
|
||||
Events.id.in_(active_events_id), *Events.valid_record_args(Events)
|
||||
)
|
||||
active_events_uu_id = [str(event.uu_id) for event in active_events.data]
|
||||
return active_events_id, active_events_uu_id
|
||||
|
||||
@@ -37,7 +37,7 @@ class UsersTokens(CrudCollection):
|
||||
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(date=system_arrow.now(),days=3))
|
||||
TIMESTAMP, default=str(system_arrow.shift(date=system_arrow.now(), days=3))
|
||||
)
|
||||
|
||||
# users = relationship("Users", back_populates="tokens", foreign_keys=[user_id])
|
||||
@@ -162,65 +162,21 @@ class Users(CrudCollection, UserLoginModule, SelectAction):
|
||||
return created_user
|
||||
|
||||
@classmethod
|
||||
def created_creds(cls, create_creds: bool = True):
|
||||
person_object = People.find_one(id=cls.person_id)
|
||||
if create_creds:
|
||||
return {
|
||||
"created_by_id": person_object.id,
|
||||
"created_by": str(person_object.firstname)
|
||||
+ " "
|
||||
+ str(person_object.surname),
|
||||
}
|
||||
def credentials(cls):
|
||||
person_object = People.filter_by_one(system=True, id=cls.person_id).data
|
||||
if not person_object:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Person not found. Please contact the admin.",
|
||||
)
|
||||
return {
|
||||
"updated_by_id": person_object.id,
|
||||
"updated_by": str(person_object.firstname)
|
||||
+ " "
|
||||
+ str(person_object.surname),
|
||||
"person_id": person_object.id,
|
||||
"person_uu_id": str(person_object.uu_id),
|
||||
}
|
||||
|
||||
#
|
||||
# def prepare_employee_token_object(self, selected_duty_uu_id):
|
||||
# from database_sql_models import (
|
||||
# Companies,
|
||||
# Employees,
|
||||
# Duties,
|
||||
# Departments,
|
||||
# )
|
||||
# found_person = People.find_one(id=self.person_id)
|
||||
# found_employee = Employees.find_one(people_id=found_person.id)
|
||||
# found_duty = Duties.find_one(uu_id=selected_duty_uu_id)
|
||||
# found_department = Departments.find_one(id=found_duty.department_id)
|
||||
# found_company = Companies.find_one(id=found_department.company_id)
|
||||
# return {
|
||||
# "people": {
|
||||
# "people_id": found_person.id,
|
||||
# "people_uu_id": found_person.uu_id.__str__(),
|
||||
# },
|
||||
# "user": {
|
||||
# "user_id": self.id,
|
||||
# "user_uu_id": self.uu_id.__str__(),
|
||||
# },
|
||||
# "duty": {
|
||||
# "duty_id": found_duty.id,
|
||||
# "duty_uu_id": found_duty.uu_id.__str__()
|
||||
# },
|
||||
# "employee": {
|
||||
# "employee_id": found_employee.id,
|
||||
# "employee_uu_id": found_employee.uu_id.__str__(),
|
||||
# "people_id": found_employee.people_id,
|
||||
# },
|
||||
# "department": {
|
||||
# "department_id": found_department.id,
|
||||
# "department_uu_id": found_department.uu_id.__str__(),
|
||||
# },
|
||||
# "company": {
|
||||
# "company_id": found_company.id,
|
||||
# "company_uu_id": found_company.uu_id.__str__()
|
||||
# },
|
||||
# }
|
||||
|
||||
def get_employee_and_duty_details(self):
|
||||
from databases import Employees, Duties
|
||||
|
||||
found_person = People.find_one(id=self.person_id)
|
||||
found_employees = Employees.filter_by_active(
|
||||
people_id=found_person.id, is_confirmed=True
|
||||
@@ -383,6 +339,7 @@ class People(CrudCollection, SelectAction):
|
||||
@classmethod
|
||||
def create_action(cls, data: InsertPerson, token):
|
||||
from databases import Employees, Duties
|
||||
|
||||
token_duties_id, token_company_id = (
|
||||
token.selected_company.duty_id,
|
||||
token.selected_company.company_id,
|
||||
@@ -498,7 +455,9 @@ class Addresses(CrudCollection):
|
||||
post_code_list = RelationshipEmployee2PostCode.filter_all(
|
||||
RelationshipEmployee2PostCode.employee_id
|
||||
== token_dict.selected_company.employee_id,
|
||||
*RelationshipEmployee2PostCode.valid_record_args(RelationshipEmployee2PostCode)
|
||||
*RelationshipEmployee2PostCode.valid_record_args(
|
||||
RelationshipEmployee2PostCode
|
||||
),
|
||||
).data
|
||||
post_code_id_list = [post_code.member_id for post_code in post_code_list]
|
||||
if not post_code_id_list:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy import SQLColumnExpression
|
||||
from sqlalchemy import BinaryExpression
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from api_validations.validations_request import ListOptions
|
||||
@@ -94,10 +94,15 @@ class FilterAttributes:
|
||||
|
||||
@classmethod
|
||||
def add_new_arg_to_args(cls, args_list, argument, value):
|
||||
new_arg_list = list(set(
|
||||
args_ for args_ in list(args_list) if isinstance(args_, SQLColumnExpression)
|
||||
))
|
||||
arg_left = lambda arg_obj: getattr(getattr(arg_obj, "left", None), 'key', None)
|
||||
new_arg_list = list(
|
||||
set(
|
||||
args_
|
||||
for args_ in list(args_list)
|
||||
if isinstance(args_, BinaryExpression)
|
||||
)
|
||||
)
|
||||
arg_left = lambda arg_obj: getattr(getattr(arg_obj, "left", None), "key", None)
|
||||
# arg_right = lambda arg_obj: getattr(getattr(arg_obj, "right", None), "value", None)
|
||||
if not any(True for arg in new_arg_list if arg_left(arg_obj=arg) == argument):
|
||||
new_arg_list.append(value)
|
||||
return tuple(new_arg_list)
|
||||
@@ -108,7 +113,7 @@ class FilterAttributes:
|
||||
from api_library.date_time_actions.date_functions import system_arrow
|
||||
|
||||
arg = cls.add_new_arg_to_args(
|
||||
arg, "expiry_ends", cls.expiry_ends >= str(system_arrow.now())
|
||||
arg, "expiry_ends", cls.expiry_ends > str(system_arrow.now())
|
||||
)
|
||||
arg = cls.add_new_arg_to_args(
|
||||
arg, "expiry_starts", cls.expiry_starts <= str(system_arrow.now())
|
||||
|
||||
Reference in New Issue
Block a user