fix doctests

This commit is contained in:
Manuel Gugger 2022-09-07 14:38:21 +02:00
parent 879dfff032
commit 4fc35d0e97
2 changed files with 142 additions and 136 deletions

191
README.md
View File

@ -1,112 +1,115 @@
# Actix Admin
# Actix Admin
The actix-admin crate aims at creating a web admin interface similar to other admin interfaces (such as [flask-admin](https://github.com/flask-admin/flask-admin) in python).
The actix-admin crate aims at creating a web admin interface similar to other admin interfaces (such as [flask-admin](https://github.com/flask-admin/flask-admin) in python).
## Getting Started
## Getting Started
* See the [example](https://github.com/mgugger/actix-admin/tree/main/example).
* See the step by [step tutorial](https://github.com/mgugger/actix-admin/tree/main/example/StepbyStep.md)
* See the [example](https://github.com/mgugger/actix-admin/tree/main/example).
* See the step by [step tutorial](https://github.com/mgugger/actix-admin/tree/main/example/StepbyStep.md)
## Features
1. Async, builds on [sea-orm](https://crates.io/crates/sea-orm) for the database backend
2. Macros, generate the required implementations for models automatically
3. Authentication, optionally pass authentication handler to implement authentication for views
4. Supports custom validation rules
5. Searchable attributes can be specified
6. Supports a custom index view
## Features
1. Async, builds on [sea-orm](https://crates.io/crates/sea-orm) for the database backend
2. Macros, generate the required implementations for models automatically
3. Authentication, optionally pass authentication handler to implement authentication for views
4. Supports custom validation rules
5. Searchable attributes can be specified
6. Supports a custom index view
## Screenshot
## Screenshot
<img src="https://raw.githubusercontent.com/mgugger/actix-admin/main/static/Screenshot.png"/>
<img src="https://raw.githubusercontent.com/mgugger/actix-admin/main/static/Screenshot.png"/>
## Quick overview
## Quick overview
### Required dependencies
```
itertools = "0.10.3"
sea-orm = { version = "^0.9.1", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = true }
actix_admin = { version = "^0.1.0" }
```
### Required dependencies
itertools = "0.10.3"
sea-orm = { version = "^0.9.1", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = true }
actix_admin = { version = "^0.1.0" }
### Derive Macros
```
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use actix_admin::prelude::*;
use super::Post;
### See inlined steps
```
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use actix_admin::prelude::*;
use actix_web::web;
use actix_web::App;
use actix_web::HttpServer;
use sea_orm::entity::prelude::*;
use sea_orm::entity::prelude::*;
use actix_admin::prelude::*;
// 1. Import ActixAdmin
use actix_admin::prelude::*;
// Use DeriveActixAmin* Macros to implement the traits for the model
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize,
DeriveActixAdmin, DeriveActixAdminModel, DeriveActixAdminViewModel
)]
#[sea_orm(table_name = "comment")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
#[actix_admin(primary_key)]
pub id: i32,
pub comment: String
}
```
// 2. Use DeriveActixAmin* Macros to implement the traits for the model
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize,
DeriveActixAdmin, DeriveActixAdminModel, DeriveActixAdminViewModel
)]
#[sea_orm(table_name = "comment")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
#[actix_admin(primary_key)]
pub id: i32,
pub comment: String
}
impl ActixAdminModelValidationTrait<ActiveModel> for Entity {}
impl ActiveModelBehavior for ActiveModel {}
### Add actix-admin to the AppState
```
pub struct AppState {
pub db: DatabaseConnection,
pub actix_admin: ActixAdmin,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation { }
impl ActixAdminAppDataTrait for AppState {
fn get_db(&self) -> &DatabaseConnection {
&self.db
}
// 3. Add actix-admin to the AppState
#[derive(Clone)]
pub struct AppState {
pub db: DatabaseConnection,
pub actix_admin: ActixAdmin,
}
fn get_actix_admin(&self) -> &ActixAdmin {
&self.actix_admin
}
}
```
// 4. Implement the ActixAdminAppDataTrait for the AppState
impl ActixAdminAppDataTrait for AppState {
fn get_db(&self) -> &DatabaseConnection {
&self.db
}
### Actix Admin Builder
```
pub fn create_actix_admin_builder() -> ActixAdminBuilder {
let comment_view_model = ActixAdminViewModel::from(Comment);
//!
let configuration = ActixAdminConfiguration {
enable_auth: false,
user_is_logged_in: None,
login_link: None,
logout_link: None,
};
//!
let mut admin_builder = ActixAdminBuilder::new(configuration);
admin_builder.add_entity::<AppState, Comment>(&comment_view_model);
//!
admin_builder
}
```
fn get_actix_admin(&self) -> &ActixAdmin {
&self.actix_admin
}
}
### Add to the actix app
```
let actix_admin = create_actix_admin_builder().get_actix_admin();
// 5. Setup the actix admin configuration
pub fn create_actix_admin_builder() -> ActixAdminBuilder {
let comment_view_model = ActixAdminViewModel::from(Entity);
let app_state = AppState {
db: conn,
actix_admin: actix_admin,
};
let configuration = ActixAdminConfiguration {
enable_auth: false,
user_is_logged_in: None,
login_link: None,
logout_link: None,
};
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(app_state.clone()))
.wrap(SessionMiddleware::new(CookieSessionStore::default(), cookie_secret_key.clone()))
.route("/", web::get().to(index))
.service(azure_auth.clone().create_scope::<AppState>())
.service(
create_actix_admin_builder().get_scope::<AppState>()
)
.wrap(middleware::Logger::default())
})
```
let mut admin_builder = ActixAdminBuilder::new(configuration);
admin_builder.add_entity::<AppState, Entity>(&comment_view_model);
### Access
The admin interface will be available under /admin/.
admin_builder
}
// 6. Add to the actix app
let actix_admin = create_actix_admin_builder().get_actix_admin();
//let opt = ConnectOptions::new("sqlite::memory:".to_owned());
//let conn = sea_orm::Database::connect(opt).unwrap();
//let app_state = AppState {
// db: conn,
// actix_admin: actix_admin,
//};
HttpServer::new(move || {
App::new()
//.app_data(web::Data::new(app_state.clone()))
.service(
create_actix_admin_builder().get_scope::<AppState>()
)
});
```
## Access
The admin interface will be available under /admin/.

View File

@ -22,20 +22,25 @@
//! ## Quick overview
//!
//! ### Required dependencies
//! ```
//! itertools = "0.10.3"
//! sea-orm = { version = "^0.9.1", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = true }
//! actix_admin = { version = "^0.1.0" }
//! ```
//!
//! ### Derive Macros
//! ### See inlined steps
//! ```
//! use sea_orm::entity::prelude::*;
//! use serde::{Deserialize, Serialize};
//! use actix_admin::prelude::*;
//! use super::Post;
//! use actix_web::web;
//! use actix_web::App;
//! use actix_web::HttpServer;
//! use sea_orm::entity::prelude::*;
//! use sea_orm::entity::prelude::*;
//! use actix_admin::prelude::*;
//! // 1. Import ActixAdmin
//! use actix_admin::prelude::*;
//!
//! // Use DeriveActixAmin* Macros to implement the traits for the model
//! // 2. Use DeriveActixAmin* Macros to implement the traits for the model
//! #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize,
//! DeriveActixAdmin, DeriveActixAdminModel, DeriveActixAdminViewModel
//! )]
@ -47,15 +52,20 @@
//! pub id: i32,
//! pub comment: String
//! }
//! ```
//! impl ActixAdminModelValidationTrait<ActiveModel> for Entity {}
//! impl ActiveModelBehavior for ActiveModel {}
//!
//! ### Add actix-admin to the AppState
//! ```
//! #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
//! pub enum Relation { }
//!
//! // 3. Add actix-admin to the AppState
//! #[derive(Clone)]
//! pub struct AppState {
//! pub db: DatabaseConnection,
//! pub actix_admin: ActixAdmin,
//! }
//!
//! // 4. Implement the ActixAdminAppDataTrait for the AppState
//! impl ActixAdminAppDataTrait for AppState {
//! fn get_db(&self) -> &DatabaseConnection {
//! &self.db
@ -65,12 +75,10 @@
//! &self.actix_admin
//! }
//! }
//! ```
//!
//! ### Actix Admin Builder
//! ```
//! // 5. Setup the actix admin configuration
//! pub fn create_actix_admin_builder() -> ActixAdminBuilder {
//! let comment_view_model = ActixAdminViewModel::from(Comment);
//! let comment_view_model = ActixAdminViewModel::from(Entity);
//!
//! let configuration = ActixAdminConfiguration {
//! enable_auth: false,
@ -80,35 +88,30 @@
//! };
//!
//! let mut admin_builder = ActixAdminBuilder::new(configuration);
//! admin_builder.add_entity::<AppState, Comment>(&comment_view_model);
//! admin_builder.add_entity::<AppState, Entity>(&comment_view_model);
//!
//! admin_builder
//! }
//! ```
//!
//! ### Add to the actix app
//! ```
//! // 6. Add to the actix app
//! let actix_admin = create_actix_admin_builder().get_actix_admin();
//!
//! let app_state = AppState {
//! db: conn,
//! actix_admin: actix_admin,
//! };
//! //let opt = ConnectOptions::new("sqlite::memory:".to_owned());
//! //let conn = sea_orm::Database::connect(opt).unwrap();
//! //let app_state = AppState {
//! // db: conn,
//! // actix_admin: actix_admin,
//! //};
//!
//! HttpServer::new(move || {
//! App::new()
//! .app_data(web::Data::new(app_state.clone()))
//! .wrap(SessionMiddleware::new(CookieSessionStore::default(), cookie_secret_key.clone()))
//! .route("/", web::get().to(index))
//! .service(azure_auth.clone().create_scope::<AppState>())
//! //.app_data(web::Data::new(app_state.clone()))
//! .service(
//! create_actix_admin_builder().get_scope::<AppState>()
//! )
//! .wrap(middleware::Logger::default())
//! })
//! });
//! ```
//!
//! ### Access
//! ## Access
//! The admin interface will be available under /admin/.
use lazy_static::lazy_static;