Askama message template

This commit is contained in:
Alphonse Paix
2025-09-20 00:51:46 +02:00
parent 6ad207d0a4
commit 2b9cf979e8
9 changed files with 29 additions and 33 deletions

View File

@@ -4,7 +4,7 @@ mod logout;
mod newsletters; mod newsletters;
mod posts; mod posts;
use crate::{routes::error_chain_fmt, templates::ErrorTemplate}; use crate::{routes::error_chain_fmt, templates::MessageTemplate};
use askama::Template; use askama::Template;
use axum::{ use axum::{
Json, Json,
@@ -20,7 +20,7 @@ use reqwest::StatusCode;
#[derive(thiserror::Error)] #[derive(thiserror::Error)]
pub enum AdminError { pub enum AdminError {
#[error("Something went wrong.")] #[error("Something went wrong while performing an admin action.")]
UnexpectedError(#[from] anyhow::Error), UnexpectedError(#[from] anyhow::Error),
#[error("Trying to access admin dashboard without authentication.")] #[error("Trying to access admin dashboard without authentication.")]
NotAuthenticated, NotAuthenticated,
@@ -62,14 +62,14 @@ impl IntoResponse for AdminError {
(StatusCode::SEE_OTHER, headers).into_response() (StatusCode::SEE_OTHER, headers).into_response()
} }
AdminError::ChangePassword(e) => { AdminError::ChangePassword(e) => {
let template = ErrorTemplate { let template = MessageTemplate::Error {
error_message: e.to_owned(), message: e.to_owned(),
}; };
Html(template.render().unwrap()).into_response() Html(template.render().unwrap()).into_response()
} }
AdminError::Publish(e) => { AdminError::Publish(e) => {
let template = ErrorTemplate { let template = MessageTemplate::Error {
error_message: e.to_string(), message: e.to_string(),
}; };
Html(template.render().unwrap()).into_response() Html(template.render().unwrap()).into_response()
} }

View File

@@ -2,7 +2,7 @@ use crate::{
authentication::{self, AuthenticatedUser, Credentials, validate_credentials}, authentication::{self, AuthenticatedUser, Credentials, validate_credentials},
routes::AdminError, routes::AdminError,
startup::AppState, startup::AppState,
templates::SuccessTemplate, templates::MessageTemplate,
}; };
use askama::Template; use askama::Template;
use axum::{ use axum::{
@@ -47,8 +47,8 @@ pub async fn change_password(
authentication::change_password(user_id, form.new_password, &connection_pool) authentication::change_password(user_id, form.new_password, &connection_pool)
.await .await
.map_err(|e| AdminError::ChangePassword(e.to_string()))?; .map_err(|e| AdminError::ChangePassword(e.to_string()))?;
let template = SuccessTemplate { let template = MessageTemplate::Success {
success_message: "Your password has been changed.".to_string(), message: "Your password has been changed.".to_string(),
}; };
Ok(Html(template.render().unwrap()).into_response()) Ok(Html(template.render().unwrap()).into_response())
} }

View File

@@ -3,7 +3,7 @@ use crate::{
idempotency::{IdempotencyKey, save_response, try_processing}, idempotency::{IdempotencyKey, save_response, try_processing},
routes::AdminError, routes::AdminError,
startup::AppState, startup::AppState,
templates::SuccessTemplate, templates::MessageTemplate,
}; };
use anyhow::Context; use anyhow::Context;
use askama::Template; use askama::Template;
@@ -98,11 +98,11 @@ pub async fn publish_newsletter(
.await .await
.context("Failed to enqueue delivery tasks.")?; .context("Failed to enqueue delivery tasks.")?;
let success_message = format!( let message = format!(
r#"The newsletter issue "{}" has been published!"#, r#"The newsletter issue "{}" has been published!"#,
form.title form.title
); );
let template = SuccessTemplate { success_message }; let template = MessageTemplate::Success { message };
let response = Html(template.render().unwrap()).into_response(); let response = Html(template.render().unwrap()).into_response();
save_response(transaction, &idempotency_key, user_id, response) save_response(transaction, &idempotency_key, user_id, response)
.await .await

View File

@@ -3,7 +3,7 @@ use crate::{
idempotency::{IdempotencyKey, save_response, try_processing}, idempotency::{IdempotencyKey, save_response, try_processing},
routes::{AdminError, enqueue_delivery_tasks, insert_newsletter_issue}, routes::{AdminError, enqueue_delivery_tasks, insert_newsletter_issue},
startup::AppState, startup::AppState,
templates::SuccessTemplate, templates::MessageTemplate,
}; };
use anyhow::Context; use anyhow::Context;
use askama::Template; use askama::Template;
@@ -68,8 +68,8 @@ pub async fn create_post(
// Send emails with unique identifiers that contains link to blog post with special param // Send emails with unique identifiers that contains link to blog post with special param
// Get handpoint that returns the post and mark the email as opened // Get handpoint that returns the post and mark the email as opened
let template = SuccessTemplate { let template = MessageTemplate::Success {
success_message: "Your new post has been saved. Subscribers will be notified.".into(), message: "Your new post has been saved. Subscribers will be notified.".into(),
}; };
let response = Html(template.render().unwrap()).into_response(); let response = Html(template.render().unwrap()).into_response();
save_response(transaction, &idempotency_key, user_id, response) save_response(transaction, &idempotency_key, user_id, response)

View File

@@ -3,7 +3,7 @@ use crate::{
routes::error_chain_fmt, routes::error_chain_fmt,
session_state::TypedSession, session_state::TypedSession,
startup::AppState, startup::AppState,
templates::ErrorTemplate, templates::MessageTemplate,
}; };
use askama::Template; use askama::Template;
use axum::{ use axum::{
@@ -47,8 +47,8 @@ impl IntoResponse for LoginError {
) )
.into_response(), .into_response(),
LoginError::AuthError(e) => { LoginError::AuthError(e) => {
let template = ErrorTemplate { let template = MessageTemplate::Error {
error_message: e.to_string(), message: e.to_string(),
}; };
Html(template.render().unwrap()).into_response() Html(template.render().unwrap()).into_response()
} }

View File

@@ -2,7 +2,7 @@ use crate::{
domain::{NewSubscriber, SubscriberEmail}, domain::{NewSubscriber, SubscriberEmail},
email_client::EmailClient, email_client::EmailClient,
startup::AppState, startup::AppState,
templates::{ErrorTemplate, SuccessTemplate}, templates::MessageTemplate,
}; };
use anyhow::Context; use anyhow::Context;
use askama::Template; use askama::Template;
@@ -74,7 +74,7 @@ impl IntoResponse for SubscribeError {
) )
.into_response(), .into_response(),
SubscribeError::ValidationError(e) => { SubscribeError::ValidationError(e) => {
let template = ErrorTemplate { error_message: e }; let template = MessageTemplate::Error { message: e };
Html(template.render().unwrap()).into_response() Html(template.render().unwrap()).into_response()
} }
} }
@@ -126,8 +126,8 @@ pub async fn subscribe(
.commit() .commit()
.await .await
.context("Failed to commit the database transaction to store a new subscriber.")?; .context("Failed to commit the database transaction to store a new subscriber.")?;
let template = SuccessTemplate { let template = MessageTemplate::Success {
success_message: "A confirmation email has been sent.".to_string(), message: "A confirmation email has been sent.".to_string(),
}; };
Ok(Html(template.render().unwrap()).into_response()) Ok(Html(template.render().unwrap()).into_response())
} }

View File

@@ -1,13 +1,9 @@
use askama::Template; use askama::Template;
#[derive(Template)] #[derive(Template)]
pub enum MessageTemplate {
#[template(path = "../templates/success.html")] #[template(path = "../templates/success.html")]
pub struct SuccessTemplate { Success { message: String },
pub success_message: String,
}
#[derive(Template)]
#[template(path = "../templates/error.html")] #[template(path = "../templates/error.html")]
pub struct ErrorTemplate { Error { message: String },
pub error_message: String,
} }

View File

@@ -4,6 +4,6 @@
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd"> <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd">
</path> </path>
</svg> </svg>
<span class="font-medium">{{ error_message }}</span> <span class="font-medium">{{ message }}</span>
</div> </div>
</div> </div>

View File

@@ -4,6 +4,6 @@
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"> <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd">
</path> </path>
</svg> </svg>
<span class="font-medium">{{ success_message }}</span> <span class="font-medium">{{ message }}</span>
</div> </div>
</div> </div>