Rebase to master

This commit is contained in:
Adrian Woźniak 2023-09-14 15:34:34 +02:00
parent c473e35566
commit c2fc9cda9b

View File

@ -1,15 +1,8 @@
use crate::{prelude::*, ActixAdminMenuElement, routes::delete_file};
use crate::{prelude::*, ActixAdminMenuElement};
use actix_web::{web, Route };
use std::collections::HashMap;
use std::fs;
use crate::routes::{
create_get, create_post, delete, delete_many, download, edit_get, edit_post, index, list,
not_found, show,
};
use crate::{prelude::*, routes::delete_file, ActixAdminMenuElement};
use std::hash::BuildHasher;
use tera::Tera;
use tera::{to_value, try_get_value, Result};
use crate::routes::*;
/// Represents a builder entity which helps generating the ActixAdmin configuration
pub struct ActixAdminBuilder {
@ -66,147 +59,13 @@ pub trait ActixAdminBuilderTrait {
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
}
>>>>>>> 7db2971 (Trim input name)
impl ActixAdminBuilderTrait for ActixAdminBuilder {
fn new(configuration: ActixAdminConfiguration) -> Self {
ActixAdminBuilder {
actix_admin: ActixAdmin {
entity_names: HashMap::new(),
view_models: HashMap::new(),
tera: crate::tera_templates::get_tera()
tera: crate::tera_templates::get_tera(),
configuration,
},
custom_routes: Vec::new(),