Compare commits

...

10 Commits

Author SHA1 Message Date
67c35a4b4a trim html 2023-07-31 13:16:30 +02:00
4c13fbe0eb Model 2023-07-31 12:19:38 +02:00
72b10d8994 Display validation errors 2023-07-30 22:01:00 +02:00
4653c4dac0 Add missing param 2023-07-29 21:00:03 +02:00
164c1a5b8e Fix feature 2023-07-29 20:52:07 +02:00
60549bd227
Update lib.rs 2023-07-29 20:49:21 +02:00
a7548208c9
Update index.rs 2023-07-29 20:47:55 +02:00
6bc688012c
Update create_or_edit_get.rs 2023-07-29 20:47:21 +02:00
0c5ad2496f
Update create_or_edit_post.rs 2023-07-29 20:46:04 +02:00
a55bea9d34
Update create_or_edit_post.rs 2023-07-28 22:07:08 +02:00
6 changed files with 35 additions and 16 deletions

View File

@ -19,7 +19,7 @@ name = "actix_admin"
path = "src/lib.rs"
[features]
default = []
default = ['enable-tracing']
enable-tracing = ['tracing']
[dependencies]

View File

@ -13,7 +13,7 @@ use actix_web::{
use async_trait::async_trait;
use derive_more::{Display, Error};
use sea_orm::DatabaseConnection;
use serde_derive::{Serialize};
use serde_derive::Serialize;
use tera::Tera;
use std::collections::HashMap;
@ -116,6 +116,8 @@ pub enum ActixAdminError {
impl error::ResponseError for ActixAdminError {
fn error_response(&self) -> HttpResponse {
#[cfg(feature="enable-tracing")]
tracing::debug!("{self:#?}");
HttpResponse::build(self.status_code())
.insert_header(ContentType::html())
.body(self.to_string())
@ -123,6 +125,7 @@ impl error::ResponseError for ActixAdminError {
fn status_code(&self) -> StatusCode {
match *self {
Self::ValidationErrors => StatusCode::BAD_REQUEST,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}

View File

@ -59,6 +59,8 @@ async fn create_or_edit_get<E: ActixAdminViewModelTrait>(session: &Session, req:
model = res;
},
Err(e) => {
#[cfg(feature="enable-tracing")]
tracing::error!("{e}");
errors.push(e);
model = ActixAdminModel::create_empty();
}
@ -69,7 +71,7 @@ async fn create_or_edit_get<E: ActixAdminViewModelTrait>(session: &Session, req:
false => HttpResponse::InternalServerError(),
};
let notifications: Vec<ActixAdminNotification> = errors.into_iter()
.map(|err| ActixAdminNotification::from(err))
.map(ActixAdminNotification::from)
.collect();
let params = web::Query::<Params>::from_query(req.query_string()).unwrap();
@ -97,6 +99,10 @@ async fn create_or_edit_get<E: ActixAdminViewModelTrait>(session: &Session, req:
let body = actix_admin.tera
.render("create_or_edit.html", &ctx)
.map_err(|err| error::ErrorInternalServerError(err))?;
.map_err(|err| {
#[cfg(feature="enable-tracing")]
tracing::error!("{err}");
error::ErrorInternalServerError(err)
})?;
Ok(http_response_code.content_type("text/html").body(body))
}
}

View File

@ -82,10 +82,19 @@ pub async fn create_or_edit_post<E: ActixAdminViewModelTrait>(
let db = db.get_ref();
let mut model = model_res.unwrap();
#[cfg(feature="enable-tracing")]
{
tracing::debug!("Entity model: {:#?}", model);
}
E::validate_entity(&mut model);
if model.has_errors() {
errors.push(ActixAdminError::ValidationErrors);
#[cfg(feature="enable-tracing")]
{
tracing::error!("OP errors: {errors:#?}");
tracing::debug!("Model errors: {:#?}", model.errors);
}
render_form::<E>(
req,
actix_admin,
@ -125,7 +134,7 @@ pub async fn create_or_edit_post<E: ActixAdminViewModelTrait>(
.finish())
}
Err(e) => {
#[cfg(enable_tracing)]
#[cfg(feature="enable-tracing")]
tracing::error!("{e}");
errors.push(e);
render_form::<E>(
@ -175,6 +184,7 @@ async fn render_form<E: ActixAdminViewModelTrait>(
ctx.insert("sort_order", &sort_order);
ctx.insert("page", &page);
ctx.insert("navbar_title", &actix_admin.configuration.navbar_title);
ctx.insert("entity_names", &actix_admin.entity_names);
ctx.insert(
"view_model",
@ -187,8 +197,8 @@ async fn render_form<E: ActixAdminViewModelTrait>(
let notifications: Vec<ActixAdminNotification> = errors
.into_iter()
.map(|err| {
#[cfg(enable_tracing)]
tracing::error!("{e}");
#[cfg(feature="enable-tracing")]
tracing::error!("{err}");
ActixAdminNotification::from(err)
})
.collect();
@ -197,8 +207,8 @@ async fn render_form<E: ActixAdminViewModelTrait>(
let body = actix_admin.tera
.render("create_or_edit.html", &ctx)
.map_err(|err| {
#[cfg(enable_tracing)]
tracing::error!("{e}");
#[cfg(feature="enable-tracing")]
tracing::error!("{err}");
error::ErrorInternalServerError(err)
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(body))

View File

@ -30,8 +30,8 @@ pub async fn index(session: Session, data: web::Data<ActixAdmin>) -> Result<Http
let body = actix_admin.tera
.render("index.html", &ctx)
.map_err(|e| {
#[cfg(enable_tracing)]
tracing::warn!("{}", e);
#[cfg(feature="enable-tracing")]
tracing::error!("{}", e);
error::ErrorInternalServerError("Template error")
})?;
Ok(HttpResponse::Ok().content_type("text/html").body(body))
@ -41,8 +41,8 @@ pub async fn not_found(data: web::Data<ActixAdmin>) -> Result<HttpResponse, Erro
let body = data.get_ref().tera
.render("not_found.html", &Context::new())
.map_err(|e| {
#[cfg(enable_tracing)]
tracing::warn!("{}", e);
#[cfg(feature="enable-tracing")]
tracing::error!("{}", e);
error::ErrorInternalServerError("Template error")
})?;
Ok(HttpResponse::NotFound().content_type("text/html").body(body))

View File

@ -7,7 +7,7 @@
model.custom_errors | get(key=model_field.field_name, default="" ) !=""
%}is-danger{% else %}is-success{% endif %}
{% endif %}
" type="{{ model_field | get_html_input_type }}" name="{{ model_field.field_name }}"
" type="{{ model_field | get_html_input_type }}" name="{{ model_field.field_name | trim }}"
placeholder="{{ model_field.field_name }}"
aria-label="{{ model_field.field_name }}">{{ model.values | get(key=model_field.field_name, default="") }}</textarea>
{% elif model_field.field_type == "FileUpload" and model.values | get(key=model_field.field_name, default="") != "" %}
@ -29,4 +29,4 @@
" type="{{ model_field | get_html_input_type }}"
value="{{ model.values | get(key=model_field.field_name, default="") }}" name="{{ model_field.field_name }}"
placeholder="{{ model_field.field_name }}" aria-label="{{ model_field.field_name }}">
{% endif %}
{% endif %}