-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into prep-release
- Loading branch information
Showing
13 changed files
with
290 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from __future__ import annotations | ||
|
||
from advanced_alchemy.base import UUIDAuditBase | ||
from advanced_alchemy.extensions.litestar import SQLAlchemyPlugin | ||
from litestar import Litestar | ||
from litestar.contrib.sqlalchemy.plugins import SQLAlchemyAsyncConfig | ||
from sqlalchemy import Column, String | ||
from sqlalchemy.ext.asyncio import create_async_engine | ||
|
||
import sqladmin_litestar_plugin | ||
from sqladmin_litestar_plugin.ext.advanced_alchemy import AuditModelView | ||
|
||
engine = create_async_engine("sqlite+aiosqlite:///") | ||
|
||
|
||
class Entity(UUIDAuditBase): | ||
my_column = Column(String(10)) | ||
|
||
|
||
class EntityAdmin(AuditModelView, model=Entity): ... | ||
|
||
|
||
app = Litestar( | ||
plugins=( | ||
SQLAlchemyPlugin(config=SQLAlchemyAsyncConfig(engine_instance=engine, create_all=True)), | ||
sqladmin_litestar_plugin.SQLAdminPlugin( | ||
views=[EntityAdmin], engine=engine, base_url="/admin" | ||
), | ||
), | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import timezone | ||
from typing import Any | ||
|
||
from sqladmin import ModelView | ||
from sqladmin.forms import ModelConverter, converts | ||
from wtforms import DateTimeField | ||
|
||
|
||
class DateTimeUTCField(DateTimeField): | ||
def process_formdata(self, valuelist: list[Any]) -> None: | ||
super().process_formdata(valuelist) | ||
|
||
if self.data is None: | ||
return | ||
|
||
self.data = self.data.replace(tzinfo=timezone.utc) | ||
|
||
|
||
class DateTimeUTCConverter(ModelConverter): | ||
# mypy: error: Untyped decorator makes function "convert_date_time_utc" untyped [misc] | ||
@converts("DateTimeUTC") # type: ignore[misc] | ||
def convert_date_time_utc(self, *, kwargs: dict[str, Any], **_: Any) -> DateTimeUTCField: # noqa: PLR6301 | ||
return DateTimeUTCField(**kwargs) | ||
|
||
|
||
class AuditModelView(ModelView): | ||
form_converter = DateTimeUTCConverter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def anyio_backend() -> object: | ||
return "asyncio" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime, timezone | ||
from typing import Any, Dict | ||
|
||
import pytest | ||
from advanced_alchemy.base import UUIDAuditBase | ||
from sqlalchemy import Column, String | ||
from sqlalchemy.orm import sessionmaker | ||
from wtforms import Form | ||
|
||
from sqladmin_litestar_plugin.ext.advanced_alchemy import AuditModelView, DateTimeUTCField | ||
|
||
|
||
class DummyPostData(Dict[str, Any]): | ||
def getlist(self, key: str) -> list[Any]: | ||
v = self[key] | ||
if isinstance(v, (list, tuple)): | ||
return list(v) | ||
return [v] | ||
|
||
|
||
def test_date_time_utc_field() -> None: | ||
class F(Form): | ||
f = DateTimeUTCField() | ||
|
||
form = F(DummyPostData({"f": "2021-01-01 00:00:00"})) | ||
assert form.f.data == datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc) | ||
|
||
|
||
def test_date_time_utc_field_on_no_data() -> None: | ||
class F(Form): | ||
f = DateTimeUTCField() | ||
|
||
form = F(DummyPostData({"f": []})) | ||
assert form.f.data is None | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_audit_model_view() -> None: | ||
class MyModel(UUIDAuditBase): | ||
my_column = Column(String(10)) | ||
|
||
class MyModelView(AuditModelView, model=MyModel): | ||
session_maker = sessionmaker() | ||
|
||
view = MyModelView() | ||
form = await view.scaffold_form() | ||
|
||
assert isinstance(form()._fields["created_at"], DateTimeUTCField) | ||
assert isinstance(form()._fields["updated_at"], DateTimeUTCField) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters