Skip to main content

The needed admin for Saffier ORM with Esmerald

Project description

Esmerald Admin

Esmerald

The needed admin for Saffier and Edgy 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 and Edgy

Esmerald admin is a flexible user interface for Saffier ORM and Edgy 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 and Edgy 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.

Edgy

Edgy is also an extremely, flexible and powerful ORM built on the top of SQLAlchemy core and 100% Pydantic with more flexibility for every single use case, also in 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
  • Edgy support
  • Modern UI using Tabler

Installation

For Saffier

$ pip install esmerald-admin[saffier]

For Edgy

$ pip install esmerald-admin[edgy]

For both

$ pip install esmerald-admin[all]

Quickstart

Saffier and Edgy are very powerfull ORMs 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 both Saffier and Edgy unique since you can use the declarative models for the admin and the core models for anything else.

See the Saffier declarative models and Edgy declarative models for more details.

Let us create a some models first. This example assumes you use the contrib user of Saffier and the contrib user of Edgy 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.

Saffier

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()

Edgy

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

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


class BaseModel(edgy.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 accounts.models import User
from esmerald import Esmerald, settings

from esmerald_admin import Admin, ModelView

database, registry = settings.db_access

app = Esmerald()
admin = Admin(app, registry.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 # or from edgy 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

Saffier Models

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"""

Edgy Models

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

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


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


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 and it is used in
    the `main.py` file.
    """
    admin.add_model_view(UserAdmin)

Application

from accounts.models import User
from esmerald import Esmerald, settings

from esmerald_admin import Admin, ModelView

database, registry = settings.db_access

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

# Declarative User
DeclarativeUser = User.declarative()


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


admin.add_view(UserAdmin)

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.4.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

esmerald_admin-0.4.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for esmerald_admin-0.4.0.tar.gz
Algorithm Hash digest
SHA256 91100c442111993b571a2964953f9090179bf30fe695f2673e33919e8425655b
MD5 9c35e58b0ed8182f0068de078dc90554
BLAKE2b-256 dec9de82619e49838d667df70d73e98a187ce872be91aa691138d2e194e9bd9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for esmerald_admin-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfb501034582f652e43b54594809f7b63cb7e95b805c28b1eaf10b58081c3a3b
MD5 e3a7a9149c0b40b22d7f8284abe3cbfc
BLAKE2b-256 3a7aee00aaf46f6ce9057470bd0e88ca5d4d69da171fd879410a9c02778dc3a8

See more details on using hashes here.

Supported by

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