Skip to main content

The needed admin for Saffier ORM with Esmerald

Project description

Esmerald Admin

Esmerald

The needed admin for Saffier ORM with Esmerald.

Test Suite Package version Supported Python versions


Documentation: https://esmerald-admin.tarsild.io 📚

Source Code: https://github.com/tarsil/esmerald-admin


Esmerald admin for Saffier ORM

Esmerald admin is a flexible user interface for Saffier ORM built on the top of the already existing and highly maintained SQLAdmin.

The main goal, as the name of the package says, is to provide a nice, flexible and easy to use user interface that interacts with Saffier ORM in a more friendly manner.

Saffier

Saffier is a flexible and powerfull ORM built on the top of SQLAlchemy core that allows you to interact with almost every single SQL database out there in an asynchronous mode.

Documentation

Majority of the documentation for this package can and should be seen in the SQLAdmin official documentation (don't forget to leave a star on that repository) as the core remains exactly the same.

The custom, unique, Esmerald way is placed here within these docs.

Main features include:

  • SQLAlchemy sync/async engines
  • Esmerald integration
  • Saffier support
  • Modern UI using Tabler

Installation

$ pip install esmerald-admin

Quickstart

Saffier is a very powerfull ORM as mentioned before and built on the top of SQLAlchemy core but also extremely flexible allowing to use the models in a declarative way, which is the way SQLAdmin is expecting to use.

This makes Saffier unique since you can use the declarative models for the admin and the core models for anything else.

See the declarative models for more details on this.

Let us create a some Saffier models first. This example assumes you use the contrib user from Esmerald.

!!! Warning Using the user provided by Esmerald is not mandatory and you can use your own design. The documentation uses the one provided by Esmerald as it is easier to explain and use.

import saffier
from esmerald.contrib.auth.saffier.base_user import AbstractUser
from saffier import Database, Registry

database = Database("sqlite:///db.sqlite")
registry = Registry(database=database)


class BaseModel(saffier.Model):
    class Meta:
        abstract = True
        registry = registry


class User(BaseModel, AbstractUser):
    """Inherits from the user base"""

    ...


# Create the tables
await registry.create_all()

Now using with Esmerald

Saffier, as mentioned before, has the declarative models ready to be used. These models are only used for the admin.

from esmerald import Esmerald
from esmerald_admin import Admin, ModelView

app = Esmerald()
admin = Admin(app, engine)

# Declarative User
DeclarativeUser = User.declarative()


class UserAdmin(ModelView, model=DeclarativeUser):
    column_list = [DeclarativeUser.id, DeclarativeUser.email]


admin.add_view(UserAdmin)

Or if you want some more "organised".

Settings

from functools import cached_property
from typing import Optional

from esmerald.conf.enums import EnvironmentType
from esmerald.conf.global_settings import EsmeraldAPISettings
from saffier import Database, Registry


class AppSettings(EsmeraldAPISettings):
    app_name: str = "My application in production mode."
    title: str = "My app"
    environment: Optional[str] = EnvironmentType.PRODUCTION
    secret_key: str = "esmerald-insecure-key"

    @cached_property
    def db_access(self):
        database = Database("sqlite:///db.sqlite")
        registry = Registry(database=database)
        return database, registry

Models

import saffier
from esmerald.conf import settings
from esmerald.contrib.auth.saffier.base_user import AbstractUser

database, models = settings.db_access


class BaseModel(saffier.Model):
    class Meta:
        abstract = True
        registry = models


class User(BaseModel, AbstractUser):
    """Inherits from the user base"""
    ...

Admin

from accounts.models import User as UserModel
from esmerald_admin import Admin, ModelView

# Declarative Models
User = UserModel.declarative()


class UserAdmin(ModelView, model=User):
    column_list = [User.id, User.username, User.email, User.first_name, User.last_name]


def get_views(admin: Admin) -> None:
    """Generates the admin views"""
    admin.add_model_view(UserAdmin)

Application

import os
import sys
from pathlib import Path

from esmerald import Esmerald, Include, settings
from esmerald_admin import Admin


def build_path():
    """
    Builds the path of the project and project root.
    """
    Path(__file__).resolve().parent.parent
    SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

    if SITE_ROOT not in sys.path:
        sys.path.append(SITE_ROOT)
        sys.path.append(os.path.join(SITE_ROOT, "apps"))


def get_admin(app, registry):
    """Starts the saffier admin"""
    from .admin import get_views

    admin = Admin(app, registry.engine)

    # Get the views function from the "admin.py"
    get_views(admin)


def get_application():
    """
    This is optional. The function is only used for organisation purposes.
    """
    build_path()

    # Registry that comes from the "settings.py"
    # This is Saffier related and centralised in the settings
    # file, as per Esmerald design
    database, registry = settings.db_access

    app = Esmerald(
        routes=[Include(namespace="linezap.urls")],
        on_startup=[database.connect],
        on_shutdown=[database.disconnect],
    )

    # Admin
    get_admin(app, registry)

    return app


app = get_application()

Now visiting /admin/ (with slash at the end) on your browser you can see the Esmerald admin interface.

Important

As mentioned before, Esmerald admin is built on the top of SQLAdmin. Besides some unique features for Esmerald with Saffier that are documented here, everything else should be checked in the SQLAdmin official documentation as it works exactly the same.

Massive thanks to @aminalaee to get this working so well and without his work, this would not be possible! ⭐️ Star his repo! ⭐️

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

esmerald_admin-0.3.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

esmerald_admin-0.3.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file esmerald_admin-0.3.0.tar.gz.

File metadata

  • Download URL: esmerald_admin-0.3.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.17

File hashes

Hashes for esmerald_admin-0.3.0.tar.gz
Algorithm Hash digest
SHA256 17113d9a36f0eced0c54404ca7efb8d9f71ea007c32ce6322d115cc4669c58b6
MD5 af2be989a0cba4c6e9bbc7254d3acf71
BLAKE2b-256 f96935a659795c157e292fb0f3966195c21cfd7876f15474ea12cfc22db47e67

See more details on using hashes here.

File details

Details for the file esmerald_admin-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: esmerald_admin-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.17

File hashes

Hashes for esmerald_admin-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5d0398429b939886bd08b2d9a1938a266ab7fe95d48c4b22e5ea6170947acab7
MD5 81f08bbc13b5c2387966a0e2242f49cd
BLAKE2b-256 cd5522cdd33e94e047d5b76865a62401ca4b194caa6592526727ea76522a963e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page