lenapi 0.1.4.1__tar.gz → 0.1.4.2__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.
Files changed (52) hide show
  1. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/PKG-INFO +26 -1
  2. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/README.md +25 -0
  3. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/__init__.py +1 -1
  4. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/models.py +50 -1
  5. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi.egg-info/PKG-INFO +26 -1
  6. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/pyproject.toml +1 -1
  7. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_framework_features.py +65 -0
  8. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/LICENSE +0 -0
  9. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/admin.py +0 -0
  10. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/application.py +0 -0
  11. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/auth.py +0 -0
  12. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/authentication.py +0 -0
  13. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/authorization.py +0 -0
  14. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/background.py +0 -0
  15. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/cli.py +0 -0
  16. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/concurrency.py +0 -0
  17. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/database.py +0 -0
  18. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/dependencies.py +0 -0
  19. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/docs_guard.py +0 -0
  20. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/logging.py +0 -0
  21. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/middleware.py +0 -0
  22. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/model.py +0 -0
  23. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/mongodb.py +0 -0
  24. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/openapi.py +0 -0
  25. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/parameters.py +0 -0
  26. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/project.py +0 -0
  27. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/request.py +0 -0
  28. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/response.py +0 -0
  29. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/security.py +0 -0
  30. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/security_store.py +0 -0
  31. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/sentinels.py +0 -0
  32. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/service.py +0 -0
  33. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/throttling.py +0 -0
  34. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi/transaction.py +0 -0
  35. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi.egg-info/SOURCES.txt +0 -0
  36. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi.egg-info/dependency_links.txt +0 -0
  37. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi.egg-info/entry_points.txt +0 -0
  38. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi.egg-info/requires.txt +0 -0
  39. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi.egg-info/top_level.txt +0 -0
  40. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/lenapi_bootstrap.py +0 -0
  41. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/setup.cfg +0 -0
  42. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_admin_console.py +0 -0
  43. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_cli_settings_recovery.py +0 -0
  44. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_docs_guard.py +0 -0
  45. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_media_upload.py +0 -0
  46. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_mongodb.py +0 -0
  47. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_platform_services.py +0 -0
  48. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_project_isolation.py +0 -0
  49. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_security_patch.py +0 -0
  50. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_sentinels.py +0 -0
  51. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_throttle.py +0 -0
  52. {lenapi-0.1.4.1 → lenapi-0.1.4.2}/tests/test_user_model_extension.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lenapi
3
- Version: 0.1.4.1
3
+ Version: 0.1.4.2
4
4
  Summary: A modern ASGI framework with Pydantic validation, sync and async database access, WebSockets, authentication, and integrated API documentation.
5
5
  Author: Maekandex Communication Limitation
6
6
  Author-email: "Ifeanyi M. Onovo" <ifeanyi@maekandex.com.ng>
