include tera tmpls as raw
This commit is contained in:
parent
4ad1106166
commit
8ed3796e45
138
src/lib.rs
138
src/lib.rs
@ -4,39 +4,45 @@
|
|||||||
//!
|
//!
|
||||||
//! See the [documentation](https://mgugger.github.io/actix-admin/) at [https://mgugger.github.io/actix-admin/](https://mgugger.github.io/actix-admin/).
|
//! See the [documentation](https://mgugger.github.io/actix-admin/) at [https://mgugger.github.io/actix-admin/](https://mgugger.github.io/actix-admin/).
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use actix_session::Session;
|
||||||
use sea_orm::DatabaseConnection;
|
|
||||||
use serde::{Serialize };
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use tera::{Tera, Result, to_value, try_get_value };
|
|
||||||
use std::{ hash::BuildHasher};
|
|
||||||
use actix_session::{Session};
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use derive_more::{Display, Error};
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
error,
|
error,
|
||||||
http::{header::ContentType, StatusCode},
|
http::{header::ContentType, StatusCode},
|
||||||
HttpResponse,
|
HttpResponse,
|
||||||
};
|
};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use derive_more::{Display, Error};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use sea_orm::DatabaseConnection;
|
||||||
|
use serde::Serialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::hash::BuildHasher;
|
||||||
|
use tera::{to_value, try_get_value, Result, Tera};
|
||||||
|
|
||||||
pub mod view_model;
|
pub mod builder;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
pub mod builder;
|
pub mod view_model;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::builder::{ ActixAdminBuilder, ActixAdminBuilderTrait};
|
pub use crate::builder::{ActixAdminBuilder, ActixAdminBuilderTrait};
|
||||||
pub use crate::model::{ ActixAdminModel, ActixAdminModelValidationTrait, ActixAdminModelTrait};
|
pub use crate::model::{ActixAdminModel, ActixAdminModelTrait, ActixAdminModelValidationTrait};
|
||||||
pub use crate::view_model::{ ActixAdminViewModel, ActixAdminViewModelTrait, ActixAdminViewModelField, ActixAdminViewModelSerializable, ActixAdminViewModelFieldType };
|
pub use crate::routes::{create_or_edit_post, get_admin_ctx, SortOrder};
|
||||||
pub use actix_admin_macros::{ DeriveActixAdmin, DeriveActixAdminModel, DeriveActixAdminViewModel, DeriveActixAdminEnumSelectList, DeriveActixAdminModelSelectList };
|
pub use crate::view_model::{
|
||||||
pub use crate::{ ActixAdminError, ActixAdminAppDataTrait, ActixAdmin, ActixAdminConfiguration };
|
ActixAdminViewModel, ActixAdminViewModelField, ActixAdminViewModelFieldType,
|
||||||
pub use crate::{ hashmap, ActixAdminSelectListTrait };
|
ActixAdminViewModelSerializable, ActixAdminViewModelTrait,
|
||||||
pub use crate::routes::{ create_or_edit_post, get_admin_ctx, SortOrder };
|
};
|
||||||
pub use crate::{ TERA };
|
pub use crate::TERA;
|
||||||
|
pub use crate::{hashmap, ActixAdminSelectListTrait};
|
||||||
|
pub use crate::{ActixAdmin, ActixAdminAppDataTrait, ActixAdminConfiguration, ActixAdminError};
|
||||||
|
pub use actix_admin_macros::{
|
||||||
|
DeriveActixAdmin, DeriveActixAdminEnumSelectList, DeriveActixAdminModel,
|
||||||
|
DeriveActixAdminModelSelectList, DeriveActixAdminViewModel,
|
||||||
|
};
|
||||||
|
pub use actix_session::Session;
|
||||||
|
pub use async_trait::async_trait;
|
||||||
pub use itertools::izip;
|
pub use itertools::izip;
|
||||||
pub use lazy_static::lazy_static;
|
pub use lazy_static::lazy_static;
|
||||||
pub use async_trait::async_trait;
|
|
||||||
pub use actix_session::{Session};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
@ -57,38 +63,91 @@ lazy_static! {
|
|||||||
tera.register_filter("get_html_input_type", get_html_input_type);
|
tera.register_filter("get_html_input_type", get_html_input_type);
|
||||||
tera.register_filter("get_html_input_class", get_html_input_class);
|
tera.register_filter("get_html_input_class", get_html_input_class);
|
||||||
tera.register_filter("get_icon", get_icon);
|
tera.register_filter("get_icon", get_icon);
|
||||||
|
|
||||||
|
let list_html = include_str!("templates/list.html");
|
||||||
|
let create_or_edit_html = include_str!("templates/create_or_edit.html");
|
||||||
|
let base_html = include_str!("templates/base.html");
|
||||||
|
let head_html = include_str!("templates/head.html");
|
||||||
|
let index_html = include_str!("templates/index.html");
|
||||||
|
let loader_html = include_str!("templates/loader.html");
|
||||||
|
let navbar_html = include_str!("templates/navbar.html");
|
||||||
|
let not_found_html = include_str!("templates/not_found.html");
|
||||||
|
let show_html = include_str!("templates/show.html");
|
||||||
|
let unauthorized_html = include_str!("templates/unauthorized.html");
|
||||||
|
|
||||||
|
// form elements
|
||||||
|
let checkbox_html = include_str!("templates/form_elements/checkbox.html");
|
||||||
|
let input_html = include_str!("templates/form_elements/input.html");
|
||||||
|
let selectlist_html = include_str!("templates/form_elements/selectlist.html");
|
||||||
|
|
||||||
|
let _res = tera.add_raw_templates(vec![
|
||||||
|
("base.html", base_html),
|
||||||
|
("list.html", list_html),
|
||||||
|
("create_or_edit.html", create_or_edit_html),
|
||||||
|
("head.html", head_html),
|
||||||
|
("index.html", index_html),
|
||||||
|
("loader.html", loader_html),
|
||||||
|
("navbar.html", navbar_html),
|
||||||
|
("not_found.html", not_found_html),
|
||||||
|
("show.html",show_html),
|
||||||
|
("unauthorized.html", unauthorized_html),
|
||||||
|
// form elements
|
||||||
|
("form_elements/checkbox.html", checkbox_html),
|
||||||
|
("form_elements/input.html", input_html),
|
||||||
|
("form_elements/selectlist.html", selectlist_html)
|
||||||
|
]);
|
||||||
|
|
||||||
tera
|
tera
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_html_input_class<S: BuildHasher>(value: &tera::Value, _: &HashMap<String, tera::Value, S>) -> Result<tera::Value> {
|
pub fn get_html_input_class<S: BuildHasher>(
|
||||||
let field = try_get_value!("get_html_input_class", "value", ActixAdminViewModelField, value);
|
value: &tera::Value,
|
||||||
|
_: &HashMap<String, tera::Value, S>,
|
||||||
|
) -> Result<tera::Value> {
|
||||||
|
let field = try_get_value!(
|
||||||
|
"get_html_input_class",
|
||||||
|
"value",
|
||||||
|
ActixAdminViewModelField,
|
||||||
|
value
|
||||||
|
);
|
||||||
let html_input_type = match field.field_type {
|
let html_input_type = match field.field_type {
|
||||||
ActixAdminViewModelFieldType::TextArea => "textarea",
|
ActixAdminViewModelFieldType::TextArea => "textarea",
|
||||||
ActixAdminViewModelFieldType::Checkbox => "checkbox",
|
ActixAdminViewModelFieldType::Checkbox => "checkbox",
|
||||||
_ => "input"
|
_ => "input",
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(to_value(html_input_type).unwrap())
|
Ok(to_value(html_input_type).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_icon<S: BuildHasher>(value: &tera::Value, _: &HashMap<String, tera::Value, S>) -> Result<tera::Value> {
|
pub fn get_icon<S: BuildHasher>(
|
||||||
|
value: &tera::Value,
|
||||||
|
_: &HashMap<String, tera::Value, S>,
|
||||||
|
) -> Result<tera::Value> {
|
||||||
let field = try_get_value!("get_icon", "value", String, value);
|
let field = try_get_value!("get_icon", "value", String, value);
|
||||||
let font_awesome_icon = match field.as_str() {
|
let font_awesome_icon = match field.as_str() {
|
||||||
"true" => "<i class=\"fa-solid fa-check\"></i>",
|
"true" => "<i class=\"fa-solid fa-check\"></i>",
|
||||||
"false" => "<i class=\"fa-solid fa-xmark\"></i>",
|
"false" => "<i class=\"fa-solid fa-xmark\"></i>",
|
||||||
_ => panic!("not implemented icon")
|
_ => panic!("not implemented icon"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(to_value(font_awesome_icon).unwrap())
|
Ok(to_value(font_awesome_icon).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_html_input_type<S: BuildHasher>(value: &tera::Value, _: &HashMap<String, tera::Value, S>) -> Result<tera::Value> {
|
pub fn get_html_input_type<S: BuildHasher>(
|
||||||
let field = try_get_value!("get_html_input_type", "value", ActixAdminViewModelField, value);
|
value: &tera::Value,
|
||||||
|
_: &HashMap<String, tera::Value, S>,
|
||||||
|
) -> Result<tera::Value> {
|
||||||
|
let field = try_get_value!(
|
||||||
|
"get_html_input_type",
|
||||||
|
"value",
|
||||||
|
ActixAdminViewModelField,
|
||||||
|
value
|
||||||
|
);
|
||||||
|
|
||||||
// TODO: convert to option
|
// TODO: convert to option
|
||||||
if field.html_input_type != "" {
|
if field.html_input_type != "" {
|
||||||
return Ok(to_value(field.html_input_type).unwrap())
|
return Ok(to_value(field.html_input_type).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let html_input_type = match field.field_type {
|
let html_input_type = match field.field_type {
|
||||||
@ -97,7 +156,7 @@ pub fn get_html_input_type<S: BuildHasher>(value: &tera::Value, _: &HashMap<Stri
|
|||||||
ActixAdminViewModelFieldType::Date => "date",
|
ActixAdminViewModelFieldType::Date => "date",
|
||||||
ActixAdminViewModelFieldType::Checkbox => "checkbox",
|
ActixAdminViewModelFieldType::Checkbox => "checkbox",
|
||||||
ActixAdminViewModelFieldType::FileUpload => "file",
|
ActixAdminViewModelFieldType::FileUpload => "file",
|
||||||
_ => "text"
|
_ => "text",
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(to_value(html_input_type).unwrap())
|
Ok(to_value(html_input_type).unwrap())
|
||||||
@ -112,31 +171,32 @@ pub trait ActixAdminAppDataTrait {
|
|||||||
// SelectListTrait
|
// SelectListTrait
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait ActixAdminSelectListTrait {
|
pub trait ActixAdminSelectListTrait {
|
||||||
async fn get_key_value(db: &DatabaseConnection) -> core::result::Result<Vec<(String, String)>, ActixAdminError>;
|
async fn get_key_value(
|
||||||
|
db: &DatabaseConnection,
|
||||||
|
) -> core::result::Result<Vec<(String, String)>, ActixAdminError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ActixAdminConfiguration {
|
pub struct ActixAdminConfiguration {
|
||||||
pub enable_auth: bool,
|
pub enable_auth: bool,
|
||||||
pub user_is_logged_in: Option<for<'a> fn(&'a Session) -> bool>,
|
pub user_is_logged_in: Option<for<'a> fn(&'a Session) -> bool>,
|
||||||
pub login_link: Option<String>,
|
pub login_link: Option<String>,
|
||||||
pub logout_link: Option<String>,
|
pub logout_link: Option<String>,
|
||||||
pub file_upload_directory: &'static str
|
pub file_upload_directory: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ActixAdmin {
|
pub struct ActixAdmin {
|
||||||
pub entity_names: HashMap<String, Vec<ActixAdminMenuElement>>,
|
pub entity_names: HashMap<String, Vec<ActixAdminMenuElement>>,
|
||||||
pub view_models: HashMap<String, ActixAdminViewModel>,
|
pub view_models: HashMap<String, ActixAdminViewModel>,
|
||||||
pub configuration: ActixAdminConfiguration
|
pub configuration: ActixAdminConfiguration,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Clone, Serialize)]
|
#[derive(PartialEq, Eq, Clone, Serialize)]
|
||||||
pub struct ActixAdminMenuElement {
|
pub struct ActixAdminMenuElement {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub link: String,
|
pub link: String,
|
||||||
pub is_custom_handler: bool
|
pub is_custom_handler: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errors
|
// Errors
|
||||||
@ -164,11 +224,9 @@ pub enum ActixAdminError {
|
|||||||
DatabaseError,
|
DatabaseError,
|
||||||
|
|
||||||
#[display(fmt = "Entity does not exist")]
|
#[display(fmt = "Entity does not exist")]
|
||||||
EntityDoesNotExistError
|
EntityDoesNotExistError,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
impl error::ResponseError for ActixAdminError {
|
impl error::ResponseError for ActixAdminError {
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn error_response(&self) -> HttpResponse {
|
||||||
HttpResponse::build(self.status_code())
|
HttpResponse::build(self.status_code())
|
||||||
@ -199,14 +257,14 @@ pub enum ActixAdminNotificationType {
|
|||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct ActixAdminNotification {
|
pub struct ActixAdminNotification {
|
||||||
css_class: String,
|
css_class: String,
|
||||||
message: String
|
message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<ActixAdminError> for ActixAdminNotification {
|
impl std::convert::From<ActixAdminError> for ActixAdminNotification {
|
||||||
fn from(e: ActixAdminError) -> ActixAdminNotification {
|
fn from(e: ActixAdminError) -> ActixAdminNotification {
|
||||||
ActixAdminNotification {
|
ActixAdminNotification {
|
||||||
css_class: ActixAdminNotificationType::Danger.to_string(),
|
css_class: ActixAdminNotificationType::Danger.to_string(),
|
||||||
message: e.to_string()
|
message: e.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user