Use Ructe (#327)
All the template are now compiled at compile-time with the `ructe` crate.
I preferred to use it instead of askama because it allows more complex Rust expressions, where askama only supports a small subset of expressions and doesn't allow them everywhere (for instance, `{{ macro!() | filter }}` would result in a parsing error).
The diff is quite huge, but there is normally no changes in functionality.
Fixes #161 and unblocks #110 and #273
This commit is contained in:
+20
-22
@@ -1,31 +1,29 @@
|
||||
use guid_create::GUID;
|
||||
use multipart::server::{Multipart, save::{SavedData, SaveResult}};
|
||||
use rocket::{Data, http::ContentType, response::{Redirect, status}};
|
||||
use rocket_contrib::Template;
|
||||
use serde_json;
|
||||
use rocket_i18n::I18n;
|
||||
use std::fs;
|
||||
use plume_models::{db_conn::DbConn, medias::*, users::User};
|
||||
use template_utils::Ructe;
|
||||
|
||||
#[get("/medias")]
|
||||
fn list(user: User, conn: DbConn) -> Template {
|
||||
pub fn list(user: User, conn: DbConn, intl: I18n) -> Ructe {
|
||||
let medias = Media::for_user(&*conn, user.id);
|
||||
Template::render("medias/index", json!({
|
||||
"account": user.to_json(&*conn),
|
||||
"medias": medias.into_iter().map(|m| m.to_json(&*conn)).collect::<Vec<serde_json::Value>>()
|
||||
}))
|
||||
render!(medias::index(
|
||||
&(&*conn, &intl.catalog, Some(user)),
|
||||
medias
|
||||
))
|
||||
}
|
||||
|
||||
#[get("/medias/new")]
|
||||
fn new(user: User, conn: DbConn) -> Template {
|
||||
Template::render("medias/new", json!({
|
||||
"account": user.to_json(&*conn),
|
||||
"form": {},
|
||||
"errors": {}
|
||||
}))
|
||||
pub fn new(user: User, conn: DbConn, intl: I18n) -> Ructe {
|
||||
render!(medias::new(
|
||||
&(&*conn, &intl.catalog, Some(user))
|
||||
))
|
||||
}
|
||||
|
||||
#[post("/medias/new", data = "<data>")]
|
||||
fn upload(user: User, data: Data, ct: &ContentType, conn: DbConn) -> Result<Redirect, status::BadRequest<&'static str>> {
|
||||
pub fn upload(user: User, data: Data, ct: &ContentType, conn: DbConn) -> Result<Redirect, status::BadRequest<&'static str>> {
|
||||
if ct.is_form_data() {
|
||||
let (_, boundary) = ct.params().find(|&(k, _)| k == "boundary").ok_or_else(|| status::BadRequest(Some("No boundary")))?;
|
||||
|
||||
@@ -86,23 +84,23 @@ fn read(data: &SavedData) -> String {
|
||||
}
|
||||
|
||||
#[get("/medias/<id>")]
|
||||
fn details(id: i32, user: User, conn: DbConn) -> Template {
|
||||
let media = Media::get(&*conn, id);
|
||||
Template::render("medias/details", json!({
|
||||
"account": user.to_json(&*conn),
|
||||
"media": media.map(|m| m.to_json(&*conn))
|
||||
}))
|
||||
pub fn details(id: i32, user: User, conn: DbConn, intl: I18n) -> Ructe {
|
||||
let media = Media::get(&*conn, id).expect("Media::details: media not found");
|
||||
render!(medias::details(
|
||||
&(&*conn, &intl.catalog, Some(user)),
|
||||
media
|
||||
))
|
||||
}
|
||||
|
||||
#[post("/medias/<id>/delete")]
|
||||
fn delete(id: i32, _user: User, conn: DbConn) -> Option<Redirect> {
|
||||
pub fn delete(id: i32, _user: User, conn: DbConn) -> Option<Redirect> {
|
||||
let media = Media::get(&*conn, id)?;
|
||||
media.delete(&*conn);
|
||||
Some(Redirect::to(uri!(list)))
|
||||
}
|
||||
|
||||
#[post("/medias/<id>/avatar")]
|
||||
fn set_avatar(id: i32, user: User, conn: DbConn) -> Option<Redirect> {
|
||||
pub fn set_avatar(id: i32, user: User, conn: DbConn) -> Option<Redirect> {
|
||||
let media = Media::get(&*conn, id)?;
|
||||
user.set_avatar(&*conn, media.id);
|
||||
Some(Redirect::to(uri!(details: id = id)))
|
||||
|
||||
Reference in New Issue
Block a user