feat: user routes

fix: global delete filter
refactor: user update schema
This commit is contained in:
2025-05-12 18:46:11 +05:30
parent 8d8f205eb1
commit c235196990
6 changed files with 145 additions and 26 deletions
+16 -20
View File
@@ -22,26 +22,22 @@ class CustomBase:
session.add(self)
session.commit()
# Global filter for deleted records
@staticmethod
@event.listens_for(Query, "before_compile", retval=True)
def _filter_deleted(query):
# Skip filtering if this query explicitly asks to include deleted items
for option in query._with_options:
if getattr(option, 'name', None) == 'include_deleted':
return query
# Find entities that inherit from CustomBase
for entity in query._entities:
if hasattr(entity, 'entity_zero'):
entity_zero = entity.entity_zero
if hasattr(entity_zero, 'mapper') and entity_zero.mapper:
mapper = entity_zero.mapper
if issubclass(mapper.class_, CustomBase):
# Apply filter for this entity
query = query.filter(mapper.class_.deleted_at.is_(None))
return query
# Global filter for deleted records
@event.listens_for(SessionLocal, "do_orm_execute")
def _add_filtering_criteria(execute_state):
if (
execute_state.is_select
and not execute_state.execution_options.get("include_deleted", False)
):
# Check if any of the entities inherit from CustomBase
for entity in execute_state.statement.column_descriptions:
entity_class = entity.get("entity", None)
if entity_class and issubclass(entity_class, CustomBase):
# Add filter condition to exclude soft-deleted records
execute_state.statement = execute_state.statement.filter(
entity_class.deleted_at.is_(None)
)
break
# Option to include deleted records
class IncludeDeleted(object):