add show view

This commit is contained in:
Manuel Gugger 2022-09-06 10:21:07 +02:00
parent c95bef4d68
commit 879dfff032
6 changed files with 75 additions and 8 deletions

View File

@ -16,5 +16,5 @@ serde = "1.0.136"
serde_derive = "1.0.136"
sea-orm = { version = "^0.9.1", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = true }
actix_admin = { path = "../" }
actix-admin = { path = "../" }
azure_auth = { path = "./azure_auth" }

View File

@ -3,7 +3,7 @@ use std::collections::HashMap;
use crate::prelude::*;
use crate::routes::{create_get, create_post, delete, delete_many, edit_get, edit_post, index, list};
use crate::routes::{create_get, create_post, delete, delete_many, edit_get, edit_post, index, list, show};
/// Represents a builder entity which helps generating the ActixAdmin configuration
pub struct ActixAdminBuilder {
@ -59,6 +59,7 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder {
.route("/edit/{id}", web::post().to(edit_post::<T, E>))
.route("/delete", web::delete().to(delete_many::<T, E>))
.route("/delete/{id}", web::delete().to(delete::<T, E>))
.route("/show/{id}", web::get().to(show::<T, E>))
);
self.actix_admin.entity_names.push(E::get_entity_name());

View File

@ -10,6 +10,9 @@ pub use index::{ index, get_admin_ctx };
mod list;
pub use list::list;
mod show;
pub use show::show;
mod delete;
pub use delete::{ delete, delete_many };

30
src/routes/show.rs Normal file
View File

@ -0,0 +1,30 @@
use actix_web::{error, web, Error, HttpResponse};
use actix_session::{Session};
use tera::{Context};
use crate::prelude::*;
use crate::TERA;
use super::{ add_auth_context };
pub async fn show<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(session: Session, data: web::Data<T>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
let actix_admin = data.get_actix_admin();
let db = &data.get_db();
let model = E::get_entity(db, id.into_inner()).await;
let entity_name = E::get_entity_name();
let view_model: &ActixAdminViewModel = actix_admin.view_models.get(&entity_name).unwrap();
let mut ctx = Context::new();
ctx.insert("model", &model);
ctx.insert("view_model", &ActixAdminViewModelSerializable::from(view_model.clone()));
ctx.insert("list_link", &E::get_list_link(&entity_name));
ctx.insert("entity_names", &actix_admin.entity_names);
add_auth_context(&session, actix_admin, &mut ctx);
let body = TERA
.render("show.html", &ctx)
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}

View File

@ -86,13 +86,17 @@
{% for entity in entities -%}
<tr>
<td><input type="checkbox" name="ids" value="{{ entity.primary_key }}"></td>
<td>{{ entity.primary_key }}</td>
<td>
<a href="show/{{ entity.primary_key }}">
<i class="fa-solid fa-magnifying-glass"></i> {{ entity.primary_key }}
</a>
</td>
{% for model_field in view_model.fields -%}
{% if model_field.field_type == "Checkbox" %}
<td>{{ entity.values | get(key=model_field.field_name) | get_icon | safe }}</td>
{% else %}
<td>{{ entity.values | get(key=model_field.field_name) }}</td>
{% endif %}
{% if model_field.field_type == "Checkbox" %}
<td>{{ entity.values | get(key=model_field.field_name) | get_icon | safe }}</td>
{% else %}
<td>{{ entity.values | get(key=model_field.field_name) }}</td>
{% endif %}
{%- endfor %}
<td>
<a href="edit/{{ entity.primary_key }}"><i class="fa-solid fa-pen-to-square"></i></a>

29
templates/show.html Normal file
View File

@ -0,0 +1,29 @@
{% extends "base.html" %}
{% block content %}
{% for model_field in view_model.fields -%}
<div class="columns">
<div class="column">
<p class="title is-5">{{ model_field.field_name | split(pat="_") | join(sep=" ") | title }}</p>
<p>
{% if model_field.field_type == "Checkbox" %}
<td>{{ model.values | get(key=model_field.field_name) | get_icon | safe }}</td>
{% else %}
<td>{{ model.values | get(key=model_field.field_name) }}</td>
{% endif %}
</p>
</div>
</div>
{%- endfor %}
<div class="columns">
<div class="column">
<div class="field is-grouped">
<div class="control">
<a class="button is-link is-light" href="{{ list_link }}">Back</a>
</div>
</div>
</div>
</div>
{% endblock content %}