migrator completed
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import typing
|
||||
from operator import or_
|
||||
from datetime import datetime, timedelta
|
||||
from platform import system
|
||||
from typing import List
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
@@ -174,10 +175,10 @@ class Build(CrudCollection, SelectActionWithEmployee):
|
||||
Integer, nullable=True, comment="Management Room ID"
|
||||
)
|
||||
|
||||
site_id: Mapped[int] = mapped_column(ForeignKey("build_sites.id"))
|
||||
site_uu_id: Mapped[str] = mapped_column(String, comment="Site UUID")
|
||||
address_id: Mapped[int] = mapped_column(ForeignKey("addresses.id"))
|
||||
address_uu_id: Mapped[str] = mapped_column(String, comment="Address UUID")
|
||||
site_id: Mapped[int] = mapped_column(ForeignKey("build_sites.id"), nullable=True)
|
||||
site_uu_id: Mapped[str] = mapped_column(String, comment="Site UUID", nullable=True)
|
||||
address_id: Mapped[int] = mapped_column(ForeignKey("addresses.id"), nullable=False)
|
||||
address_uu_id: Mapped[str] = mapped_column(String, comment="Address UUID", nullable=False)
|
||||
build_types_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("build_types.id"), nullable=False, comment="Building Type"
|
||||
)
|
||||
@@ -243,22 +244,24 @@ class Build(CrudCollection, SelectActionWithEmployee):
|
||||
).data
|
||||
data_dict["address_id"] = official_address.id
|
||||
data_dict["build_no"] = str(official_address.build_number)
|
||||
data_dict.pop("address_uu_id", None)
|
||||
if not data_dict["address_id"]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Address is not found in database. Re-enter address record then try again.",
|
||||
)
|
||||
build_type = BuildTypes.find_one(uu_id=str(data.build_types_uu_id))
|
||||
build_type = BuildTypes.filter_by_one(system=True, uu_id=str(data.build_types_uu_id)).data
|
||||
data_dict["build_types_id"] = build_type.id
|
||||
del data_dict["build_types_uu_id"]
|
||||
build_created = cls.find_or_create(**data_dict)
|
||||
cls.__many__table__.find_or_create(
|
||||
created_build_relation = cls.__many__table__.find_or_create(
|
||||
company_id=token.selected_company.company_id,
|
||||
employee_id=token.selected_company.employee_id,
|
||||
member_id=build_created.id,
|
||||
is_confirmed=True,
|
||||
)
|
||||
build_created.save()
|
||||
build_created.update(is_confirmed=True)
|
||||
build_created.save()
|
||||
created_build_relation.update(is_confirmed=True)
|
||||
created_build_relation.save()
|
||||
return build_created
|
||||
|
||||
@classmethod
|
||||
@@ -316,7 +319,7 @@ class Build(CrudCollection, SelectActionWithEmployee):
|
||||
building_types = None
|
||||
for part in self.parts:
|
||||
building_types = {}
|
||||
build_type = BuildTypes.find_one(id=part.build_part_type_id)
|
||||
build_type = BuildTypes.filter_by_one(system=True, id=part.build_part_type_id).data
|
||||
if build_type.type_code in building_types:
|
||||
building_types[build_type.type_code]["list"].append(part.part_no)
|
||||
else:
|
||||
@@ -412,13 +415,14 @@ class BuildParts(CrudCollection):
|
||||
building = build_from_duty.first()
|
||||
if not building:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_418_IM_A_TEAPOT,
|
||||
status_code=status.HTTP_406_NOT_ACCEPTABLE,
|
||||
detail="This Employee can not reach this building or building uu-id not found in database. "
|
||||
"Check with your supervisor.",
|
||||
)
|
||||
|
||||
if build_types := BuildTypes.filter_one(
|
||||
BuildTypes.uu_id == data.build_part_type_uu_id
|
||||
BuildTypes.uu_id == data.build_part_type_uu_id,
|
||||
*BuildTypes.valid_record_args(BuildTypes)
|
||||
).data:
|
||||
part_direction = ApiEnumDropdown.get_by_uuid(
|
||||
uuid=str(data.part_direction_uu_id)
|
||||
@@ -467,7 +471,7 @@ class BuildParts(CrudCollection):
|
||||
|
||||
@property
|
||||
def part_name(self):
|
||||
if build_type := BuildTypes.find_one(id=self.build_part_type_id):
|
||||
if build_type := BuildTypes.filter_by_one(system=True, id=self.build_part_type_id).data:
|
||||
return f"{str(build_type.type_name).upper()} : {str(self.part_no).upper()}"
|
||||
return f"Undefined:{str(build_type.type_name).upper()}"
|
||||
|
||||
@@ -594,6 +598,7 @@ class BuildArea(CrudCollection):
|
||||
"""
|
||||
|
||||
__tablename__ = "build_area"
|
||||
__exclude__fields__ = []
|
||||
|
||||
area_name: Mapped[str] = mapped_column(String, server_default="")
|
||||
area_code: Mapped[str] = mapped_column(String, server_default="")
|
||||
|
||||
@@ -125,35 +125,34 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
row_attr = bool(getattr(getattr(cls, key), "foreign_keys", None))
|
||||
if is_primary or row_attr and key_ == Mapped[int]:
|
||||
return False, None
|
||||
|
||||
if key_:
|
||||
elif val is None:
|
||||
return True, None
|
||||
elif str(key[-5:]).lower() == "uu_id":
|
||||
return True, str(val)
|
||||
elif key_:
|
||||
if key_ == Mapped[int]:
|
||||
return True, int(val) if val is not None else None
|
||||
return True, int(val)
|
||||
elif key_ == Mapped[bool]:
|
||||
return True, bool(val) if val is not None else None
|
||||
return True, bool(val)
|
||||
elif key_ == Mapped[float] or key_ == Mapped[NUMERIC]:
|
||||
return True, round(float(val), 3) if val is not None else None
|
||||
return True, round(float(val), 3)
|
||||
elif key_ == Mapped[int]:
|
||||
return True, int(val) if val is not None else None
|
||||
return True, int(val)
|
||||
elif key_ == Mapped[TIMESTAMP]:
|
||||
formatted_date = client_arrow.get(str(val)).format(
|
||||
"DD-MM-YYYY HH:mm:ss"
|
||||
)
|
||||
return True, str(formatted_date) if val is not None else None
|
||||
return True, str(client_arrow.get(str(val)).format("DD-MM-YYYY HH:mm:ss"))
|
||||
elif key_ == Mapped[str]:
|
||||
return True, str(val)
|
||||
else:
|
||||
if isinstance(val, datetime.datetime):
|
||||
formatted_date = client_arrow.get(str(val)).format(
|
||||
"DD-MM-YYYY HH:mm:ss"
|
||||
)
|
||||
return True, str(formatted_date) if val is not None else None
|
||||
return True, str(client_arrow.get(str(val)).format("DD-MM-YYYY HH:mm:ss"))
|
||||
elif isinstance(value_type, bool):
|
||||
return True, bool(val) if val is not None else None
|
||||
return True, bool(val)
|
||||
elif isinstance(value_type, float) or isinstance(value_type, Decimal):
|
||||
return True, round(float(val), 3) if val is not None else None
|
||||
return True, round(float(val), 3)
|
||||
elif isinstance(value_type, int):
|
||||
return True, int(val) if val is not None else None
|
||||
return True, int(val)
|
||||
elif isinstance(value_type, str):
|
||||
return True, str(val) if val is not None else None
|
||||
return True, str(val)
|
||||
elif isinstance(value_type, type(None)):
|
||||
return True, None
|
||||
return False, None
|
||||
@@ -242,10 +241,12 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
exclude_list = [
|
||||
element
|
||||
for element in self.__system_default_model__
|
||||
if str(element)[-2:] == "id"
|
||||
if str(element)[-2:] == "id" and str(element)[-5:].lower() == 'uu_id'
|
||||
]
|
||||
columns_include_list = list(set(include).difference(set(exclude_list)))
|
||||
columns_include_list.extend(["uu_id", "active"])
|
||||
# columns_include_list.extend([column for column in self.columns if str(column)[-5:].lower() == 'uu_id'])
|
||||
|
||||
columns_include_list.extend(['uu_id'])
|
||||
for key in list(columns_include_list):
|
||||
val = getattr(self, key)
|
||||
correct, value_of_database = self.iterate_over_variables(val, key)
|
||||
@@ -262,7 +263,10 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
if str(element)[-2:] == "id"
|
||||
]
|
||||
)
|
||||
columns_excluded_list = set(self.columns).difference(set(exclude))
|
||||
columns_excluded_list = list(set(self.columns).difference(set(exclude)))
|
||||
# columns_excluded_list.extend([column for column in self.columns if str(column)[-5:].lower() == 'uu_id'])
|
||||
columns_excluded_list.extend(['uu_id', 'active'])
|
||||
|
||||
for key in list(columns_excluded_list):
|
||||
val = getattr(self, key)
|
||||
correct, value_of_database = self.iterate_over_variables(val, key)
|
||||
@@ -274,16 +278,17 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
)
|
||||
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)
|
||||
columns for columns in columns_list if str(columns)[-2:] != "id"
|
||||
]
|
||||
columns_list.extend([column for column in self.columns if str(column)[-5:].lower() == 'uu_id'])
|
||||
for remove_field in self.__system_default_model__:
|
||||
if remove_field in columns_list:
|
||||
columns_list.remove(remove_field)
|
||||
for key in list(columns_list):
|
||||
val = getattr(self, key)
|
||||
correct, value_of_database = self.iterate_over_variables(val, key)
|
||||
if correct:
|
||||
return_dict[key] = value_of_database
|
||||
|
||||
# all_arguments = [
|
||||
# record
|
||||
# for record in self.__class__.__dict__
|
||||
@@ -305,7 +310,8 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
# return_dict[all_argument] = (
|
||||
# populate_arg.get_dict() if populate_arg else []
|
||||
# )
|
||||
return dict(sorted(return_dict.items(), reverse=False))
|
||||
# return dict(sorted(return_dict.items(), reverse=False))
|
||||
return return_dict
|
||||
|
||||
|
||||
class BaseMixin(CrudMixin, ReprMixin, SerializeMixin, FilterAttributes):
|
||||
|
||||
@@ -9,10 +9,10 @@ from sqlalchemy.orm import (
|
||||
Mapped,
|
||||
mapped_column,
|
||||
)
|
||||
from databases.sql_models.core_mixin import BaseCollection
|
||||
from databases.sql_models.core_mixin import CrudCollection
|
||||
|
||||
|
||||
class ApiEnumDropdown(BaseCollection):
|
||||
class ApiEnumDropdown(CrudCollection):
|
||||
__tablename__ = "api_enum_dropdown"
|
||||
__exclude__fields__ = ["enum_class"]
|
||||
|
||||
@@ -31,35 +31,30 @@ class ApiEnumDropdown(BaseCollection):
|
||||
|
||||
@classmethod
|
||||
def get_by_uuid(cls, uuid: str):
|
||||
return cls.query.filter(cls.uu_id == uuid).first()
|
||||
return cls.filter_by_one(system=True, uu_id=str(uuid)).data
|
||||
|
||||
@classmethod
|
||||
def get_debit_search(cls, search_debit: str = None, search_uu_id: str = None):
|
||||
if search_uu_id:
|
||||
if search := cls.query.filter(
|
||||
cls.enum_class.in_(["DebitTypes"]),
|
||||
cls.uu_id == search_uu_id,
|
||||
cls.active == True,
|
||||
).first():
|
||||
if search := cls.filter_one(
|
||||
cls.enum_class.in_(["DebitTypes"]), cls.uu_id == search_uu_id, system=True
|
||||
).data:
|
||||
return search
|
||||
elif search_debit:
|
||||
if search := cls.query.filter(
|
||||
cls.enum_class.in_(["DebitTypes"]),
|
||||
cls.key == search_debit,
|
||||
cls.active == True,
|
||||
).first():
|
||||
if search := cls.filter_one(
|
||||
cls.enum_class.in_(["DebitTypes"]), cls.key == search_debit, system=True
|
||||
).data:
|
||||
return search
|
||||
return cls.query.filter(
|
||||
cls.enum_class.in_(["DebitTypes"]),
|
||||
cls.active == True,
|
||||
).all()
|
||||
return cls.filter_all(
|
||||
cls.enum_class.in_(["DebitTypes"]), system=True
|
||||
).data
|
||||
|
||||
@classmethod
|
||||
def get_due_types(cls):
|
||||
if due_list := cls.filter_all(
|
||||
cls.enum_class == "BuildDuesTypes",
|
||||
cls.key.in_(["BDT-A", "BDT-D"]),
|
||||
cls.active == True,
|
||||
system=True
|
||||
).data:
|
||||
return [due.uu_id.__str__() for due in due_list]
|
||||
raise HTTPException(
|
||||
@@ -70,23 +65,21 @@ class ApiEnumDropdown(BaseCollection):
|
||||
@classmethod
|
||||
def due_type_search(cls, search_management: str = None, search_uu_id: str = None):
|
||||
if search_uu_id:
|
||||
if search := cls.query.filter(
|
||||
cls.enum_class.in_(["BuildDuesTypes"]),
|
||||
cls.uu_id == search_uu_id,
|
||||
cls.active == True,
|
||||
).first():
|
||||
if search := cls.filter_one(
|
||||
cls.enum_class.in_(["BuildDuesTypes"]), cls.uu_id == search_uu_id,
|
||||
system=True
|
||||
).data:
|
||||
return search
|
||||
elif search_management:
|
||||
if search := cls.query.filter(
|
||||
cls.enum_class.in_(["BuildDuesTypes"]),
|
||||
cls.key == search_management,
|
||||
cls.active == True,
|
||||
).first():
|
||||
if search := cls.filter_one(
|
||||
cls.enum_class.in_(["BuildDuesTypes"]), cls.key == search_management,
|
||||
system=True
|
||||
).data:
|
||||
return search
|
||||
return cls.query.filter(
|
||||
return cls.filter_all(
|
||||
cls.enum_class.in_(["BuildDuesTypes"]),
|
||||
cls.active == True,
|
||||
).all()
|
||||
system=True
|
||||
).data
|
||||
|
||||
def get_enum_dict(self):
|
||||
return {
|
||||
@@ -101,7 +94,7 @@ class ApiEnumDropdown(BaseCollection):
|
||||
def uuid_of_enum(cls, enum_class: str, key: str):
|
||||
return str(
|
||||
getattr(
|
||||
cls.filter_one(cls.enum_class == enum_class, cls.key == key).data,
|
||||
cls.filter_one(cls.enum_class == enum_class, cls.key == key, system=True).data,
|
||||
"uu_id",
|
||||
None,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user