updated handler exceptions
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
import hashlib
|
||||
import uuid
|
||||
import requests
|
||||
|
||||
import secrets
|
||||
from datetime import timedelta
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
from api_services.redis.functions import RedisActions
|
||||
from databases.no_sql_models.validations import (
|
||||
PasswordHistoryViaUser,
|
||||
AccessHistoryViaUser,
|
||||
)
|
||||
from api_library.date_time_actions.date_functions import system_arrow, client_arrow
|
||||
from api_library.date_time_actions.date_functions import system_arrow
|
||||
from api_configs import ApiStatic, Auth
|
||||
|
||||
|
||||
class PasswordModule:
|
||||
@staticmethod
|
||||
def generate_token(length=32):
|
||||
return uuid.uuid4().__str__()[:length]
|
||||
return secrets.token_urlsafe(length)
|
||||
|
||||
@staticmethod
|
||||
def create_hashed_password(domain: str, id_: str, password: str):
|
||||
@@ -25,6 +26,10 @@ class PasswordModule:
|
||||
def check_password(cls, domain, id_, password, password_hashed):
|
||||
return cls.create_hashed_password(domain, id_, password) == password_hashed
|
||||
|
||||
@classmethod
|
||||
def generate_access_token(cls):
|
||||
return secrets.token_urlsafe(Auth.ACCESS_TOKEN_LENGTH)
|
||||
|
||||
|
||||
class AuthModule(PasswordModule):
|
||||
|
||||
@@ -33,6 +38,7 @@ class AuthModule(PasswordModule):
|
||||
from databases import Users
|
||||
from sqlalchemy import or_
|
||||
from fastapi import status
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
found_user: Users = Users.query.filter(
|
||||
or_(
|
||||
@@ -56,9 +62,6 @@ class AuthModule(PasswordModule):
|
||||
)
|
||||
return found_user
|
||||
|
||||
def generate_access_token(self):
|
||||
return self.generate_token(Auth.ACCESS_TOKEN_LENGTH)
|
||||
|
||||
def remove_refresher_token(self, domain, disconnect: bool = False):
|
||||
from databases import UsersTokens
|
||||
|
||||
@@ -76,6 +79,8 @@ class AuthModule(PasswordModule):
|
||||
UsersTokens.save()
|
||||
|
||||
def check_password_is_different(self, password):
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
main_domain = self.get_main_domain_and_other_domains(get_main_domain=True)
|
||||
if self.hash_password == self.create_hashed_password(
|
||||
domain=main_domain, id_=self.uu_id, password=password
|
||||
@@ -88,9 +93,9 @@ class AuthModule(PasswordModule):
|
||||
@staticmethod
|
||||
def create_password(found_user, password, password_token=None):
|
||||
from databases import MongoQueryIdentity, Users
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
found_user: Users = found_user
|
||||
|
||||
if found_user.password_token:
|
||||
replace_day = 0
|
||||
try:
|
||||
@@ -145,9 +150,7 @@ class AuthModule(PasswordModule):
|
||||
return found_user.password_token
|
||||
|
||||
def generate_refresher_token(self, domain: str, remember_me=False):
|
||||
from databases import (
|
||||
UsersTokens,
|
||||
)
|
||||
from databases import UsersTokens
|
||||
|
||||
if remember_me:
|
||||
refresh_token = self.generate_token(Auth.REFRESHER_TOKEN_LENGTH)
|
||||
@@ -183,6 +186,7 @@ class AuthModule(PasswordModule):
|
||||
|
||||
|
||||
class UserLoginModule(AuthModule):
|
||||
|
||||
@classmethod
|
||||
def set_login_details_to_mongo_database(
|
||||
cls, found_user, headers_request, access_token, record_id
|
||||
@@ -212,8 +216,8 @@ class UserLoginModule(AuthModule):
|
||||
"address": address_package,
|
||||
"user_id": found_user.id,
|
||||
}
|
||||
already_exits = mongo_db.mongo_engine.filter_by(filter_query) or None
|
||||
no_address_validates = mongo_db.mongo_engine.get_all()[0] == 0
|
||||
already_exits = mongo_db.mongo_engine.filter_by(filter_query).data
|
||||
no_address_validates = mongo_db.mongo_engine.get_all().data
|
||||
access_via_user = query_engine.update_access_history_via_user(
|
||||
AccessHistoryViaUser(
|
||||
**{
|
||||
@@ -241,7 +245,6 @@ class UserLoginModule(AuthModule):
|
||||
}
|
||||
},
|
||||
)
|
||||
print("update_mongo", update_mongo)
|
||||
else:
|
||||
insert_mongo = mongo_db.mongo_engine.insert(
|
||||
payload={
|
||||
@@ -257,14 +260,14 @@ class UserLoginModule(AuthModule):
|
||||
"is_first": True if no_address_validates else False,
|
||||
}
|
||||
)
|
||||
print("insert_mongo", insert_mongo)
|
||||
|
||||
@classmethod
|
||||
def login_user_with_credentials(cls, data, request):
|
||||
from databases.sql_models.identity.identity import Users
|
||||
from ApiServices.api_handlers.auth_actions.auth import AuthActions
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
found_user = Users.check_user_exits(
|
||||
found_user: Users = Users.check_user_exits(
|
||||
access_key=data.access_key, domain=data.domain
|
||||
)
|
||||
if not found_user:
|
||||
@@ -280,19 +283,20 @@ class UserLoginModule(AuthModule):
|
||||
)
|
||||
|
||||
if found_user.check_password(
|
||||
domain=data.domain,
|
||||
id_=found_user.uu_id,
|
||||
password_hashed=found_user.hash_password,
|
||||
password=data.password,
|
||||
):
|
||||
domain=data.domain,
|
||||
id_=found_user.uu_id,
|
||||
password_hashed=found_user.hash_password,
|
||||
password=data.password,
|
||||
):
|
||||
access_object_to_redis = AuthActions.save_access_token_to_redis(
|
||||
request=request,
|
||||
found_user=found_user,
|
||||
domain=data.domain,
|
||||
# remember_me=data.remember_me,
|
||||
)
|
||||
print("access_object_to_redis", access_object_to_redis)
|
||||
access_token = access_object_to_redis.get("access_token")
|
||||
|
||||
access_object_to_redis["user"] = found_user
|
||||
headers_request = dict(request.headers)
|
||||
headers_request["evyos-user-agent"] = headers_request.get("user-agent")
|
||||
headers_request["evyos-platform"] = headers_request.get("user-agent")
|
||||
|
||||
@@ -17,7 +17,7 @@ from sqlalchemy import (
|
||||
Numeric,
|
||||
Integer,
|
||||
)
|
||||
from sqlalchemy.orm import mapped_column, Mapped, relationship
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from api_validations.validations_request import (
|
||||
InsertDecisionBook,
|
||||
@@ -26,6 +26,19 @@ from api_validations.validations_request import (
|
||||
InsertBuildDecisionBookProjects,
|
||||
)
|
||||
from databases.sql_models.core_mixin import CrudCollection
|
||||
from databases.language_models.building.decision_book import (
|
||||
BuildDecisionBookLanguageModel,
|
||||
BuildDecisionBookInvitationsLanguageModel,
|
||||
BuildDecisionBookPersonLanguageModel,
|
||||
BuildDecisionBookPersonOccupantsLanguageModel,
|
||||
BuildDecisionBookItemsLanguageModel,
|
||||
BuildDecisionBookItemsUnapprovedLanguageModel,
|
||||
BuildDecisionBookPaymentsLanguageModel,
|
||||
BuildDecisionBookLegalLanguageModel,
|
||||
BuildDecisionBookProjectsLanguageModel,
|
||||
BuildDecisionBookProjectPersonLanguageModel,
|
||||
BuildDecisionBookProjectItemsLanguageModel,
|
||||
)
|
||||
|
||||
|
||||
class BuildDecisionBook(CrudCollection):
|
||||
@@ -44,6 +57,7 @@ class BuildDecisionBook(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookLanguageModel
|
||||
|
||||
decision_book_pdf_path: Mapped[str] = mapped_column(
|
||||
String, server_default="", nullable=True
|
||||
@@ -242,6 +256,7 @@ class BuildDecisionBookInvitations(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book_invitations"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookInvitationsLanguageModel
|
||||
|
||||
build_id: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
build_uu_id: Mapped[str] = mapped_column(
|
||||
@@ -341,6 +356,7 @@ class BuildDecisionBookPerson(CrudCollection):
|
||||
__tablename__ = "build_decision_book_person"
|
||||
__exclude__fields__ = []
|
||||
__enum_list__ = [("management_typecode", "BuildManagementType", "bm")]
|
||||
__language_model__ = BuildDecisionBookPersonLanguageModel
|
||||
|
||||
dues_percent_discount: Mapped[int] = mapped_column(SmallInteger, server_default="0")
|
||||
dues_fix_discount: Mapped[float] = mapped_column(Numeric(10, 2), server_default="0")
|
||||
@@ -517,6 +533,7 @@ class BuildDecisionBookPersonOccupants(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book_person_occupants"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookPersonOccupantsLanguageModel
|
||||
|
||||
build_decision_book_person_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("build_decision_book_person.id"), nullable=False
|
||||
@@ -559,6 +576,7 @@ class BuildDecisionBookItems(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book_items"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookItemsLanguageModel
|
||||
|
||||
item_order: Mapped[int] = mapped_column(
|
||||
SmallInteger, nullable=False, comment="Order Number of Item"
|
||||
@@ -799,6 +817,7 @@ class BuildDecisionBookItemsUnapproved(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book_items_unapproved"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookItemsUnapprovedLanguageModel
|
||||
|
||||
item_objection: Mapped[str] = mapped_column(
|
||||
Text, nullable=False, comment="Objection Content"
|
||||
@@ -841,6 +860,7 @@ class BuildDecisionBookPayments(CrudCollection):
|
||||
__tablename__ = "build_decision_book_payments"
|
||||
__exclude__fields__ = []
|
||||
__enum_list__ = [("receive_debit", "DebitTypes", "D")]
|
||||
__language_model__ = BuildDecisionBookPaymentsLanguageModel
|
||||
|
||||
payment_plan_time_periods: Mapped[str] = mapped_column(
|
||||
String(10), nullable=False, comment="Payment Plan Time Periods"
|
||||
@@ -951,6 +971,7 @@ class BuildDecisionBookLegal(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book_legal"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookLegalLanguageModel
|
||||
|
||||
period_start_date: Mapped[TIMESTAMP] = mapped_column(
|
||||
TIMESTAMP(timezone=True), nullable=False, comment="Start Date of Legal Period"
|
||||
@@ -1027,6 +1048,7 @@ class BuildDecisionBookProjects(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book_projects"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookProjectsLanguageModel
|
||||
|
||||
project_no: Mapped[str] = mapped_column(
|
||||
String(12), nullable=True, comment="Project Number of Decision Book"
|
||||
@@ -1194,6 +1216,7 @@ class BuildDecisionBookProjectPerson(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book_project_person"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookProjectPersonLanguageModel
|
||||
# __enum_list__ = [("management_typecode", "ProjectTeamTypes", "PTT-EMP")]
|
||||
|
||||
dues_percent_discount: Mapped[int] = mapped_column(SmallInteger, server_default="0")
|
||||
@@ -1226,6 +1249,7 @@ class BuildDecisionBookProjectItems(CrudCollection):
|
||||
|
||||
__tablename__ = "build_decision_book_project_items"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = BuildDecisionBookProjectItemsLanguageModel
|
||||
|
||||
item_header: Mapped[str] = mapped_column(
|
||||
String, nullable=False, comment="Item Header"
|
||||
|
||||
@@ -5,6 +5,13 @@ from sqlalchemy import (
|
||||
Numeric,
|
||||
)
|
||||
from sqlalchemy.orm import mapped_column, Mapped
|
||||
|
||||
from databases.language_models.company.employee import (
|
||||
StaffLanguageModel,
|
||||
EmployeesLanguageModel,
|
||||
EmployeeHistoryLanguageModel,
|
||||
EmployeesSalariesLanguageModel,
|
||||
)
|
||||
from databases.sql_models.core_mixin import CrudCollection
|
||||
|
||||
from api_validations.validations_request import InsertCompanyEmployees
|
||||
@@ -14,6 +21,7 @@ class Staff(CrudCollection):
|
||||
|
||||
__tablename__ = "staff"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = StaffLanguageModel
|
||||
|
||||
staff_description: Mapped[str] = mapped_column(
|
||||
String, server_default="", comment="Staff Description"
|
||||
@@ -61,6 +69,7 @@ class Employees(CrudCollection):
|
||||
|
||||
__tablename__ = "employees"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = EmployeesLanguageModel
|
||||
|
||||
staff_id: Mapped[int] = mapped_column(ForeignKey("staff.id"))
|
||||
staff_uu_id: Mapped[str] = mapped_column(
|
||||
@@ -81,6 +90,7 @@ class EmployeeHistory(CrudCollection):
|
||||
|
||||
__tablename__ = "employee_history"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = EmployeeHistoryLanguageModel
|
||||
|
||||
staff_id: Mapped[int] = mapped_column(
|
||||
ForeignKey("staff.id"), nullable=False, comment="Staff ID"
|
||||
@@ -105,6 +115,7 @@ class EmployeesSalaries(CrudCollection):
|
||||
|
||||
__tablename__ = "employee_salaries"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = EmployeesSalariesLanguageModel
|
||||
|
||||
gross_salary: Mapped[float] = mapped_column(
|
||||
Numeric(20, 6), nullable=False, comment="Gross Salary"
|
||||
|
||||
@@ -21,7 +21,7 @@ from sqlalchemy_mixins.serialize import SerializeMixin
|
||||
from sqlalchemy_mixins.repr import ReprMixin
|
||||
from sqlalchemy_mixins.smartquery import SmartQueryMixin
|
||||
|
||||
from api_library import DateTimeLocal, client_arrow, system_arrow
|
||||
from api_library import DateTimeLocal, system_arrow
|
||||
from databases.sql_models.sql_operations import FilterAttributes
|
||||
from databases.sql_models.postgres_database import Base
|
||||
|
||||
@@ -138,14 +138,14 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
|
||||
return True, int(val)
|
||||
elif key_ == Mapped[TIMESTAMP]:
|
||||
return True, str(
|
||||
cls.client_arrow.get(str(val)).format("DD-MM-YYYY HH:mm:ss")
|
||||
cls.client_arrow.get(str(val)).format("DD-MM-YYYY HH:mm:ss +0")
|
||||
)
|
||||
elif key_ == Mapped[str]:
|
||||
return True, str(val)
|
||||
else:
|
||||
if isinstance(val, datetime.datetime):
|
||||
return True, str(
|
||||
cls.client_arrow.get(str(val)).format("DD-MM-YYYY HH:mm:ss")
|
||||
cls.client_arrow.get(str(val)).format("DD-MM-YYYY HH:mm:ss +0")
|
||||
)
|
||||
elif isinstance(value_type, bool):
|
||||
return True, bool(val)
|
||||
|
||||
@@ -122,6 +122,9 @@ class Users(CrudCollection, UserLoginModule, SelectAction):
|
||||
person_uu_id: Mapped[str] = mapped_column(
|
||||
String, server_default="", comment="Person UUID", index=True
|
||||
)
|
||||
local_timezone = mapped_column(
|
||||
String, server_default="GMT+3", comment="Local timezone of user"
|
||||
)
|
||||
person = relationship("People", back_populates="user", foreign_keys=[person_id])
|
||||
|
||||
@property
|
||||
@@ -183,11 +186,6 @@ class Users(CrudCollection, UserLoginModule, SelectAction):
|
||||
@classmethod
|
||||
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.",
|
||||
# )
|
||||
if person_object:
|
||||
return {
|
||||
"person_id": person_object.id,
|
||||
|
||||
@@ -15,6 +15,7 @@ from databases.sql_models.core_mixin import CrudCollection
|
||||
class ApiEnumDropdown(CrudCollection):
|
||||
__tablename__ = "api_enum_dropdown"
|
||||
__exclude__fields__ = ["enum_class"]
|
||||
__language_model__ = None
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
uu_id: Mapped[str] = mapped_column(
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from sqlalchemy import String, Boolean
|
||||
from databases.sql_models.core_mixin import CrudCollection
|
||||
from sqlalchemy import String
|
||||
from sqlalchemy.orm import mapped_column, Mapped
|
||||
|
||||
from databases.language_models.rules.rules import EndpointRestrictionLanguageModel
|
||||
from databases.sql_models.core_mixin import CrudCollection
|
||||
|
||||
|
||||
class EndpointRestriction(CrudCollection):
|
||||
"""
|
||||
@@ -10,6 +12,7 @@ class EndpointRestriction(CrudCollection):
|
||||
|
||||
__tablename__ = "endpoint_restriction"
|
||||
__exclude__fields__ = []
|
||||
__language_model__ = EndpointRestrictionLanguageModel
|
||||
|
||||
endpoint_function: Mapped[str] = mapped_column(
|
||||
String, server_default="", comment="Function name of the API endpoint"
|
||||
|
||||
@@ -50,18 +50,9 @@ class FilterAttributes:
|
||||
"""Save the data via the metadata."""
|
||||
try:
|
||||
meta_data = getattr(cls, "meta_data", {})
|
||||
meta_data_created = meta_data.get("created", False)
|
||||
if meta_data_created:
|
||||
print("meta_data_created commit", meta_data_created)
|
||||
if meta_data.get("created", False):
|
||||
cls.__session__.commit()
|
||||
print("meta_data_created rollback", meta_data_created)
|
||||
cls.__session__.rollback()
|
||||
# cls.raise_http_exception(
|
||||
# status_code="HTTP_304_NOT_MODIFIED",
|
||||
# error_case=meta_data.get("error_case", "Error on save and commit"),
|
||||
# data={},
|
||||
# message=meta_data.get("message", "Error on save and commit"),
|
||||
# )
|
||||
except SQLAlchemyError as e:
|
||||
cls.raise_http_exception(
|
||||
status_code="HTTP_304_NOT_MODIFIED",
|
||||
@@ -233,8 +224,9 @@ class FilterAttributes:
|
||||
for smart_iter in cls.filter_expr(**filter_list.get("query", {})):
|
||||
if key := arg_left(smart_iter):
|
||||
args = cls.add_new_arg_to_args(args, key, smart_iter)
|
||||
query = cls._query().filter(*args)
|
||||
query = cls._query()
|
||||
cls.total_count = query.count()
|
||||
query = query.filter(*args)
|
||||
if cls.filter_attr:
|
||||
data_query = cls.add_query_to_filter(query, filter_list)
|
||||
cls.filter_attr = None
|
||||
|
||||
Reference in New Issue
Block a user