move tera template into cfg feature to allow for other templates

This commit is contained in:
Manuel Gugger 2023-08-24 17:46:30 +02:00
parent defe9cdd90
commit f82b331f98
17 changed files with 185 additions and 134 deletions

View File

@ -18,6 +18,10 @@ exclude = [
name = "actix_admin" name = "actix_admin"
path = "src/lib.rs" path = "src/lib.rs"
[features]
default = ["bulma_css"]
bulma_css = []
[dependencies] [dependencies]
actix-web = "^4.3.1" actix-web = "^4.3.1"
actix-session = { version = "^0.7.2", features = [] } actix-session = { version = "^0.7.2", features = [] }

View File

@ -1,13 +1,10 @@
use crate::{prelude::*, ActixAdminMenuElement, routes::delete_file}; use crate::{prelude::*, ActixAdminMenuElement, routes::delete_file};
use actix_web::{web, Route }; use actix_web::{web, Route };
use tera::Tera;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use crate::routes::{ use crate::routes::{
create_get, create_post, delete, delete_many, edit_get, edit_post, index, list, not_found, show, download create_get, create_post, delete, delete_many, edit_get, edit_post, index, list, not_found, show, download
}; };
use std::hash::BuildHasher;
use tera::{to_value, try_get_value, Result};
/// Represents a builder entity which helps generating the ActixAdmin configuration /// Represents a builder entity which helps generating the ActixAdmin configuration
pub struct ActixAdminBuilder { pub struct ActixAdminBuilder {
@ -70,133 +67,6 @@ pub trait ActixAdminBuilderTrait {
fn get_actix_admin(&self) -> ActixAdmin; fn get_actix_admin(&self) -> ActixAdmin;
} }
fn get_html_input_class<S: BuildHasher>(
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 {
ActixAdminViewModelFieldType::TextArea => "textarea",
ActixAdminViewModelFieldType::Checkbox => "checkbox",
_ => "input",
};
Ok(to_value(html_input_type).unwrap())
}
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 font_awesome_icon = match field.as_str() {
"true" => "<i class=\"fa-solid fa-check\"></i>",
"false" => "<i class=\"fa-solid fa-xmark\"></i>",
_ => panic!("not implemented icon"),
};
Ok(to_value(font_awesome_icon).unwrap())
}
fn get_regex_val<S: BuildHasher>(
value: &tera::Value,
args: &HashMap<String, tera::Value, S>,
) -> Result<tera::Value> {
let field = try_get_value!("get_regex_val", "value", ActixAdminViewModelField, value);
let s = args.get("values");
let field_val = s.unwrap().get(&field.field_name);
println!("field {} regex {:?}", field.field_name, field.list_regex_mask);
match (field_val, field.list_regex_mask) {
(Some(val), Some(r)) => {
let val_str = val.to_string();
let is_match = r.is_match(&val_str);
println!("is match: {}, regex {}", is_match, r.to_string());
let result_str = r.replace_all(&val_str, "*");
return Ok(to_value(result_str).unwrap());
},
(Some(val), None) => { return Ok(to_value(val).unwrap()); },
(_, _) => panic!("key {} not found in model values", &field.field_name)
}
}
fn get_html_input_type<S: BuildHasher>(
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
if field.html_input_type != "" {
return Ok(to_value(field.html_input_type).unwrap());
}
let html_input_type = match field.field_type {
ActixAdminViewModelFieldType::Text => "text",
ActixAdminViewModelFieldType::DateTime => "datetime-local",
ActixAdminViewModelFieldType::Date => "date",
ActixAdminViewModelFieldType::Checkbox => "checkbox",
ActixAdminViewModelFieldType::FileUpload => "file",
_ => "text",
};
Ok(to_value(html_input_type).unwrap())
}
fn get_tera() -> Tera {
let mut tera = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src/templates/*.html")).unwrap();
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_icon", get_icon);
tera.register_filter("get_regex_val", get_regex_val);
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
}
impl ActixAdminBuilderTrait for ActixAdminBuilder { impl ActixAdminBuilderTrait for ActixAdminBuilder {
fn new(configuration: ActixAdminConfiguration) -> Self { fn new(configuration: ActixAdminConfiguration) -> Self {
ActixAdminBuilder { ActixAdminBuilder {
@ -204,7 +74,7 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder {
entity_names: HashMap::new(), entity_names: HashMap::new(),
view_models: HashMap::new(), view_models: HashMap::new(),
configuration: configuration, configuration: configuration,
tera: get_tera() tera: crate::tera_templates::get_tera()
}, },
custom_routes: Vec::new(), custom_routes: Vec::new(),
scopes: HashMap::new(), scopes: HashMap::new(),

View File

@ -13,7 +13,7 @@ use actix_web::{
use async_trait::async_trait; use async_trait::async_trait;
use derive_more::{Display, Error}; use derive_more::{Display, Error};
use sea_orm::DatabaseConnection; use sea_orm::DatabaseConnection;
use serde_derive::{Serialize}; use serde_derive::Serialize;
use tera::Tera; use tera::Tera;
use std::collections::HashMap; use std::collections::HashMap;
@ -21,6 +21,7 @@ pub mod builder;
pub mod model; pub mod model;
pub mod routes; pub mod routes;
pub mod view_model; pub mod view_model;
pub mod tera_templates;
pub mod prelude { pub mod prelude {
pub use crate::builder::{ActixAdminBuilder, ActixAdminBuilderTrait}; pub use crate::builder::{ActixAdminBuilder, ActixAdminBuilderTrait};

View File

@ -3,8 +3,8 @@
<title>{{ navbar_title }}</title> <title>{{ navbar_title }}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
<script src="https://unpkg.com/htmx.org@1.9.2"></script> <script src="https://unpkg.com/htmx.org@1.9.4"></script>
<script> <script>
document.onkeydown = function (e) { document.onkeydown = function (e) {

176
src/tera_templates.rs Normal file
View File

@ -0,0 +1,176 @@
use std::{collections::HashMap, hash::BuildHasher};
use tera::Tera;
use tera::{to_value, try_get_value, Result};
use crate::view_model::{ActixAdminViewModelField, ActixAdminViewModelFieldType};
struct TeraTemplate {
// Pages
list_html: &'static str,
create_or_edit_html: &'static str,
base_html: &'static str,
head_html: &'static str,
index_html: &'static str,
loader_html: &'static str,
navbar_html: &'static str,
not_found_html: &'static str,
show_html: &'static str,
unauthorized_html: &'static str,
// Form Elements
checkbox_html: &'static str,
input_html: &'static str,
selectlist_html: &'static str,
}
pub fn get_tera() -> Tera {
let mut tera = load_templates();
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_icon", get_icon);
tera.register_filter("get_regex_val", get_regex_val);
tera
}
fn get_html_input_class<S: BuildHasher>(
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 {
ActixAdminViewModelFieldType::TextArea => "textarea",
ActixAdminViewModelFieldType::Checkbox => "checkbox",
_ => "input",
};
Ok(to_value(html_input_type).unwrap())
}
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 font_awesome_icon = match field.as_str() {
"true" => "<i class=\"fa-solid fa-check\"></i>",
"false" => "<i class=\"fa-solid fa-xmark\"></i>",
_ => panic!("not implemented icon"),
};
Ok(to_value(font_awesome_icon).unwrap())
}
fn get_regex_val<S: BuildHasher>(
value: &tera::Value,
args: &HashMap<String, tera::Value, S>,
) -> Result<tera::Value> {
let field = try_get_value!("get_regex_val", "value", ActixAdminViewModelField, value);
let s = args.get("values");
let field_val = s.unwrap().get(&field.field_name);
println!(
"field {} regex {:?}",
field.field_name, field.list_regex_mask
);
match (field_val, field.list_regex_mask) {
(Some(val), Some(r)) => {
let val_str = val.to_string();
let is_match = r.is_match(&val_str);
println!("is match: {}, regex {}", is_match, r.to_string());
let result_str = r.replace_all(&val_str, "*");
return Ok(to_value(result_str).unwrap());
}
(Some(val), None) => {
return Ok(to_value(val).unwrap());
}
(_, _) => panic!("key {} not found in model values", &field.field_name),
}
}
fn get_html_input_type<S: BuildHasher>(
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
if field.html_input_type != "" {
return Ok(to_value(field.html_input_type).unwrap());
}
let html_input_type = match field.field_type {
ActixAdminViewModelFieldType::Text => "text",
ActixAdminViewModelFieldType::DateTime => "datetime-local",
ActixAdminViewModelFieldType::Date => "date",
ActixAdminViewModelFieldType::Checkbox => "checkbox",
ActixAdminViewModelFieldType::FileUpload => "file",
_ => "text",
};
Ok(to_value(html_input_type).unwrap())
}
fn add_templates_to_tera(tera: &mut Tera, tera_template: TeraTemplate) {
let _res = tera.add_raw_templates(vec![
("base.html", tera_template.base_html),
("list.html", tera_template.list_html),
("create_or_edit.html", tera_template.create_or_edit_html),
("head.html", tera_template.head_html),
("index.html", tera_template.index_html),
("loader.html", tera_template.loader_html),
("navbar.html", tera_template.navbar_html),
("not_found.html", tera_template.not_found_html),
("show.html", tera_template.show_html),
("unauthorized.html", tera_template.unauthorized_html),
// form elements
("form_elements/checkbox.html", tera_template.checkbox_html),
("form_elements/input.html", tera_template.input_html),
(
"form_elements/selectlist.html",
tera_template.selectlist_html,
),
]);
}
// Cargo Features
#[cfg(feature = "bulma_css")]
fn load_templates() -> Tera {
let mut tera = Tera::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/templates/bulma/*.html"
))
.unwrap();
let tera_template = TeraTemplate {
list_html: include_str!("templates/bulma/list.html"),
create_or_edit_html: include_str!("templates/bulma/create_or_edit.html"),
base_html: include_str!("templates/bulma/base.html"),
head_html: include_str!("templates/bulma/head.html"),
index_html: include_str!("templates/bulma/index.html"),
loader_html: include_str!("templates/bulma/loader.html"),
navbar_html: include_str!("templates/bulma/navbar.html"),
not_found_html: include_str!("templates/bulma/not_found.html"),
show_html: include_str!("templates/bulma/show.html"),
unauthorized_html: include_str!("templates/bulma/unauthorized.html"),
// form elements
checkbox_html: include_str!("templates/bulma/form_elements/checkbox.html"),
input_html: include_str!("templates/bulma/form_elements/input.html"),
selectlist_html: include_str!("templates/bulma/form_elements/selectlist.html"),
};
add_templates_to_tera(&mut tera, tera_template);
tera
}