@@ -2295,6 +2295,31 @@ student, created = Student.get_or_create(
2295
2295
 
2296
2296
  `get_or_404`, `update_or_404`, and `delete_or_404` raise `HTTPException(404, ...)` when no record matches, which lenApi returns as an HTTP 404 response. `create_update` creates a missing row or updates an existing one and returns the model instance. `get_or_create` returns `(instance, created)`, where `created` is a boolean.
2297
2297
 
2298
+ Async routes can use the matching `a`-prefixed helpers with `await`:
2299
+
2300
+ ```python
2301
+ student = await Student.aget_or_404(id=student_id)
2302
+
2303
+ student = await Student.acreate_update(
2304
+ defaults={"course": "Python"},
2305
+ person_name="Ifeanyi",
2306
+ )
2307
+
2308
+ student = await Student.aupdate_or_404(
2309
+ defaults={"course": "Java"},
2310
+ person_name="Ifeanyi",
2311
+ )
2312
+
2313
+ await Student.adelete_or_404(person_name="Ifeanyi")
2314
+
2315
+ student, created = await Student.aget_or_create(
2316
+ defaults={"course": "Python"},
2317
+ person_name="Ifeanyi",
2318
+ )
2319
+ ```
2320
+
2321
+ The async helpers are `aget_or_404`, `acreate_update`, `aupdate_or_404`, `adelete_or_404`, and `aget_or_create`. Pass `session=...` when using an explicit `AsyncSession`; otherwise they use the active LenAPI request session. The `*_or_404` methods raise `HTTPException(404, ...)` when no row matches.
2322
+
2298
2323
  For a declared foreign-key column, `.special("field_name")` resolves its related model and exposes it under that same field name:
2299
2324
 
2300
2325
  ```python
@@ -2243,6 +2243,31 @@ student, created = Student.get_or_create(
2243
2243
 
2244
2244
  `get_or_404`, `update_or_404`, and `delete_or_404` raise `HTTPException(404, ...)` when no record matches, which lenApi returns as an HTTP 404 response. `create_update` creates a missing row or updates an existing one and returns the model instance. `get_or_create` returns `(instance, created)`, where `created` is a boolean.
2245
2245
 
2246
+ Async routes can use the matching `a`-prefixed helpers with `await`:
2247
+
2248
+ ```python
2249
+ student = await Student.aget_or_404(id=student_id)
2250
+
2251
+ student = await Student.acreate_update(
2252
+ defaults={"course": "Python"},
2253
+ person_name="Ifeanyi",
2254
+ )
2255
+
2256
+ student = await Student.aupdate_or_404(
2257
+ defaults={"course": "Java"},
2258
+ person_name="Ifeanyi",
2259
+ )
2260
+
2261
+ await Student.adelete_or_404(person_name="Ifeanyi")
2262
+
2263
+ student, created = await Student.aget_or_create(
2264
+ defaults={"course": "Python"},
2265
+ person_name="Ifeanyi",
2266
+ )
2267
+ ```
2268
+
2269
+ The async helpers are `aget_or_404`, `acreate_update`, `aupdate_or_404`, `adelete_or_404`, and `aget_or_create`. Pass `session=...` when using an explicit `AsyncSession`; otherwise they use the active LenAPI request session. The `*_or_404` methods raise `HTTPException(404, ...)` when no row matches.
2270
+
2246
2271
  For a declared foreign-key column, `.special("field_name")` resolves its related model and exposes it under that same field name:
2247
2272
 
2248
2273
  ```python
@@ -8,7 +8,7 @@ if sys.version_info < (3, 11):
8
8
  "on 2026-10-01 and is no longer supported."
9
9
  )
10
10
 
11
- __version__ = "0.1.4.1"
11
+ __version__ = "0.1.4.2"
12
12
 
13
13
  from .application import Application, LENAPI, Router
14
14
  from .background import BackgroundQueue, BackgroundTasks
@@ -879,7 +879,15 @@ class Model(DeclarativeBase):
879
879
  """Return the single matching row, or raise an HTTP 404 exception."""
880
880
  instance = cls.get(**filters).get()
881
881
  if instance is None:
882
- raise HTTPException(404, f"{cls.__name__} not found")
882
+ raise HTTPException(404, "Not found")
883
+ return instance
884
+
885
+ @classmethod
886
+ async def aget_or_404(cls, *, session=None, **filters):
887
+ """Async counterpart to get_or_404()."""
888
+ instance = await cls.aget(session=session, **filters)
889
+ if instance is None:
890
+ raise HTTPException(404, "Not found")
883
891
  return instance
884
892
 
885
893
  @classmethod
@@ -894,6 +902,18 @@ class Model(DeclarativeBase):
894
902
  setattr(instance, field, value)
895
903
  return instance.save()
896
904
 
905
+ @classmethod
906
+ async def acreate_update(cls, defaults: dict | None = None, *, session=None, **lookup):
907
+ """Async counterpart to create_update()."""
908
+ if not lookup:
909
+ raise ValueError("acreate_update requires at least one lookup field")
910
+ instance = await cls.aget(session=session, **lookup)
911
+ if instance is None:
912
+ return await cls.acreate(session=session, **(lookup | (defaults or {})))
913
+ for field, value in (defaults or {}).items():
914
+ setattr(instance, field, value)
915
+ return await instance.asave(session=session)
916
+
897
917
  @classmethod
898
918
  def update_or_404(cls, defaults: dict | None = None, **lookup):
899
919
  """Update a matching row, or raise an HTTP 404 exception."""
@@ -904,6 +924,16 @@ class Model(DeclarativeBase):
904
924
  setattr(instance, field, value)
905
925
  return instance.save()
906
926
 
