From dcde768fb607144067b5e22d99ccbfaf72449bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Sun, 25 Aug 2024 09:18:06 +0200 Subject: [PATCH] wip: further development --- rear/src/depot/constants.rs | 5 ++++ rear/src/depot/mod.rs | 5 +++- rear/src/depot/state.rs | 2 ++ rear/src/depot/views.rs | 31 ++++++++------------ rear/src/lib.rs | 1 + rear_auth/src/user_admin_repository.rs | 2 +- templates/depot/items/item_change_form.jinja | 4 +-- 7 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 rear/src/depot/constants.rs diff --git a/rear/src/depot/constants.rs b/rear/src/depot/constants.rs new file mode 100644 index 0000000..887da16 --- /dev/null +++ b/rear/src/depot/constants.rs @@ -0,0 +1,5 @@ +pub(crate) struct TemplateFiles { + base_hx: &'static str, +} + +pub static TPL: TemplateFiles = TemplateFiles { base_hx: "" }; diff --git a/rear/src/depot/mod.rs b/rear/src/depot/mod.rs index 9bfc512..8ad20be 100644 --- a/rear/src/depot/mod.rs +++ b/rear/src/depot/mod.rs @@ -1,5 +1,6 @@ use axum::{routing::get, Router}; +mod constants; mod context; pub mod prelude; mod registry; @@ -8,7 +9,9 @@ pub mod state; pub mod views; pub mod widgets; -pub fn routes() -> Router { +use state::DepotFn; + +pub fn routes() -> Router { Router::new() .route("/", get(views::index::).post(views::index_action::)) .route("/:section", get(views::list_section::)) diff --git a/rear/src/depot/state.rs b/rear/src/depot/state.rs index ead12c9..b40915b 100644 --- a/rear/src/depot/state.rs +++ b/rear/src/depot/state.rs @@ -9,3 +9,5 @@ pub trait DepotState { } pub type SharedDepotRegistry = Arc; + +pub trait DepotFn = DepotState + Clone + Send + Sync + 'static; diff --git a/rear/src/depot/views.rs b/rear/src/depot/views.rs index f77996e..39b28e7 100644 --- a/rear/src/depot/views.rs +++ b/rear/src/depot/views.rs @@ -6,7 +6,7 @@ use log::info; use serde_json::Value; use super::context::DepotContext; -use super::state::DepotState; +use super::state::{DepotFn, DepotState}; pub fn base_template(headers: &HeaderMap) -> Option { let hx_request = headers.get("HX-Request").is_some(); @@ -17,10 +17,7 @@ pub fn base_template(headers: &HeaderMap) -> Option { } } -pub async fn index( - depot: State, - headers: HeaderMap, -) -> impl IntoResponse { +pub async fn index(depot: State, headers: HeaderMap) -> impl IntoResponse { let templates = depot.get_templates(); let registry = depot.get_registry(); templates.render_html( @@ -34,13 +31,11 @@ pub async fn index( } // Index Action is POST to the index site. We can anchor some general business code here. -pub async fn index_action( - depot: State, -) -> impl IntoResponse { +pub async fn index_action(depot: State) -> impl IntoResponse { "There is your answer!".to_owned() } -pub async fn list_section( +pub async fn list_section( depot: State, Path(depot_key): Path, ) -> impl IntoResponse { @@ -49,7 +44,7 @@ pub async fn list_section( } // List Items renders the entire list item page. -pub async fn list_item_collection( +pub async fn list_item_collection( depot: State, headers: HeaderMap, Path((depot_key, model_key)): Path<(String, String)>, @@ -81,7 +76,7 @@ pub async fn list_item_collection } // Items Action is a POST to an item list. By default these are actions, that work on a list of items as input. -pub async fn item_collection_action( +pub async fn item_collection_action( depot: State, Path((depot_key, model_key)): Path<(String, String)>, ) -> impl IntoResponse { @@ -89,7 +84,7 @@ pub async fn item_collection_action( +pub async fn view_item_details( depot: State, headers: HeaderMap, Path((depot_key, model_key, id)): Path<(String, String, String)>, @@ -128,7 +123,7 @@ pub async fn view_item_details( templates.render_html("depot/items/item_detail.jinja", context) } -pub async fn new_item( +pub async fn new_item( depot: State, headers: HeaderMap, Path((depot_key, model_key)): Path<(String, String)>, @@ -158,7 +153,7 @@ pub async fn new_item( templates.render_html("depot/items/item_create.jinja", context) } -pub async fn create_item( +pub async fn create_item( depot: State, headers: HeaderMap, Path((depot_key, model_key)): Path<(String, String)>, @@ -196,7 +191,7 @@ pub async fn create_item( } /// Change is the GET version. -pub async fn change_item( +pub async fn change_item( depot: State, headers: HeaderMap, Path((depot_key, model_key, id)): Path<(String, String, String)>, @@ -237,7 +232,7 @@ pub async fn change_item( templates.render_html("depot/items/item_change.jinja", context) } -pub async fn update_item( +pub async fn update_item( depot: State, headers: HeaderMap, Path((depot_key, model_key, id)): Path<(String, String, String)>, @@ -280,14 +275,14 @@ pub async fn update_item( } // Item Action allows running an action on one single dataset. -pub async fn item_action( +pub async fn item_action( depot: State, Path((depot_key, model_key, model_id)): Path<(String, String, String)>, ) -> impl IntoResponse { "There is your answer!".to_owned() } -pub async fn debug_view( +pub async fn debug_view( depot: State, Path(data): Path, ) -> impl IntoResponse { diff --git a/rear/src/lib.rs b/rear/src/lib.rs index e6ebc3e..99902ba 100644 --- a/rear/src/lib.rs +++ b/rear/src/lib.rs @@ -1,2 +1,3 @@ +#![feature(trait_alias)] pub mod depot; pub mod service; diff --git a/rear_auth/src/user_admin_repository.rs b/rear_auth/src/user_admin_repository.rs index bf427f7..f7a2767 100644 --- a/rear_auth/src/user_admin_repository.rs +++ b/rear_auth/src/user_admin_repository.rs @@ -38,7 +38,7 @@ impl DepotRepository for UserRepository { .build() .set_widget( "password", - Widget::widget("/admin/widgets/password_change.jinja").as_password(), + Widget::widget("/depot/widgets/password_change.jinja").as_password(), ) .set_widget("is_staff", Widget::checkbox()) .set_widget("is_active", Widget::checkbox()) diff --git a/templates/depot/items/item_change_form.jinja b/templates/depot/items/item_change_form.jinja index 2e95485..926f805 100644 --- a/templates/depot/items/item_change_form.jinja +++ b/templates/depot/items/item_change_form.jinja @@ -16,9 +16,9 @@ {% from "/depot/items/items.jinja" import field_widget %} {% for field_name, field_defs in fields %} {% if item %} - {% set field_value = item.fields[field_name]|none("") %} + {% set field_value = item.fields[field_name]|none("") %} {% else %} - {% set field_value = "" %} + {% set field_value = "" %} {% endif %}
{{ field_widget(field_name, field_defs, field_value) }}