tools2fast-fastapi 0.3.14__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tools2fast_fastapi-0.3.14/src/tools2fast_fastapi.egg-info → tools2fast_fastapi-0.4.0}/PKG-INFO +4 -5
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/pyproject.toml +3 -4
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/__init__.py +9 -1
- tools2fast_fastapi-0.4.0/src/tools2fast_fastapi/__version__.py +1 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/models/mixins.py +59 -5
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/schemas/mixins.py +24 -3
- tools2fast_fastapi-0.4.0/src/tools2fast_fastapi/services/soft_delete.py +148 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0/src/tools2fast_fastapi.egg-info}/PKG-INFO +4 -5
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi.egg-info/SOURCES.txt +3 -1
- tools2fast_fastapi-0.4.0/tests/test_uuid_soft_delete_mixins.py +226 -0
- tools2fast_fastapi-0.3.14/src/tools2fast_fastapi/__version__.py +0 -1
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/LICENSE +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/MANIFEST.in +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/README.md +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/docs/guide.md +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/examples/example.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/setup.cfg +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/models/__init__.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/schemas/__init__.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/schemas/response_schema.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/services/__init__.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/services/document_simple_revision_service.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/services/document_with_items_revision_service.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/services/number_service.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/services/query_filters.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/services/revision_service.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/services/transaction_service.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/skills/SKILL.md +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/utils/__init__.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/utils/routing.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi.egg-info/dependency_links.txt +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi.egg-info/requires.txt +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi.egg-info/top_level.txt +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/tests/test_auth_error.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/tests/test_initial.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/tests/test_query_filters.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/tests/test_response_schema.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/tests/test_revision_service.py +0 -0
- {tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/tests/test_schemas.py +0 -0
{tools2fast_fastapi-0.3.14/src/tools2fast_fastapi.egg-info → tools2fast_fastapi-0.4.0}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tools2fast-fastapi
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Shared tools, mixins, and base services for FastAPI applications - Solautyc Team internal use
|
|
5
5
|
Author-email: Angel Daniel Sanchez Castillo <angeldaniel.sanchezcastillo@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -34,12 +34,11 @@ Classifier: Development Status :: 3 - Alpha
|
|
|
34
34
|
Classifier: Intended Audience :: Developers
|
|
35
35
|
Classifier: License :: OSI Approved :: MIT License
|
|
36
36
|
Classifier: Programming Language :: Python :: 3
|
|
37
|
-
Classifier: Programming Language :: Python :: 3.
|
|
38
|
-
Classifier: Programming Language :: Python :: 3.
|
|
39
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
40
39
|
Classifier: Framework :: FastAPI
|
|
41
40
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
42
|
-
Requires-Python: >=3.
|
|
41
|
+
Requires-Python: >=3.14
|
|
43
42
|
Description-Content-Type: text/markdown
|
|
44
43
|
License-File: LICENSE
|
|
45
44
|
Requires-Dist: fastapi>=0.100.0
|
|
@@ -7,7 +7,7 @@ name = "tools2fast-fastapi"
|
|
|
7
7
|
dynamic = ["version"]
|
|
8
8
|
description = "Shared tools, mixins, and base services for FastAPI applications - Solautyc Team internal use"
|
|
9
9
|
readme = "README.md"
|
|
10
|
-
requires-python = ">=3.
|
|
10
|
+
requires-python = ">=3.14"
|
|
11
11
|
license = {file = "LICENSE"}
|
|
12
12
|
authors = [
|
|
13
13
|
{name = "Angel Daniel Sanchez Castillo", email = "angeldaniel.sanchezcastillo@gmail.com"}
|
|
@@ -18,9 +18,8 @@ classifiers = [
|
|
|
18
18
|
"Intended Audience :: Developers",
|
|
19
19
|
"License :: OSI Approved :: MIT License",
|
|
20
20
|
"Programming Language :: Python :: 3",
|
|
21
|
-
"Programming Language :: Python :: 3.
|
|
22
|
-
"Programming Language :: Python :: 3.
|
|
23
|
-
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Programming Language :: Python :: 3.15",
|
|
24
23
|
"Framework :: FastAPI",
|
|
25
24
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
26
25
|
]
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
from .models.mixins import AuditMixin, AuditTimestampMixin, IdMixin, TimestampMixin, NumberUniqueMixin, RevisionMixin, NumberMixin
|
|
1
|
+
from .models.mixins import AuditMixin, AuditTimestampMixin, IdMixin, UUIDIdMixin, TimestampMixin, NumberUniqueMixin, RevisionMixin, NumberMixin, SoftDeleteMixin
|
|
2
2
|
from .services import DocumentSimpleRevisionService, DocumentWithItemsRevisionService, TransactionService
|
|
3
|
+
from .services.soft_delete import soft_delete, restore, active_only, deleted_only
|
|
3
4
|
from .schemas import (
|
|
4
5
|
APIResponse,
|
|
5
6
|
AuthError,
|
|
@@ -22,11 +23,18 @@ __all__ = [
|
|
|
22
23
|
# Model mixins
|
|
23
24
|
"AuditTimestampMixin",
|
|
24
25
|
"IdMixin",
|
|
26
|
+
"UUIDIdMixin",
|
|
25
27
|
"TimestampMixin",
|
|
26
28
|
"AuditMixin",
|
|
27
29
|
"NumberUniqueMixin",
|
|
28
30
|
"RevisionMixin",
|
|
29
31
|
"NumberMixin",
|
|
32
|
+
"SoftDeleteMixin",
|
|
33
|
+
# Soft-delete functions
|
|
34
|
+
"soft_delete",
|
|
35
|
+
"restore",
|
|
36
|
+
"active_only",
|
|
37
|
+
"deleted_only",
|
|
30
38
|
# Response models
|
|
31
39
|
"APIResponse",
|
|
32
40
|
"AuthError",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.4.0"
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/models/mixins.py
RENAMED
|
@@ -1,15 +1,40 @@
|
|
|
1
1
|
from datetime import datetime, timezone
|
|
2
|
+
from uuid import UUID, uuid7
|
|
2
3
|
|
|
3
4
|
from sqlalchemy import DateTime, func, event
|
|
4
5
|
from sqlmodel import BigInteger, Field
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
class IdMixin:
|
|
8
|
-
"""
|
|
9
|
+
"""
|
|
10
|
+
DEPRECATED: Use UUIDIdMixin instead.
|
|
11
|
+
|
|
12
|
+
Mixin para proveer clave primaria autoincremental tipo BigInteger.
|
|
13
|
+
|
|
14
|
+
.. deprecated:: 0.4.0
|
|
15
|
+
Use :class:`UUIDIdMixin` for new models. IdMixin will be removed in a future version.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def __init_subclass__(cls, **kwargs):
|
|
19
|
+
"""Emit deprecation warning when subclassed."""
|
|
20
|
+
import warnings
|
|
21
|
+
warnings.warn(
|
|
22
|
+
"IdMixin is deprecated and will be removed in a future version. "
|
|
23
|
+
"Use UUIDIdMixin instead for UUID v7 primary keys.",
|
|
24
|
+
DeprecationWarning,
|
|
25
|
+
stacklevel=2,
|
|
26
|
+
)
|
|
27
|
+
super().__init_subclass__(**kwargs)
|
|
9
28
|
|
|
10
29
|
id: int | None = Field(default=None, primary_key=True, index=True, sa_type=BigInteger)
|
|
11
30
|
|
|
12
31
|
|
|
32
|
+
class UUIDIdMixin:
|
|
33
|
+
"""Mixin para proveer clave primaria tipo UUID v7 (time-ordered)."""
|
|
34
|
+
|
|
35
|
+
id: UUID = Field(default_factory=uuid7, primary_key=True, index=True)
|
|
36
|
+
|
|
37
|
+
|
|
13
38
|
class TimestampMixin:
|
|
14
39
|
"""Mixin reutilizable para marcas de tiempo en UTC."""
|
|
15
40
|
|
|
@@ -29,19 +54,48 @@ class TimestampMixin:
|
|
|
29
54
|
class AuditMixin:
|
|
30
55
|
"""Mixin para rastrear el usuario que crea y actualiza un registro."""
|
|
31
56
|
|
|
32
|
-
created_by:
|
|
57
|
+
created_by: UUID | None = Field(
|
|
33
58
|
default=None,
|
|
34
59
|
index=True,
|
|
35
60
|
description="ID of the user who created this record.",
|
|
36
|
-
sa_type=BigInteger,
|
|
37
61
|
)
|
|
38
|
-
updated_by:
|
|
62
|
+
updated_by: UUID | None = Field(
|
|
39
63
|
default=None,
|
|
40
64
|
index=True,
|
|
41
65
|
description="ID of the user who last updated this record.",
|
|
42
|
-
sa_type=BigInteger,
|
|
43
66
|
)
|
|
44
67
|
|
|
68
|
+
|
|
69
|
+
class SoftDeleteMixin:
|
|
70
|
+
"""
|
|
71
|
+
Mixin para borrado lógico (soft delete).
|
|
72
|
+
|
|
73
|
+
Agrega:
|
|
74
|
+
- deleted_at (datetime | None): Timestamp de cuándo se borró el registro.
|
|
75
|
+
- deleted_by (UUID | None): ID del usuario que borró el registro.
|
|
76
|
+
|
|
77
|
+
Uso:
|
|
78
|
+
class MyModel(UUIDIdMixin, TimestampMixin, SoftDeleteMixin, SQLModel, table=True):
|
|
79
|
+
__tablename__ = "my_models"
|
|
80
|
+
name: str = Field()
|
|
81
|
+
|
|
82
|
+
El registro se considera "borrado" cuando deleted_at no es None.
|
|
83
|
+
Para filtrar registros activos, usar active_only(query).
|
|
84
|
+
Para filtrar registros borrados, usar deleted_only(query).
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
deleted_at: datetime | None = Field(
|
|
88
|
+
default=None,
|
|
89
|
+
index=True,
|
|
90
|
+
description="Timestamp when the record was soft-deleted. None means active.",
|
|
91
|
+
sa_type=DateTime(timezone=True),
|
|
92
|
+
)
|
|
93
|
+
deleted_by: UUID | None = Field(
|
|
94
|
+
default=None,
|
|
95
|
+
description="ID of the user who soft-deleted this record.",
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
45
99
|
class NumberMixin:
|
|
46
100
|
"""
|
|
47
101
|
Mixin para proveer un campo `number` que actúa como un identificador continuo
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/schemas/mixins.py
RENAMED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
2
|
from typing import Literal
|
|
3
|
+
from uuid import UUID
|
|
3
4
|
|
|
4
5
|
from pydantic import BaseModel
|
|
5
6
|
from sqlmodel import SQLModel
|
|
@@ -98,11 +99,24 @@ class PaymentRequiredMixin(BaseModel):
|
|
|
98
99
|
|
|
99
100
|
|
|
100
101
|
class IdMixin(SQLModel):
|
|
101
|
-
"""
|
|
102
|
+
"""
|
|
103
|
+
DEPRECATED: Use UUIDIdMixin instead.
|
|
104
|
+
|
|
105
|
+
Schema mixin para recursos que tienen un ID entero.
|
|
106
|
+
|
|
107
|
+
.. deprecated:: 0.4.0
|
|
108
|
+
Use :class:`UUIDIdMixin` for new schemas. IdMixin will be removed in a future version.
|
|
109
|
+
"""
|
|
102
110
|
|
|
103
111
|
id: int
|
|
104
112
|
|
|
105
113
|
|
|
114
|
+
class UUIDIdMixin(SQLModel):
|
|
115
|
+
"""Schema mixin para recursos que tienen un ID UUID."""
|
|
116
|
+
|
|
117
|
+
id: UUID
|
|
118
|
+
|
|
119
|
+
|
|
106
120
|
class TimestampMixin(SQLModel):
|
|
107
121
|
"""Schema mixin para recursos que incluyen timestamps de creación y actualización."""
|
|
108
122
|
|
|
@@ -113,8 +127,15 @@ class TimestampMixin(SQLModel):
|
|
|
113
127
|
class AuditMixin(SQLModel):
|
|
114
128
|
"""Schema mixin para recursos que incluyen IDs de usuario auditables."""
|
|
115
129
|
|
|
116
|
-
created_by:
|
|
117
|
-
updated_by:
|
|
130
|
+
created_by: UUID | None = None
|
|
131
|
+
updated_by: UUID | None = None
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class SoftDeleteMixin(SQLModel):
|
|
135
|
+
"""Schema mixin para recursos que incluyen campos de borrado lógico."""
|
|
136
|
+
|
|
137
|
+
deleted_at: datetime | None = None
|
|
138
|
+
deleted_by: UUID | None = None
|
|
118
139
|
|
|
119
140
|
|
|
120
141
|
class NumberMixin(SQLModel):
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""Soft-delete functions for logical deletion and restoration of records."""
|
|
2
|
+
from datetime import datetime, timezone
|
|
3
|
+
from typing import TypeVar
|
|
4
|
+
from uuid import UUID
|
|
5
|
+
|
|
6
|
+
from sqlalchemy import Select
|
|
7
|
+
from sqlmodel import SQLModel
|
|
8
|
+
from sqlmodel.ext.asyncio.session import AsyncSession
|
|
9
|
+
|
|
10
|
+
# Type variable for SQLModel classes
|
|
11
|
+
SQLModelType = TypeVar("SQLModelType", bound=SQLModel)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def soft_delete(
|
|
15
|
+
session: AsyncSession,
|
|
16
|
+
model_cls: type[SQLModelType],
|
|
17
|
+
id: UUID,
|
|
18
|
+
deleted_by: UUID,
|
|
19
|
+
) -> None:
|
|
20
|
+
"""
|
|
21
|
+
Mark a record as soft-deleted.
|
|
22
|
+
|
|
23
|
+
Sets deleted_at to current timestamp and deleted_by to the provided user ID.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
session: Async database session
|
|
27
|
+
model_cls: SQLModel class to delete
|
|
28
|
+
id: UUID of the record to delete
|
|
29
|
+
deleted_by: UUID of the user performing the deletion
|
|
30
|
+
|
|
31
|
+
Raises:
|
|
32
|
+
ValueError: If the record does not exist or is already deleted
|
|
33
|
+
"""
|
|
34
|
+
# Fetch the record
|
|
35
|
+
record = await session.get(model_cls, id)
|
|
36
|
+
if record is None:
|
|
37
|
+
raise ValueError(f"{model_cls.__name__} with id {id} not found")
|
|
38
|
+
|
|
39
|
+
# Check if already deleted
|
|
40
|
+
if hasattr(record, "deleted_at") and record.deleted_at is not None:
|
|
41
|
+
raise ValueError(f"{model_cls.__name__} with id {id} is already deleted")
|
|
42
|
+
|
|
43
|
+
# Mark as deleted
|
|
44
|
+
record.deleted_at = datetime.now(timezone.utc)
|
|
45
|
+
record.deleted_by = deleted_by
|
|
46
|
+
|
|
47
|
+
session.add(record)
|
|
48
|
+
await session.commit()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
async def restore(
|
|
52
|
+
session: AsyncSession,
|
|
53
|
+
model_cls: type[SQLModelType],
|
|
54
|
+
id: UUID,
|
|
55
|
+
restored_by: UUID,
|
|
56
|
+
) -> None:
|
|
57
|
+
"""
|
|
58
|
+
Restore a soft-deleted record.
|
|
59
|
+
|
|
60
|
+
Sets deleted_at and deleted_by to None, and updates updated_by to the restorer.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
session: Async database session
|
|
64
|
+
model_cls: SQLModel class to restore
|
|
65
|
+
id: UUID of the record to restore
|
|
66
|
+
restored_by: UUID of the user performing the restoration
|
|
67
|
+
|
|
68
|
+
Raises:
|
|
69
|
+
ValueError: If the record does not exist or is not deleted
|
|
70
|
+
"""
|
|
71
|
+
# Fetch the record
|
|
72
|
+
record = await session.get(model_cls, id)
|
|
73
|
+
if record is None:
|
|
74
|
+
raise ValueError(f"{model_cls.__name__} with id {id} not found")
|
|
75
|
+
|
|
76
|
+
# Check if actually deleted
|
|
77
|
+
if not hasattr(record, "deleted_at") or record.deleted_at is None:
|
|
78
|
+
raise ValueError(f"{model_cls.__name__} with id {id} is not deleted")
|
|
79
|
+
|
|
80
|
+
# Restore the record
|
|
81
|
+
record.deleted_at = None
|
|
82
|
+
record.deleted_by = None
|
|
83
|
+
|
|
84
|
+
# Update updated_by to track who restored it
|
|
85
|
+
if hasattr(record, "updated_by"):
|
|
86
|
+
record.updated_by = restored_by
|
|
87
|
+
|
|
88
|
+
session.add(record)
|
|
89
|
+
await session.commit()
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def active_only(query: Select) -> Select:
|
|
93
|
+
"""
|
|
94
|
+
Filter a query to only return active (non-deleted) records.
|
|
95
|
+
|
|
96
|
+
Adds WHERE deleted_at IS NULL to the query.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
query: SQLAlchemy Select query
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
Modified query with active filter applied
|
|
103
|
+
|
|
104
|
+
Example:
|
|
105
|
+
stmt = active_only(select(User))
|
|
106
|
+
result = await session.exec(stmt)
|
|
107
|
+
"""
|
|
108
|
+
# Get the model class from the query
|
|
109
|
+
# The query should be like select(Model) or select(Model.id, ...)
|
|
110
|
+
if not query.column_descriptions:
|
|
111
|
+
raise ValueError("Query must have column descriptions")
|
|
112
|
+
|
|
113
|
+
# Get the first entity (model class)
|
|
114
|
+
first_entity = query.column_descriptions[0].get('entity')
|
|
115
|
+
if first_entity is None:
|
|
116
|
+
raise ValueError("Query must select from a model class")
|
|
117
|
+
|
|
118
|
+
# Add the filter for deleted_at IS NULL
|
|
119
|
+
return query.where(first_entity.deleted_at.is_(None))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def deleted_only(query: Select) -> Select:
|
|
123
|
+
"""
|
|
124
|
+
Filter a query to only return deleted records.
|
|
125
|
+
|
|
126
|
+
Adds WHERE deleted_at IS NOT NULL to the query.
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
query: SQLAlchemy Select query
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
Modified query with deleted filter applied
|
|
133
|
+
|
|
134
|
+
Example:
|
|
135
|
+
stmt = deleted_only(select(User))
|
|
136
|
+
result = await session.exec(stmt)
|
|
137
|
+
"""
|
|
138
|
+
# Get the model class from the query
|
|
139
|
+
if not query.column_descriptions:
|
|
140
|
+
raise ValueError("Query must have column descriptions")
|
|
141
|
+
|
|
142
|
+
# Get the first entity (model class)
|
|
143
|
+
first_entity = query.column_descriptions[0].get('entity')
|
|
144
|
+
if first_entity is None:
|
|
145
|
+
raise ValueError("Query must select from a model class")
|
|
146
|
+
|
|
147
|
+
# Add the filter for deleted_at IS NOT NULL
|
|
148
|
+
return query.where(first_entity.deleted_at.isnot(None))
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0/src/tools2fast_fastapi.egg-info}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tools2fast-fastapi
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Shared tools, mixins, and base services for FastAPI applications - Solautyc Team internal use
|
|
5
5
|
Author-email: Angel Daniel Sanchez Castillo <angeldaniel.sanchezcastillo@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -34,12 +34,11 @@ Classifier: Development Status :: 3 - Alpha
|
|
|
34
34
|
Classifier: Intended Audience :: Developers
|
|
35
35
|
Classifier: License :: OSI Approved :: MIT License
|
|
36
36
|
Classifier: Programming Language :: Python :: 3
|
|
37
|
-
Classifier: Programming Language :: Python :: 3.
|
|
38
|
-
Classifier: Programming Language :: Python :: 3.
|
|
39
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
40
39
|
Classifier: Framework :: FastAPI
|
|
41
40
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
42
|
-
Requires-Python: >=3.
|
|
41
|
+
Requires-Python: >=3.14
|
|
43
42
|
Description-Content-Type: text/markdown
|
|
44
43
|
License-File: LICENSE
|
|
45
44
|
Requires-Dist: fastapi>=0.100.0
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi.egg-info/SOURCES.txt
RENAMED
|
@@ -22,6 +22,7 @@ src/tools2fast_fastapi/services/document_with_items_revision_service.py
|
|
|
22
22
|
src/tools2fast_fastapi/services/number_service.py
|
|
23
23
|
src/tools2fast_fastapi/services/query_filters.py
|
|
24
24
|
src/tools2fast_fastapi/services/revision_service.py
|
|
25
|
+
src/tools2fast_fastapi/services/soft_delete.py
|
|
25
26
|
src/tools2fast_fastapi/services/transaction_service.py
|
|
26
27
|
src/tools2fast_fastapi/skills/SKILL.md
|
|
27
28
|
src/tools2fast_fastapi/utils/__init__.py
|
|
@@ -31,4 +32,5 @@ tests/test_initial.py
|
|
|
31
32
|
tests/test_query_filters.py
|
|
32
33
|
tests/test_response_schema.py
|
|
33
34
|
tests/test_revision_service.py
|
|
34
|
-
tests/test_schemas.py
|
|
35
|
+
tests/test_schemas.py
|
|
36
|
+
tests/test_uuid_soft_delete_mixins.py
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"""Tests for UUID and soft-delete mixins (ODD-1)."""
|
|
2
|
+
import warnings
|
|
3
|
+
from datetime import datetime, timezone
|
|
4
|
+
from uuid import UUID
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
from sqlmodel import Field, SQLModel, select
|
|
8
|
+
|
|
9
|
+
from tools2fast_fastapi import (
|
|
10
|
+
UUIDIdMixin,
|
|
11
|
+
SoftDeleteMixin,
|
|
12
|
+
AuditMixin,
|
|
13
|
+
TimestampMixin,
|
|
14
|
+
soft_delete,
|
|
15
|
+
restore,
|
|
16
|
+
active_only,
|
|
17
|
+
deleted_only,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
22
|
+
# TEST MODELS
|
|
23
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class TestModel(UUIDIdMixin, TimestampMixin, SQLModel, table=True):
|
|
27
|
+
"""Test model with UUID primary key."""
|
|
28
|
+
__tablename__ = "test_models"
|
|
29
|
+
name: str = Field()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class TestSoftDeleteModel(UUIDIdMixin, TimestampMixin, SoftDeleteMixin, SQLModel, table=True):
|
|
33
|
+
"""Test model with UUID primary key and soft-delete."""
|
|
34
|
+
__tablename__ = "test_soft_delete_models"
|
|
35
|
+
name: str = Field()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class TestSoftDeleteWithAuditModel(UUIDIdMixin, TimestampMixin, AuditMixin, SoftDeleteMixin, SQLModel, table=True):
|
|
39
|
+
"""Test model with UUID primary key, audit, and soft-delete."""
|
|
40
|
+
__tablename__ = "test_soft_delete_with_audit_models"
|
|
41
|
+
name: str = Field()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class TestAuditModel(UUIDIdMixin, TimestampMixin, AuditMixin, SQLModel, table=True):
|
|
45
|
+
"""Test model with UUID primary key and audit fields."""
|
|
46
|
+
__tablename__ = "test_audit_models"
|
|
47
|
+
name: str = Field()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
51
|
+
# UUIDIdMixin TESTS
|
|
52
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_uuid_id_mixin_generates_uuid7():
|
|
56
|
+
"""UUIDIdMixin should generate a UUID v7 by default."""
|
|
57
|
+
model = TestModel(name="Test")
|
|
58
|
+
assert model.id is not None
|
|
59
|
+
assert isinstance(model.id, UUID)
|
|
60
|
+
# UUID v7 should be version 7
|
|
61
|
+
assert model.id.version == 7
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_uuid_id_mixin_accepts_custom_uuid():
|
|
65
|
+
"""UUIDIdMixin should accept a custom UUID."""
|
|
66
|
+
custom_uuid = UUID("0190a7e3-8c4a-7d0b-9c8e-3f4a5b6c7d8e")
|
|
67
|
+
model = TestModel(id=custom_uuid, name="Test")
|
|
68
|
+
assert model.id == custom_uuid
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_uuid_id_mixin_primary_key():
|
|
72
|
+
"""UUIDIdMixin should define id as primary key."""
|
|
73
|
+
# Check that the field is marked as primary key
|
|
74
|
+
field_info = TestModel.model_fields["id"]
|
|
75
|
+
assert field_info.primary_key is True
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
79
|
+
# SoftDeleteMixin TESTS
|
|
80
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_soft_delete_mixin_defaults_to_none():
|
|
84
|
+
"""SoftDeleteMixin should default deleted_at and deleted_by to None."""
|
|
85
|
+
model = TestSoftDeleteModel(name="Test")
|
|
86
|
+
assert model.deleted_at is None
|
|
87
|
+
assert model.deleted_by is None
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_soft_delete_mixin_accepts_datetime():
|
|
91
|
+
"""SoftDeleteMixin should accept datetime for deleted_at."""
|
|
92
|
+
now = datetime.now(timezone.utc)
|
|
93
|
+
deleted_by = UUID("0190a7e3-8c4a-7d0b-9c8e-3f4a5b6c7d8e")
|
|
94
|
+
model = TestSoftDeleteModel(name="Test", deleted_at=now, deleted_by=deleted_by)
|
|
95
|
+
assert model.deleted_at == now
|
|
96
|
+
assert model.deleted_by == deleted_by
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
100
|
+
# AuditMixin TESTS (UUID version)
|
|
101
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def test_audit_mixin_uses_uuid():
|
|
105
|
+
"""AuditMixin should use UUID for created_by and updated_by."""
|
|
106
|
+
user_uuid = UUID("0190a7e3-8c4a-7d0b-9c8e-3f4a5b6c7d8e")
|
|
107
|
+
model = TestAuditModel(name="Test", created_by=user_uuid, updated_by=user_uuid)
|
|
108
|
+
assert isinstance(model.created_by, UUID)
|
|
109
|
+
assert isinstance(model.updated_by, UUID)
|
|
110
|
+
assert model.created_by == user_uuid
|
|
111
|
+
assert model.updated_by == user_uuid
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_audit_mixin_defaults_to_none():
|
|
115
|
+
"""AuditMixin should default created_by and updated_by to None."""
|
|
116
|
+
model = TestAuditModel(name="Test")
|
|
117
|
+
assert model.created_by is None
|
|
118
|
+
assert model.updated_by is None
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
122
|
+
# SOFT DELETE FUNCTIONS TESTS
|
|
123
|
+
# ═══════════════════════════════════════════════════════════════════════════════
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@pytest.mark.asyncio
|
|
127
|
+
async def test_soft_delete_function(session):
|
|
128
|
+
"""soft_delete() should mark a record as deleted."""
|
|
129
|
+
# Create a test record
|
|
130
|
+
model = TestSoftDeleteModel(name="Test")
|
|
131
|
+
session.add(model)
|
|
132
|
+
await session.commit()
|
|
133
|
+
await session.refresh(model)
|
|
134
|
+
|
|
135
|
+
# Verify not deleted initially
|
|
136
|
+
assert model.deleted_at is None
|
|
137
|
+
assert model.deleted_by is None
|
|
138
|
+
|
|
139
|
+
# Soft delete the record
|
|
140
|
+
deleter_uuid = UUID("0190a7e3-8c4a-7d0b-9c8e-3f4a5b6c7d8e")
|
|
141
|
+
await soft_delete(session, TestSoftDeleteModel, model.id, deleter_uuid)
|
|
142
|
+
|
|
143
|
+
# Verify deleted
|
|
144
|
+
await session.refresh(model)
|
|
145
|
+
assert model.deleted_at is not None
|
|
146
|
+
assert model.deleted_by == deleter_uuid
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@pytest.mark.asyncio
|
|
150
|
+
async def test_restore_function(session):
|
|
151
|
+
"""restore() should reactivate a deleted record."""
|
|
152
|
+
# Create and soft delete a test record
|
|
153
|
+
model = TestSoftDeleteWithAuditModel(name="Test")
|
|
154
|
+
session.add(model)
|
|
155
|
+
await session.commit()
|
|
156
|
+
await session.refresh(model)
|
|
157
|
+
|
|
158
|
+
deleter_uuid = UUID("0190a7e3-8c4a-7d0b-9c8e-3f4a5b6c7d8e")
|
|
159
|
+
await soft_delete(session, TestSoftDeleteWithAuditModel, model.id, deleter_uuid)
|
|
160
|
+
await session.refresh(model)
|
|
161
|
+
|
|
162
|
+
# Verify deleted
|
|
163
|
+
assert model.deleted_at is not None
|
|
164
|
+
|
|
165
|
+
# Restore the record
|
|
166
|
+
restorer_uuid = UUID("0190a7e3-8c4a-7d0b-9c8e-3f4a5b6c7d8f")
|
|
167
|
+
await restore(session, TestSoftDeleteWithAuditModel, model.id, restorer_uuid)
|
|
168
|
+
|
|
169
|
+
# Verify restored
|
|
170
|
+
await session.refresh(model)
|
|
171
|
+
assert model.deleted_at is None
|
|
172
|
+
assert model.deleted_by is None
|
|
173
|
+
# updated_by should be the restorer
|
|
174
|
+
assert model.updated_by == restorer_uuid
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
@pytest.mark.asyncio
|
|
178
|
+
async def test_active_only_filter(session):
|
|
179
|
+
"""active_only() should filter to only non-deleted records."""
|
|
180
|
+
# Create two records
|
|
181
|
+
active_model = TestSoftDeleteModel(name="Active")
|
|
182
|
+
deleted_model = TestSoftDeleteModel(name="Deleted")
|
|
183
|
+
session.add(active_model)
|
|
184
|
+
session.add(deleted_model)
|
|
185
|
+
await session.commit()
|
|
186
|
+
await session.refresh(active_model)
|
|
187
|
+
await session.refresh(deleted_model)
|
|
188
|
+
|
|
189
|
+
# Soft delete one
|
|
190
|
+
deleter_uuid = UUID("0190a7e3-8c4a-7d0b-9c8e-3f4a5b6c7d8e")
|
|
191
|
+
await soft_delete(session, TestSoftDeleteModel, deleted_model.id, deleter_uuid)
|
|
192
|
+
|
|
193
|
+
# Query with active_only filter
|
|
194
|
+
stmt = active_only(select(TestSoftDeleteModel))
|
|
195
|
+
result = await session.exec(stmt)
|
|
196
|
+
records = result.all()
|
|
197
|
+
|
|
198
|
+
# Should only return the active record
|
|
199
|
+
assert len(records) == 1
|
|
200
|
+
assert records[0].name == "Active"
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@pytest.mark.asyncio
|
|
204
|
+
async def test_deleted_only_filter(session):
|
|
205
|
+
"""deleted_only() should filter to only deleted records."""
|
|
206
|
+
# Create two records
|
|
207
|
+
active_model = TestSoftDeleteModel(name="Active")
|
|
208
|
+
deleted_model = TestSoftDeleteModel(name="Deleted")
|
|
209
|
+
session.add(active_model)
|
|
210
|
+
session.add(deleted_model)
|
|
211
|
+
await session.commit()
|
|
212
|
+
await session.refresh(active_model)
|
|
213
|
+
await session.refresh(deleted_model)
|
|
214
|
+
|
|
215
|
+
# Soft delete one
|
|
216
|
+
deleter_uuid = UUID("0190a7e3-8c4a-7d0b-9c8e-3f4a5b6c7d8e")
|
|
217
|
+
await soft_delete(session, TestSoftDeleteModel, deleted_model.id, deleter_uuid)
|
|
218
|
+
|
|
219
|
+
# Query with deleted_only filter
|
|
220
|
+
stmt = deleted_only(select(TestSoftDeleteModel))
|
|
221
|
+
result = await session.exec(stmt)
|
|
222
|
+
records = result.all()
|
|
223
|
+
|
|
224
|
+
# Should only return the deleted record
|
|
225
|
+
assert len(records) == 1
|
|
226
|
+
assert records[0].name == "Deleted"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.3.14"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/models/__init__.py
RENAMED
|
File without changes
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/schemas/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/services/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/skills/SKILL.md
RENAMED
|
File without changes
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/utils/__init__.py
RENAMED
|
File without changes
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi/utils/routing.py
RENAMED
|
File without changes
|
|
File without changes
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi.egg-info/requires.txt
RENAMED
|
File without changes
|
{tools2fast_fastapi-0.3.14 → tools2fast_fastapi-0.4.0}/src/tools2fast_fastapi.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|