927
+ @classmethod
928
+ async def aupdate_or_404(cls, defaults: dict | None = None, *, session=None, **lookup):
929
+ """Async counterpart to update_or_404()."""
930
+ if not lookup:
931
+ raise ValueError("aupdate_or_404 requires at least one lookup field")
932
+ instance = await cls.aget_or_404(session=session, **lookup)
933
+ for field, value in (defaults or {}).items():
934
+ setattr(instance, field, value)
935
+ return await instance.asave(session=session)
936
+
907
937
 
908
938
  @classmethod
909
939
  def delete_or_404(cls, **filters) -> bool:
@@ -914,6 +944,15 @@ class Model(DeclarativeBase):
914
944
  instance.delete()
915
945
  return True
916
946
 
947
+ @classmethod
948
+ async def adelete_or_404(cls, *, session=None, **filters) -> bool:
949
+ """Async counterpart to delete_or_404()."""
950
+ if not filters:
951
+ raise ValueError("adelete_or_404 requires at least one lookup field")
952
+ instance = await cls.aget_or_404(session=session, **filters)
953
+ await instance.adelete(session=session)
954
+ return True
955
+
917
956
  @classmethod
918
957
  def get_or_create(cls, defaults: dict | None = None, **lookup):
919
958
  """Return (instance, created), following Django's get_or_create convention."""
@@ -924,6 +963,16 @@ class Model(DeclarativeBase):
924
963
  return instance, False
925
964
  return cls.create(**(lookup | (defaults or {}))), True
926
965
 
966
+ @classmethod
967
+ async def aget_or_create(cls, defaults: dict | None = None, *, session=None, **lookup):
968
+ """Async counterpart to get_or_create(); returns (instance, created)."""
969
+ if not lookup:
970
+ raise ValueError("aget_or_create requires at least one lookup field")
971
+ instance = await cls.aget(session=session, **lookup)
972
+ if instance is not None:
973
+ return instance, False
974
+ return await cls.acreate(session=session, **(lookup | (defaults or {}))), True
975
+
927
976
  @classmethod
928
977
  def all(cls, *, limit=None, offset=0):
