From c645bf83a3e0f66a1c86cf4c499e6159b66cf653 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 24 Jan 2023 19:25:23 +0530 Subject: [PATCH] feat: port join page to tera --- src/pages/auth/join.rs | 100 +++++++++++++++++--------------- src/pages/auth/mod.rs | 22 +++++-- templates/auth/login/index.html | 91 +++++++++++++++-------------- 3 files changed, 115 insertions(+), 98 deletions(-) diff --git a/src/pages/auth/join.rs b/src/pages/auth/join.rs index 2e04734..21baf06 100644 --- a/src/pages/auth/join.rs +++ b/src/pages/auth/join.rs @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Aravinth Manivannan + * Copyright (C) 2022 Aravinth Manivannan * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -14,76 +14,81 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -use actix_web::HttpResponseBuilder; -use actix_web::{error::ResponseError, http::header, web, HttpResponse, Responder}; -use lazy_static::lazy_static; -use sailfish::TemplateOnce; +use actix_web::http::header::ContentType; +use std::cell::RefCell; +use tera::Context; + +use crate::pages::errors::*; +use crate::settings::Settings; + +use actix_web::{http::header, web, HttpResponse, Responder}; use crate::api::v1::admin::auth::runners; -use crate::errors::*; -use crate::pages::errors::ErrorPage; use crate::AppData; use crate::PAGES; -#[derive(Clone, TemplateOnce)] -#[template(path = "auth/join/index.html")] -struct IndexPage<'a> { - error: Option>, +pub use super::*; + +pub const REGISTER: TemplateFile = TemplateFile::new("register", "auth/join/index.html"); + +pub struct Register { + ctx: RefCell, } -const PAGE: &str = "Join"; - -impl<'a> Default for IndexPage<'a> { - fn default() -> Self { - IndexPage { error: None } +impl CtxError for Register { + fn with_error(&self, e: &ReadableError) -> String { + self.ctx.borrow_mut().insert(ERROR_KEY, e); + self.render() } } -impl<'a> IndexPage<'a> { - pub fn new(title: &'a str, message: &'a str) -> Self { - Self { - error: Some(ErrorPage::new(title, message)), - } +impl Register { + fn new(settings: &Settings) -> Self { + let ctx = RefCell::new(context(settings, "Join")); + Self { ctx } + } + + pub fn render(&self) -> String { + TEMPLATES.render(REGISTER.name, &self.ctx.borrow()).unwrap() + } + + pub fn page(s: &Settings) -> String { + let p = Self::new(s); + p.render() } } -lazy_static! { - static ref INDEX: String = IndexPage::default().render_once().unwrap(); +#[actix_web_codegen_const_routes::get(path = "PAGES.auth.join")] +#[tracing::instrument(name = "Serve registration page", skip(ctx))] +pub async fn get_join(ctx: AppData) -> impl Responder { + let login = Register::page(&ctx.settings); + let html = ContentType::html(); + HttpResponse::Ok().content_type(html).body(login) } -#[my_codegen::get(path = "crate::PAGES.auth.join")] -pub async fn join() -> impl Responder { - HttpResponse::Ok() - .content_type("text/html; charset=utf-8") - .body(&*INDEX.as_str()) +pub fn services(cfg: &mut web::ServiceConfig) { + cfg.service(get_join); + cfg.service(join_submit); } -#[my_codegen::post(path = "PAGES.auth.join")] +#[actix_web_codegen_const_routes::post(path = "PAGES.auth.join")] +#[tracing::instrument(name = "Process web UI registration", skip(data))] pub async fn join_submit( payload: web::Form, data: AppData, -) -> PageResult { +) -> PageResult { let mut payload = payload.into_inner(); if payload.email.is_some() && payload.email.as_ref().unwrap().is_empty() { payload.email = None; } - match runners::register_runner(&payload, &data).await { - Ok(()) => Ok(HttpResponse::Found() - .insert_header((header::LOCATION, PAGES.auth.login)) - .finish()), - Err(e) => { - let status = e.status_code(); - let heading = status.canonical_reason().unwrap_or("Error"); - Ok(HttpResponseBuilder::new(status) - .content_type("text/html; charset=utf-8") - .body( - IndexPage::new(heading, &format!("{}", e)) - .render_once() - .unwrap(), - )) - } - } + runners::register_runner(&payload, &data) + .await + .map_err(|e| PageError::new(Register::new(&data.settings), e))?; + + Ok(HttpResponse::Found() + .insert_header((header::LOCATION, PAGES.auth.login)) + .finish()) } #[cfg(test)] @@ -103,7 +108,8 @@ mod tests { #[actix_rt::test] async fn auth_join_form_works() { - let data = Data::new().await; + let settings = Settings::new().unwrap(); + let data = Data::new(settings).await; const NAME: &str = "testuserformjoin"; const NAME2: &str = "testuserformjoin2"; const EMAIL: &str = "testuserformjoin@a.com"; diff --git a/src/pages/auth/mod.rs b/src/pages/auth/mod.rs index b37e2f7..1a4b53b 100644 --- a/src/pages/auth/mod.rs +++ b/src/pages/auth/mod.rs @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Aravinth Manivannan + * Copyright (C) 2023 Aravinth Manivannan * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -14,21 +14,31 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +use actix_web::*; + +pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES}; + +pub fn register_templates(t: &mut tera::Tera) { + for template in [login::LOGIN, join::REGISTER].iter() { + template.register(t).expect(template.name); + } +} + pub mod join; pub mod login; -pub mod sudo; +//pub mod sudo; pub fn services(cfg: &mut actix_web::web::ServiceConfig) { - cfg.service(login::login); - cfg.service(login::login_submit); - cfg.service(join::join); - cfg.service(join::join_submit); + login::services(cfg); + join::services(cfg); } pub mod routes { use actix_auth_middleware::GetLoginRoute; + use serde::{Deserialize, Serialize}; use url::Url; + #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct Auth { pub login: &'static str, pub join: &'static str, diff --git a/templates/auth/login/index.html b/templates/auth/login/index.html index 69dc951..8252046 100644 --- a/templates/auth/login/index.html +++ b/templates/auth/login/index.html @@ -1,48 +1,49 @@ -<. include!("../../components/base/top.html"); .> - -
- -

Sign in

- <. include!("../../components/error/index.html"); .> -
- +{% extends 'base' %} - +{% block body %} + +
+ +

Sign in

+ {% include "error_comp" %} + + -
- Forgot password? - -
- + -

- New to mCaptcha Survey? - Create an account -

-
-<. include!("../../components/footer/index.html"); .> - -<. include!("../../components/base/bottom.html"); .> +
+ Forgot password? + +
+ + +

+ New to mCaptcha Survey? + Create an account +

+
+ +{% endblock body %}