From 58f8cb14b88e63b529e3b0cb12f0bc1b7ba1f0ac Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 5 Mar 2024 08:59:44 +0100 Subject: [PATCH 1/2] allow admins to show logbook for any year --- src/model/logbook.rs | 4 ++++ src/tera/log.rs | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/model/logbook.rs b/src/model/logbook.rs index e8c911d..a9a70ce 100644 --- a/src/model/logbook.rs +++ b/src/model/logbook.rs @@ -260,6 +260,10 @@ ORDER BY departure DESC pub async fn completed(db: &SqlitePool) -> Vec { let year = chrono::Local::now().year(); + Self::completed_in_year(db, year).await + } + + pub async fn completed_in_year(db: &SqlitePool, year: i32) -> Vec { let logs = sqlx::query_as( &format!(" SELECT id, boat_id, shipmaster, steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype diff --git a/src/tera/log.rs b/src/tera/log.rs index 2c47e71..e2dca51 100644 --- a/src/tera/log.rs +++ b/src/tera/log.rs @@ -23,7 +23,7 @@ use crate::model::{ LogbookUpdateError, }, logtype::LogType, - user::{DonauLinzUser, User, UserWithRoles, UserWithWaterStatus}, + user::{AdminUser, DonauLinzUser, User, UserWithRoles, UserWithWaterStatus}, }; pub struct KioskCookie(String); @@ -96,6 +96,16 @@ async fn show(db: &State, user: DonauLinzUser) -> Template { ) } +#[get("/show?")] +async fn show_for_year(db: &State, user: AdminUser, year: i32) -> Template { + let logs = Logbook::completed_in_year(db, year).await; + + Template::render( + "log.completed", + context!(logs, loggedin_user: &UserWithRoles::from_user(user.user, db).await), + ) +} + #[get("/show")] async fn show_kiosk(db: &State, _kiosk: KioskCookie) -> Template { let logs = Logbook::completed(db).await; @@ -378,6 +388,7 @@ pub fn routes() -> Vec { new_kiosk, show, show_kiosk, + show_for_year, delete, delete_kiosk ] From 8b0cbe23d14cbf081a239594ed7224d98043e5a8 Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 5 Mar 2024 09:19:15 +0100 Subject: [PATCH 2/2] show year selector to admins for logbook --- src/model/user.rs | 2 +- templates/log.completed.html.tera | 39 ++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/model/user.rs b/src/model/user.rs index 00d66a6..9ae4a3d 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -753,7 +753,7 @@ impl<'r> FromRequest<'r> for AdminUser { if user.has_role(db, "admin").await { Outcome::Success(AdminUser { user }) } else { - Outcome::Error((Status::Forbidden, LoginError::NotACox)) + Outcome::Forward(Status::Forbidden) } } Outcome::Error(f) => Outcome::Error(f), diff --git a/templates/log.completed.html.tera b/templates/log.completed.html.tera index 2568f5d..1edaefa 100644 --- a/templates/log.completed.html.tera +++ b/templates/log.completed.html.tera @@ -3,7 +3,16 @@ {% extends "base" %} {% block content %}
-

Logbuch

+

+ Logbuch + {% if "admin" in loggedin_user.roles %} + + {% endif %} +

@@ -17,4 +26,32 @@ {% for log in logs %}{{ log::show_old(log=log, state="completed", only_ones=false, index=loop.index) }}{% endfor %}
+ {% endblock content %}