929
978
  """Build a full-table query; calling delete() on it drops the table."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lenapi
3
- Version: 0.1.4.1
3
+ Version: 0.1.4.2
4
4
  Summary: A modern ASGI framework with Pydantic validation, sync and async database access, WebSockets, authentication, and integrated API documentation.
5
5
  Author: Maekandex Communication Limitation
6
6
  Author-email: "Ifeanyi M. Onovo" <ifeanyi@maekandex.com.ng>
@@ -2295,6 +2295,31 @@ student, created = Student.get_or_create(
2295
2295
 
2296
2296
  `get_or_404`, `update_or_404`, and `delete_or_404` raise `HTTPException(404, ...)` when no record matches, which lenApi returns as an HTTP 404 response. `create_update` creates a missing row or updates an existing one and returns the model instance. `get_or_create` returns `(instance, created)`, where `created` is a boolean.
2297
2297
 
2298
+ Async routes can use the matching `a`-prefixed helpers with `await`:
2299
+
2300
+ ```python
2301
+ student = await Student.aget_or_404(id=student_id)
2302
+
2303
+ student = await Student.acreate_update(
2304
+ defaults={"course": "Python"},
2305
+ person_name="Ifeanyi",
2306
+ )
2307
+
2308
+ student = await Student.aupdate_or_404(
2309
+ defaults={"course": "Java"},
2310
+ person_name="Ifeanyi",
2311
+ )
2312
+
2313
+ await Student.adelete_or_404(person_name="Ifeanyi")
2314
+
2315
+ student, created = await Student.aget_or_create(
2316
+ defaults={"course": "Python"},
2317
+ person_name="Ifeanyi",
2318
+ )
2319
+ ```
2320
+
2321
+ The async helpers are `aget_or_404`, `acreate_update`, `aupdate_or_404`, `adelete_or_404`, and `aget_or_create`. Pass `session=...` when using an explicit `AsyncSession`; otherwise they use the active LenAPI request session. The `*_or_404` methods raise `HTTPException(404, ...)` when no row matches.
2322
+
2298
2323
  For a declared foreign-key column, `.special("field_name")` resolves its related model and exposes it under that same field name:
2299
2324
 
2300
2325
  ```python
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "lenapi"
7
- version = "0.1.4.1"
7
+ version = "0.1.4.2"
8
8
  description = "A modern ASGI framework with Pydantic validation, sync and async database access, WebSockets, authentication, and integrated API documentation."
9
9
  authors = [
10
10
  {name = "Ifeanyi M. Onovo", email = "ifeanyi@maekandex.com.ng"},
@@ -721,6 +721,10 @@ def test_model_fields_defaults_uuid_unique_indexes_and_all_crud_helpers(tmp_path
721
721
  assert one.id == 1 and isinstance(one.public_id, uuid.UUID) and one.active is True
722
722
  assert FeatureRecord.get(name="Ada").get().info == {"active": True}
723
723
  assert FeatureRecord.get_or_404(name="Grace").amount == Decimal("25.00")
724
+ with pytest.raises(HTTPException) as missing_sync_get:
725
+ FeatureRecord.get_or_404(name="missing")
726
+ assert missing_sync_get.value.status_code == 404
727
+ assert str(missing_sync_get.value.detail) == "Not found"
724
728
  assert FeatureRecord.get(name="missing").exists() is False
725
729
  assert FeatureRecord.filter(group="math").exists() is True
726
730
  assert FeatureRecord.filter(group="missing").exists() is False
@@ -747,6 +751,67 @@ def test_model_fields_defaults_uuid_unique_indexes_and_all_crud_helpers(tmp_path
747
751
  db.close()
748
752
 
749
753
 
754
+ def test_async_crud_or_404_and_upsert_helpers(tmp_path):
755
+ async def run():
756
+ db = AsyncDatabase(f"sqlite+aiosqlite:///{tmp_path / 'async-crud-helpers.db'}")
757
+ async with db.engine.begin() as connection:
758
+ await connection.run_sync(FeatureRecord.__table__.create)
759
+ try:
760
+ async with db.session() as session:
761
+ ada = await FeatureRecord.acreate(
762
+ session=session, name="Ada async", group="logic"
763
+ )
764
+ assert ada.id is not None
765
+ assert (await FeatureRecord.aget_or_404(
766
+ session=session, name="Ada async"
767
+ )).group == "logic"
768
+
769
+ with pytest.raises(HTTPException) as missing_get:
770
+ await FeatureRecord.aget_or_404(session=session, name="missing")
771
+ assert missing_get.value.status_code == 404
772
+ assert str(missing_get.value.detail) == "Not found"
773
+
774
+ existing, created = await FeatureRecord.aget_or_create(
775
+ session=session, name="Ada async", defaults={"group": "unused"}
776
+ )
777
+ assert created is False and existing.group == "logic"
778
+ created_record, created = await FeatureRecord.aget_or_create(
779
+ session=session, name="Turing async", defaults={"group": "math"}
780
+ )
781
+ assert created is True and created_record.group == "math"
782
+
783
+ updated = await FeatureRecord.acreate_update(
784
+ {"group": "updated"}, session=session, name="Ada async"
785
+ )
786
+ assert updated.group == "updated"
787
+ inserted = await FeatureRecord.acreate_update(
788
+ {"group": "new"}, session=session, name="Grace async"
789
+ )
790
+ assert inserted.group == "new"
791
+
792
+ changed = await FeatureRecord.aupdate_or_404(
793
+ {"group": "updated again"}, session=session, name="Ada async"
794
+ )
795
+ assert changed.group == "updated again"
796
+ with pytest.raises(HTTPException) as missing_update:
797
+ await FeatureRecord.aupdate_or_404(
798
+ {"group": "never"}, session=session, name="missing"
799
+ )
800
+ assert missing_update.value.status_code == 404
801
+
802
+ assert await FeatureRecord.adelete_or_404(
803
+ session=session, name="Turing async"
804
+ ) is True
805
+ assert await FeatureRecord.aget(session=session, name="Turing async") is None
806
+ with pytest.raises(HTTPException) as missing_delete:
807
+ await FeatureRecord.adelete_or_404(session=session, name="missing")
808
+ assert missing_delete.value.status_code == 404
809
+ finally:
810
+ await db.close()
811
+
812
+ asyncio.run(run())
813
+
814
+
750
815
  def test_transaction_atomic_commits_and_rolls_back_sync_model_creates(tmp_path):
751
816
  db = Database(f"sqlite:///{tmp_path / 'atomic-sync.db'}")
752
817
  FeatureRecord.__table__.create(db.engine)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes