Compare commits

...

3 Commits

Author SHA1 Message Date
1f18ba54d3 Rename 2023-10-11 11:20:50 +02:00
4799f92961 Insert defaul with 1 query 2023-10-11 11:20:24 +02:00
f27ed3e90e Rename 2023-10-11 11:20:24 +02:00
8 changed files with 93 additions and 46 deletions

View File

@ -9,8 +9,8 @@ use oswilno_contract::sea_orm_active_enums::ParkingSpaceState;
use oswilno_contract::{accounts, parking_space_locations};
use oswilno_session::{Authenticated, MaybeAuthenticated};
use oswilno_view::{
filters, is_partial, Blank, Errors, Lang, Layout, Main, MainOpts, SearchOpts, SessionOpts,
TranslationStorage,
filters, is_partial, Blank, Errors, HelperContext, Lang, Layout, Main, MainOpts, SearchOpts,
SessionOpts, TranslationStorage,
};
use sea_orm::prelude::*;
use sea_orm::ActiveValue::{NotSet, Set};
@ -67,6 +67,7 @@ struct AllPartialParkingSpace {
location_by_id: BTreeMap<i32, Rc<parking_space_locations::Model>>,
session: Option<SessionOpts>,
account_id: Option<i32>,
hcx: HelperContext,
}
#[autometrics]
@ -75,6 +76,7 @@ async fn all_parking_spaces(
req: HttpRequest,
db: Data<sea_orm::DatabaseConnection>,
session: MaybeAuthenticated,
hcx: HelperContext,
) -> HttpResponse {
let db = db.into_inner();
let session = session.into_option();
@ -83,7 +85,7 @@ async fn all_parking_spaces(
tracing::debug!("session {session:?}");
let mut parking_spaces = load_parking_spaces(db, account_id).await;
let mut parking_spaces = load_parking_spaces(db, account_id, htx).await;
parking_spaces.session = session.clone();
let main = Main {
body: parking_spaces,
@ -117,6 +119,7 @@ async fn search_parking_spaces() -> HttpResponse {
async fn load_parking_spaces(
db: Arc<DatabaseConnection>,
account_id: Option<i32>,
hcx: HelperContext,
) -> AllPartialParkingSpace {
let rents = parking_space_rents::Entity::find().all(&*db).await.unwrap();
let ids = rents
@ -180,6 +183,7 @@ async fn load_parking_spaces(
locations,
location_by_id,
session: None,
hcx,
}
}
@ -199,8 +203,7 @@ async fn load_locations(db: Arc<DatabaseConnection>) -> Vec<parking_space_locati
async fn form_show(
req: HttpRequest,
session: Authenticated,
t: Data<TranslationStorage>,
lang: Lang,
hcx: HelperContext,
db: Data<DatabaseConnection>,
) -> HttpResponse {
let db = db.into_inner();
@ -208,9 +211,8 @@ async fn form_show(
let body = ParkingSpaceFormPartial {
form: Default::default(),
locations: load_locations(db.clone()).await,
lang,
errors: Default::default(),
t: t.into_inner(),
hcx,
};
let main = Main {
body,
@ -235,10 +237,9 @@ async fn form_show(
#[template(path = "../templates/parking-spaces/form-partial.html")]
struct ParkingSpaceFormPartial {
form: CreateParkingSpace,
lang: Lang,
errors: Errors,
t: Arc<TranslationStorage>,
locations: Vec<parking_space_locations::Model>,
hcx: HelperContext,
}
#[derive(Debug, Default, serde::Deserialize)]
@ -254,8 +255,7 @@ async fn create(
db: Data<sea_orm::DatabaseConnection>,
p: Form<CreateParkingSpace>,
session: Authenticated,
t: Data<TranslationStorage>,
lang: Lang,
hcx: HelperContext,
) -> HttpResponse {
use oswilno_contract::parking_spaces::*;
let CreateParkingSpace {
@ -280,8 +280,7 @@ async fn create(
spot,
id: None,
},
t: t.into_inner(),
lang,
hcx,
errors: Default::default(),
locations: load_locations(db.clone()).await,
}

View File

@ -1,14 +1,14 @@
{% match parking_space.state %}
{% when ParkingSpaceState::Pending %}
<div class="text-center p-1 text-sm text-yellow-400 bg-transparent rounded-sm hover:bg-yellow-200 hover:text-yellow-900 dark:hover:bg-yellow-800 dark:hover:text-yellow-300">
Pending
{{"Pending"|t(lang,t)}}
</div>
{% when ParkingSpaceState::Verified %}
<div class="text-center p-1 text-sm text-blue-400 bg-transparent rounded-sm hover:bg-blue-200 hover:text-blue-900 dark:hover:bg-blue-800 dark:hover:text-blue-300">
Accepted
{{"Accepted"|t(lang,t)}}
</div>
{% when ParkingSpaceState::Banned %}
<div class="text-center p-1 text-sm text-red-400 bg-transparent rounded-sm hover:bg-red-200 hover:text-red-900 dark:hover:bg-red-800 dark:hover:text-red-300">
Rejected
{{"Rejected"|t(lang,t)}}
</div>
{% endmatch %}

View File

@ -7,7 +7,9 @@ use actix_web::{get, post, HttpRequest, HttpResponse};
use askama_actix::Template;
use autometrics::autometrics;
use garde::Validate;
use oswilno_view::{Blank, Errors, Lang, Layout, Main, MainOpts, TranslationStorage};
use oswilno_view::{
Blank, Errors, HelperContext, Lang, Layout, Main, MainOpts, TranslationStorage,
};
use sea_orm::DatabaseConnection;
use serde::{Deserialize, Serialize};
@ -148,8 +150,7 @@ impl SessionConfigurator {
#[template(path = "./sign-in/partial.html")]
struct SignInPartialTemplate {
form: SignInPayload,
lang: Lang,
t: Arc<TranslationStorage>,
hcx: HelperContext,
errors: Errors,
}
@ -160,15 +161,14 @@ pub struct SignInPayload {
}
#[get("/login")]
async fn login_view(req: HttpRequest, t: Data<TranslationStorage>) -> HttpResponse {
async fn login_view(req: HttpRequest, hcx: HelperContext) -> HttpResponse {
HttpResponse::Ok().body(
if oswilno_view::is_partial(&req) {
Main {
body: SignInPartialTemplate {
form: SignInPayload::default(),
lang: Lang::Pl,
t: t.into_inner(),
errors: Errors::default(),
hcx,
},
opts: MainOpts {
show: true,
@ -183,9 +183,8 @@ async fn login_view(req: HttpRequest, t: Data<TranslationStorage>) -> HttpRespon
main: Main {
body: SignInPartialTemplate {
form: SignInPayload::default(),
lang: Lang::Pl,
t: t.into_inner(),
errors: Errors::default(),
hcx,
},
opts: MainOpts {
show: true,
@ -208,10 +207,8 @@ async fn login(
db: Data<DatabaseConnection>,
redis: Data<SessionStorage>,
payload: Form<SignInPayload>,
t: Data<oswilno_view::TranslationStorage>,
lang: Lang,
hcx: HelperContext,
) -> Result<HttpResponse, Error> {
let t = t.into_inner();
let mut errors = Errors::default();
let form = payload.into_inner();
match login_inner(
@ -226,14 +223,9 @@ async fn login(
{
Some(res) => Ok(res),
None => Ok(HttpResponse::Ok().body(
(SignInPartialTemplate {
form,
lang,
t,
errors,
})
.render()
.unwrap(),
(SignInPartialTemplate { form, errors, hcx })
.render()
.unwrap(),
)),
}
}
@ -352,6 +344,7 @@ async fn find_account(
async fn refresh_token(
refresh_token: actix_jwt_session::Authenticated<RefreshToken>,
storage: Data<SessionStorage>,
_hcx: HelperContext,
) -> HttpResponse {
let s = storage.into_inner();
let pair = match s.refresh::<Claims>(refresh_token.access_jti()).await {

View File

@ -1,15 +1,10 @@
use crate::{Lang, TranslationStorage};
use crate::HelperContext;
use askama::Result;
use std::sync::Arc;
#[tracing::instrument]
pub fn t<T: std::fmt::Display + std::fmt::Debug>(
s: T,
lang: &Lang,
t: &Arc<TranslationStorage>,
) -> Result<String> {
#[tracing::instrument(skip(htx))]
pub fn t<T: std::fmt::Display + std::fmt::Debug>(s: T, htx: &HelperContext) -> Result<String> {
// tracing::debug!("translating {s:?} to lang {lang:?} with {t:?}");
Ok(t.to_lang(*lang, &s.to_string()))
Ok(htx.t.to_lang(htx.lang, &s.to_string()))
}
pub fn skip_if<T: std::fmt::Display + PartialEq + Copy>(n: &T, skip: T) -> Result<String> {

View File

@ -0,0 +1,57 @@
use crate::Lang;
use crate::{ExtractLangFuture, TranslationStorage};
use actix_web::error::Error;
use actix_web::web::Data;
use actix_web::HttpRequest;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
#[derive(Clone, Debug)]
pub struct HelperContext {
pub t: Data<TranslationStorage>,
pub lang: Lang,
}
impl actix_web::FromRequest for HelperContext {
type Error = actix_web::error::Error;
type Future = ExtractFuture;
fn from_request(
req: &actix_web::HttpRequest,
_payload: &mut actix_web::dev::Payload,
) -> Self::Future {
ExtractFuture {
req: Some(req.clone()),
}
}
}
pub struct ExtractFuture {
req: Option<HttpRequest>,
}
impl Future for ExtractFuture {
type Output = Result<HelperContext, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let lang = Pin::new(&mut ExtractLangFuture {
req: this.req.clone(),
})
.poll(cx);
let lang = match lang {
Poll::Ready(Ok(lang)) => lang,
_ => panic!("{lang:?}"),
};
let t = this
.req
.as_ref()
.unwrap()
.app_data::<Data<TranslationStorage>>()
.unwrap()
.clone();
Poll::Ready(Ok(HelperContext { t, lang }))
}
}

View File

@ -24,7 +24,7 @@ impl FromStr for Lang {
}
pub struct ExtractLangFuture {
req: Option<HttpRequest>,
pub req: Option<HttpRequest>,
}
impl Future for ExtractLangFuture {

View File

@ -4,9 +4,12 @@ use std::sync::{Arc, RwLock};
use actix_web::http::header::ContentType;
use actix_web::web::ServiceConfig;
use actix_web::{get, HttpRequest, HttpResponse};
pub use helper_context::*;
pub use lang::*;
pub mod filters;
pub mod helper_context;
pub mod lang;
pub fn mount(config: &mut ServiceConfig) {

View File

@ -21,7 +21,7 @@
hx-target="main"
class="block py-2 pl-3 pr-4 text-gray-900 rounded hover:bg-gray-100 md:hover:bg-transparent md:hover:text-blue-700 md:p-0 dark:text-white md:dark:hover:text-blue-500 dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent dark:border-gray-700"
>
About
Targowisko
</a>
</li>
<li>