Package: esmerald-admin
Esmerald AdminThe needed admin for Saffier and Edgy with Esmerald.Documentation:https://esmerald-admin.tarsild.io📚Source Code:https://github.com/tarsil/esmerald-adminEsmerald admin forSaffierandEdgyEsmerald admin is a flexible user interface forSaffier ORMandEdgybuilt on the top of the already existing and highly maintainedSQLAdmin.The main goal, as the name of the package says, is to provide a nice, flexible and easy to use
user interface that interacts withSaffierandEdgyin a more friendly manner.SaffierSaffieris 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.EdgyEdgyis also an extremely, flexible and powerful ORM built on the top of SQLAlchemy core
and100% Pydanticwith more flexibility for every single use case, also in asynchronous mode.DocumentationMajority of the documentation for this packagecan and shouldbe seen in theSQLAdminofficial 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 enginesEsmerald integrationSaffiersupportEdgysupportModern UI using TablerInstallationFor Saffier$pipinstallesmerald-admin[saffier]For Edgy$pipinstallesmerald-admin[edgy]For both$pipinstallesmerald-admin[all]QuickstartSaffier 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 adeclarativeway, 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 theSaffier declarative modelsandEdgy declarative modelsfor more details.Let us create a some models first. This example assumes you use thecontrib user of Saffierand thecontrib user of Edgyfrom Esmerald.!!! Warning
Using the user provided by Esmerald isnot mandatoryand you can use your own design.
The documentation uses the one provided by Esmerald as it is easier to explain and use.Saffierimportsaffierfromesmerald.contrib.auth.saffier.base_userimportAbstractUserfromsaffierimportDatabase,Registrydatabase=Database("sqlite:///db.sqlite")registry=Registry(database=database)classBaseModel(saffier.Model):classMeta:abstract=Trueregistry=registryclassUser(BaseModel,AbstractUser):"""Inherits from the user base"""# Create the tablesawaitregistry.create_all()EdgyimportedgyfromedgyimportDatabase,Registryfromesmerald.contrib.auth.edgy.base_userimportAbstractUserdatabase=Database("sqlite:///db.sqlite")registry=Registry(database=database)classBaseModel(edgy.Model):classMeta:abstract=Trueregistry=registryclassUser(BaseModel,AbstractUser):"""Inherits from the user base"""...# Create the tablesawaitregistry.create_all()Now using with EsmeraldSaffier, as mentioned before, has thedeclarative modelsready to be used.
These models areonly used for the admin.fromaccounts.modelsimportUserfromesmeraldimportEsmerald,settingsfromesmerald_adminimportAdmin,ModelViewdatabase,registry=settings.db_accessapp=Esmerald()admin=Admin(app,registry.engine)# Declarative UserDeclarativeUser=User.declarative()classUserAdmin(ModelView,model=DeclarativeUser):column_list=[DeclarativeUser.id,DeclarativeUser.email]admin.add_view(UserAdmin)Or if you want some more "organised".Settingsfromfunctoolsimportcached_propertyfromtypingimportOptionalfromesmerald.conf.enumsimportEnvironmentTypefromesmerald.conf.global_settingsimportEsmeraldAPISettingsfromsaffierimportDatabase,Registry# or from edgy import Database, RegistryclassAppSettings(EsmeraldAPISettings):app_name:str="My application in production mode."title:str="My app"environment:Optional[str]=EnvironmentType.PRODUCTIONsecret_key:str="esmerald-insecure-key"@cached_propertydefdb_access(self):database=Database("sqlite:///db.sqlite")registry=Registry(database=database)returndatabase,registrySaffier Modelsimportsaffierfromesmerald.contrib.auth.saffier.base_userimportAbstractUserfromsaffierimportDatabase,Registrydatabase=Database("sqlite:///db.sqlite")registry=Registry(database=database)classBaseModel(saffier.Model):classMeta:abstract=Trueregistry=registryclassUser(BaseModel,AbstractUser):"""Inherits from the user base"""Edgy ModelsimportedgyfromedgyimportDatabase,Registryfromesmerald.contrib.auth.edgy.base_userimportAbstractUserdatabase=Database("sqlite:///db.sqlite")registry=Registry(database=database)classBaseModel(edgy.Model):classMeta:abstract=Trueregistry=registryclassUser(BaseModel,AbstractUser):"""Inherits from the user base"""Adminfromaccounts.modelsimportUserasUserModelfromesmerald_adminimportAdmin,ModelView# Declarative ModelsUser=UserModel.declarative()classUserAdmin(ModelView,model=User):column_list=[User.id,User.username,User.email,User.first_name,User.last_name]defget_views(admin:Admin)->None:"""Generates the admin views and it is used inthe `main.py` file."""admin.add_model_view(UserAdmin)Applicationfromaccounts.modelsimportUserfromesmeraldimportEsmerald,settingsfromesmerald_adminimportAdmin,ModelViewdatabase,registry=settings.db_accessapp=Esmerald()admin=Admin(app,registry.engine)# Declarative UserDeclarativeUser=User.declarative()classUserAdmin(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.ImportantAs mentioned before, Esmerald admin is built on the top ofSQLAdmin. Besides some
unique features for Esmerald with Saffier that are documented here,everything else should be checkedin theSQLAdminofficial documentationas it works exactly the same.Massive thanks to@aminalaeeto get this working so well and without his work, this
would not be possible! ⭐️ Star his repo! ⭐️