175 lines
3.7 KiB
Rust
175 lines
3.7 KiB
Rust
use askama::Template;
|
|
|
|
use crate::{
|
|
app::Context,
|
|
data::{db, model},
|
|
log::Log,
|
|
translation::{self, Sentence},
|
|
};
|
|
|
|
pub struct Recipes {
|
|
public: Vec<(i64, String)>,
|
|
private: Vec<(i64, String)>,
|
|
current_id: Option<i64>,
|
|
}
|
|
|
|
impl Recipes {
|
|
pub async fn new(
|
|
connection: db::Connection,
|
|
user: &Option<model::User>,
|
|
lang: &str,
|
|
current_id: Option<i64>,
|
|
) -> Result<Self, db::DBError> {
|
|
Ok(Recipes {
|
|
public: connection
|
|
.get_all_public_recipe_titles(lang, user.as_ref().map(|u| u.id))
|
|
.await?,
|
|
private: if let Some(user) = user {
|
|
connection.get_all_private_recipe_titles(user.id).await?
|
|
} else {
|
|
vec![]
|
|
},
|
|
current_id,
|
|
})
|
|
}
|
|
|
|
pub fn is_current(&self, id: &&i64) -> bool {
|
|
self.current_id == Some(**id)
|
|
}
|
|
}
|
|
|
|
mod filters {
|
|
use askama::filters::Safe;
|
|
pub fn markdown<T: std::fmt::Display>(
|
|
s: T,
|
|
_: &dyn askama::Values,
|
|
) -> askama::Result<Safe<String>> {
|
|
Ok(Safe(comrak::markdown_to_html(
|
|
&s.to_string(),
|
|
&comrak::ComrakOptions::default(),
|
|
)))
|
|
}
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "home.html")]
|
|
pub struct HomeTemplate {
|
|
pub context: Context,
|
|
pub recipes: Recipes,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "dev_panel.html")]
|
|
pub struct DevPanelTemplate {
|
|
pub context: Context,
|
|
pub recipes: Recipes,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "logs.html")]
|
|
pub struct LogsTemplate {
|
|
pub context: Context,
|
|
pub recipes: Recipes,
|
|
pub log: Log,
|
|
pub current_log_file: String,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "message.html")]
|
|
pub struct MessageTemplate<'a> {
|
|
pub context: Context,
|
|
|
|
pub message: &'a str,
|
|
pub as_code: bool, // Display the message in <pre> markup.
|
|
}
|
|
|
|
impl<'a> MessageTemplate<'a> {
|
|
pub fn new(message: &'a str, context: Context) -> MessageTemplate<'a> {
|
|
MessageTemplate {
|
|
context,
|
|
message,
|
|
as_code: false,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "sign_up_form.html")]
|
|
pub struct SignUpFormTemplate<'a> {
|
|
pub context: Context,
|
|
|
|
pub email: String,
|
|
pub message: &'a str,
|
|
pub message_email: &'a str,
|
|
pub message_password: &'a str,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "sign_in_form.html")]
|
|
pub struct SignInFormTemplate<'a> {
|
|
pub context: Context,
|
|
|
|
pub email: &'a str,
|
|
pub message: &'a str,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "ask_reset_password.html")]
|
|
pub struct AskResetPasswordTemplate<'a> {
|
|
pub context: Context,
|
|
|
|
pub email: &'a str,
|
|
pub message: &'a str,
|
|
pub message_email: &'a str,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "reset_password.html")]
|
|
pub struct ResetPasswordTemplate<'a> {
|
|
pub context: Context,
|
|
|
|
pub reset_token: &'a str,
|
|
pub message: &'a str,
|
|
pub message_password: &'a str,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "profile.html")]
|
|
pub struct ProfileTemplate<'a> {
|
|
pub context: Context,
|
|
|
|
pub username: &'a str,
|
|
pub email: &'a str,
|
|
pub default_servings: u32,
|
|
pub message: &'a str,
|
|
pub message_email: &'a str,
|
|
pub message_password: &'a str,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "recipe_view.html")]
|
|
pub struct RecipeViewTemplate {
|
|
pub context: Context,
|
|
|
|
pub recipes: Recipes,
|
|
|
|
pub recipe: model::Recipe,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "recipe_edit.html")]
|
|
pub struct RecipeEditTemplate {
|
|
pub context: Context,
|
|
|
|
pub recipes: Recipes,
|
|
|
|
pub recipe: model::Recipe,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "recipes_list_fragment.html")]
|
|
pub struct RecipesListFragmentTemplate {
|
|
pub context: Context,
|
|
|
|
pub recipes: Recipes,
|
|
}
|