sqladmin-litestar-plugin


Namesqladmin-litestar-plugin JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2024-08-16 03:01:05
maintainerNone
docs_urlNone
authorPeter Schutt
requires_python<4.0,>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sqladmin-litestar-plugin

## Overview

The `SQLAdminPlugin` integrates SQLAdmin with a Litestar application.

## Acknowledgements

Thanks to [aminalaee](https://github.com/aminalaee) and all [contributors](https://github.com/aminalaee/sqladmin/graphs/contributors) for the excellent [SQLAdmin](https://github.com/aminalaee/sqladmin) project.

## Installation

To install the dependencies, run:

```bash
pip install sqladmin-litestar-plugin
```

## Usage

To use the plugin, import the `SQLAdminPlugin` class and pass it to the `Litestar` application.

By default, the plugin will create a new admin interface at `/admin`.

### Example

```python
from litestar import Litestar
from sqladmin import ModelView
from sqladmin_litestar_plugin import SQLAdminPlugin
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import declarative_base


engine = create_async_engine("sqlite+aiosqlite:///example.db")
Base = declarative_base()


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String)


class UserAdmin(ModelView, model=User):
    column_list = [User.id, User.name]


async def on_startup() -> None:
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)  # Create tables


admin = SQLAdminPlugin(views=[UserAdmin], engine=engine)
app = Litestar(plugins=[admin], on_startup=[on_startup])
```

## Configuration

The `SQLAdminPlugin` accepts the following arguments:

- `views`: A sequence of `ModelView` classes to add to the admin app. Each `ModelView` class configures the interface for a SQL model.
- `engine`: An SQLAlchemy engine to connect to your database.
- `sessionmaker`: An SQLAlchemy `sessionmaker` instance used to manage sessions.
- `base_url`: The base URL where the admin app will be hosted.
- `title`: The title of the admin app, which appears in the browser's title bar and the header of the admin interface.
- `logo_url`: The URL of the logo to display in the admin app, enhancing brand visibility.
- `templates_dir`: The directory containing the Jinja2 templates for the admin interface, allowing for customization of the UI.
- `middlewares`: A sequence of Starlette middlewares to add to the admin app, useful for handling requests or adding additional functionality.
- `authentication_backend`: An authentication backend to secure the admin app, managing user authentication and authorization.

Views are not added to the admin app until the Litestar application is instantiated, so you can append views to the
`views` list until this point.

## Use with Advanced-Alchemy Audit Base Variants

Advanced-Alchemy (AA) provides variants of base models that include `created_at` and `updated_at` fields which enforce
that `tzinfo` is set on the `datetime` instance passed through to SQLAlchemy.

When a model is created via the SQLAdmin UI, the `created_at` and `updated_at` fields default to the current time in UTC,
however, the `tzinfo` property of the `datetime` is not set.

`sqladmin-litestar-plugin` provides a custom `ModelView` class that ensures the `tzinfo` property is set on `datetime`
instances when the form field represents an AA `DateTimeUTC` field.

Example:

```python
from __future__ import annotations

from advanced_alchemy.base import UUIDAuditBase
from sqlalchemy import Column, String

from sqladmin_litestar_plugin.ext.advanced_alchemy import AuditModelView


class Entity(UUIDAuditBase):
    my_column = Column(String(10))


class EntityAdmin(AuditModelView, model=Entity): ...
```

For a full working example, see the `examples/aa_audit_base` directory in this repo.

The `AuditModelView` class should also be useful for models that don't depend on one of the AA audit model bases, but
still use `DateTimeUTC` fields.


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "sqladmin-litestar-plugin",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Peter Schutt",
    "author_email": "peter.github@proton.me",
    "download_url": "https://files.pythonhosted.org/packages/a9/89/90fdb698565857b79c3cc84058c705acab75cfe19ec8cfd6c82ffb15fb3d/sqladmin_litestar_plugin-0.2.0.tar.gz",
    "platform": null,
    "description": "# sqladmin-litestar-plugin\n\n## Overview\n\nThe `SQLAdminPlugin` integrates SQLAdmin with a Litestar application.\n\n## Acknowledgements\n\nThanks to [aminalaee](https://github.com/aminalaee) and all [contributors](https://github.com/aminalaee/sqladmin/graphs/contributors) for the excellent [SQLAdmin](https://github.com/aminalaee/sqladmin) project.\n\n## Installation\n\nTo install the dependencies, run:\n\n```bash\npip install sqladmin-litestar-plugin\n```\n\n## Usage\n\nTo use the plugin, import the `SQLAdminPlugin` class and pass it to the `Litestar` application.\n\nBy default, the plugin will create a new admin interface at `/admin`.\n\n### Example\n\n```python\nfrom litestar import Litestar\nfrom sqladmin import ModelView\nfrom sqladmin_litestar_plugin import SQLAdminPlugin\nfrom sqlalchemy import Column, Integer, String\nfrom sqlalchemy.ext.asyncio import create_async_engine\nfrom sqlalchemy.orm import declarative_base\n\n\nengine = create_async_engine(\"sqlite+aiosqlite:///example.db\")\nBase = declarative_base()\n\n\nclass User(Base):\n    __tablename__ = \"users\"\n\n    id = Column(Integer, primary_key=True)\n    name = Column(String)\n\n\nclass UserAdmin(ModelView, model=User):\n    column_list = [User.id, User.name]\n\n\nasync def on_startup() -> None:\n    async with engine.begin() as conn:\n        await conn.run_sync(Base.metadata.create_all)  # Create tables\n\n\nadmin = SQLAdminPlugin(views=[UserAdmin], engine=engine)\napp = Litestar(plugins=[admin], on_startup=[on_startup])\n```\n\n## Configuration\n\nThe `SQLAdminPlugin` accepts the following arguments:\n\n- `views`: A sequence of `ModelView` classes to add to the admin app. Each `ModelView` class configures the interface for a SQL model.\n- `engine`: An SQLAlchemy engine to connect to your database.\n- `sessionmaker`: An SQLAlchemy `sessionmaker` instance used to manage sessions.\n- `base_url`: The base URL where the admin app will be hosted.\n- `title`: The title of the admin app, which appears in the browser's title bar and the header of the admin interface.\n- `logo_url`: The URL of the logo to display in the admin app, enhancing brand visibility.\n- `templates_dir`: The directory containing the Jinja2 templates for the admin interface, allowing for customization of the UI.\n- `middlewares`: A sequence of Starlette middlewares to add to the admin app, useful for handling requests or adding additional functionality.\n- `authentication_backend`: An authentication backend to secure the admin app, managing user authentication and authorization.\n\nViews are not added to the admin app until the Litestar application is instantiated, so you can append views to the\n`views` list until this point.\n\n## Use with Advanced-Alchemy Audit Base Variants\n\nAdvanced-Alchemy (AA) provides variants of base models that include `created_at` and `updated_at` fields which enforce\nthat `tzinfo` is set on the `datetime` instance passed through to SQLAlchemy.\n\nWhen a model is created via the SQLAdmin UI, the `created_at` and `updated_at` fields default to the current time in UTC,\nhowever, the `tzinfo` property of the `datetime` is not set.\n\n`sqladmin-litestar-plugin` provides a custom `ModelView` class that ensures the `tzinfo` property is set on `datetime`\ninstances when the form field represents an AA `DateTimeUTC` field.\n\nExample:\n\n```python\nfrom __future__ import annotations\n\nfrom advanced_alchemy.base import UUIDAuditBase\nfrom sqlalchemy import Column, String\n\nfrom sqladmin_litestar_plugin.ext.advanced_alchemy import AuditModelView\n\n\nclass Entity(UUIDAuditBase):\n    my_column = Column(String(10))\n\n\nclass EntityAdmin(AuditModelView, model=Entity): ...\n```\n\nFor a full working example, see the `examples/aa_audit_base` directory in this repo.\n\nThe `AuditModelView` class should also be useful for models that don't depend on one of the AA audit model bases, but\nstill use `DateTimeUTC` fields.\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.2.0",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8ce79398b0a4b162b69a311241acb2370c705c5d067cd1af72375efc617d1770",
                "md5": "f70c6d34bfa49f2e539ae42a8d9159b2",
                "sha256": "07e6d5a3f996e8618c305f5401d2040375617453bd08314132bb028295341ecf"
            },
            "downloads": -1,
            "filename": "sqladmin_litestar_plugin-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f70c6d34bfa49f2e539ae42a8d9159b2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 6870,
            "upload_time": "2024-08-16T03:01:04",
            "upload_time_iso_8601": "2024-08-16T03:01:04.645739Z",
            "url": "https://files.pythonhosted.org/packages/8c/e7/9398b0a4b162b69a311241acb2370c705c5d067cd1af72375efc617d1770/sqladmin_litestar_plugin-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a98990fdb698565857b79c3cc84058c705acab75cfe19ec8cfd6c82ffb15fb3d",
                "md5": "00a1e4df3b63e831be891c0e04fa94bb",
                "sha256": "fb0bbc64812528324cd753738788a95ee298cc33066e9382646ecd1995a61fff"
            },
            "downloads": -1,
            "filename": "sqladmin_litestar_plugin-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "00a1e4df3b63e831be891c0e04fa94bb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 6900,
            "upload_time": "2024-08-16T03:01:05",
            "upload_time_iso_8601": "2024-08-16T03:01:05.870432Z",
            "url": "https://files.pythonhosted.org/packages/a9/89/90fdb698565857b79c3cc84058c705acab75cfe19ec8cfd6c82ffb15fb3d/sqladmin_litestar_plugin-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-16 03:01:05",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "sqladmin-litestar-plugin"
}
        
Elapsed time: 0.86409s