More ructe templates.

This commit is contained in:
Rasmus Kaj 2016-09-23 16:40:54 +02:00
parent 9a038765b9
commit b79cbc7714
6 changed files with 86 additions and 92 deletions

View File

@ -38,7 +38,6 @@ use diesel::expression::sql_literal::SqlLiteral;
use diesel::prelude::*; use diesel::prelude::*;
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use chrono::naive::date::NaiveDate; use chrono::naive::date::NaiveDate;
use std::str::from_utf8;
use rphotos::models::{Camera, Person, Photo, Place, Tag}; use rphotos::models::{Camera, Person, Photo, Place, Tag};
@ -513,10 +512,7 @@ fn all_years<'mw>(req: &mut Request,
}).collect(); }).collect();
let mut stream = try!(res.start()); let mut stream = try!(res.start());
// TODO Use a proper sub-template match templates::groups(&mut stream, "All photos", Vec::new(), user, groups) {
let mut headvec = Vec::new();
templates::head(&mut headvec, Vec::new()).unwrap();
match templates::groups(&mut stream, "All photos", Html(from_utf8(&headvec).unwrap()), groups) {
Ok(()) => Ok(Halt(stream)), Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e)) Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
} }
@ -566,10 +562,7 @@ fn months_in_year<'mw>(req: &mut Request,
}).collect(); }).collect();
let mut stream = try!(res.start()); let mut stream = try!(res.start());
// TODO Use a proper sub-template match templates::groups(&mut stream, &title, Vec::new(), user, groups) {
let mut headvec = Vec::new();
templates::head(&mut headvec, Vec::new()).unwrap();
match templates::groups(&mut stream, &title, Html(from_utf8(&headvec).unwrap()), groups) {
Ok(()) => Ok(Halt(stream)), Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e)) Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
} }
@ -583,11 +576,10 @@ fn days_in_month<'mw>(req: &mut Request,
use rphotos::schema::photos::dsl::{date, grade}; use rphotos::schema::photos::dsl::{date, grade};
let c: &PgConnection = &req.db_conn(); let c: &PgConnection = &req.db_conn();
render!(res, "templates/groups.tpl", { let user: Option<String> = req.authorized_user();
user: Option<String> = req.authorized_user(), let lpath: Vec<Link> = vec![Link::year(year)];
lpath: Vec<Link> = vec![Link::year(year)], let title: String = format!("Photos from {} {}", monthname(month), year);
title: String = format!("Photos from {} {}", monthname(month), year), let groups: Vec<Group> =
groups: Vec<Group> =
SqlLiteral::new(format!( SqlLiteral::new(format!(
"select cast(extract(day from date) as int) d, count(*) c \ "select cast(extract(day from date) as int) d, count(*) c \
from photos where extract(year from date)={} \ from photos where extract(year from date)={} \
@ -616,8 +608,13 @@ fn days_in_month<'mw>(req: &mut Request,
count: count, count: count,
photo: photo photo: photo
} }
}).collect() }).collect();
})
let mut stream = try!(res.start());
match templates::groups(&mut stream, &title, lpath, user, groups) {
Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
}
} }
fn all_null_date<'mw>(req: &mut Request, fn all_null_date<'mw>(req: &mut Request,
@ -626,16 +623,19 @@ fn all_null_date<'mw>(req: &mut Request,
use rphotos::schema::photos::dsl::{date, path}; use rphotos::schema::photos::dsl::{date, path};
let c: &PgConnection = &req.db_conn(); let c: &PgConnection = &req.db_conn();
render!(res, "templates/index.tpl", { let mut stream = try!(res.start());
user: Option<String> = req.authorized_user(), match templates::index(&mut stream,
lpath: Vec<Link> = vec![], &"Photos without a date",
title: &'static str = "Photos without a date", vec![],
photos: Vec<Photo> = Photo::query(req.authorized_user().is_some()) req.authorized_user(),
Photo::query(req.authorized_user().is_some())
.filter(date.is_null()) .filter(date.is_null())
.order(path.asc()) .order(path.asc())
.limit(500) .limit(500)
.load(c).unwrap() .load(c).unwrap()) {
}) Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
}
} }
fn all_for_day<'mw>(req: &mut Request, fn all_for_day<'mw>(req: &mut Request,
@ -648,19 +648,24 @@ fn all_for_day<'mw>(req: &mut Request,
use rphotos::schema::photos::dsl::{date, grade}; use rphotos::schema::photos::dsl::{date, grade};
let c: &PgConnection = &req.db_conn(); let c: &PgConnection = &req.db_conn();
render!(res, "templates/index.tpl", {
user: Option<String> = req.authorized_user(), let user: Option<String> = req.authorized_user();
lpath: Vec<Link> = vec![Link::year(year), let lpath: Vec<Link> = vec![Link::year(year),
Link::month(year, month)], Link::month(year, month)];
title: String = format!("Photos from {} {} {}", let title: String = format!("Photos from {} {} {}",
day, monthname(month), year), day, monthname(month), year);
photos: Vec<Photo> = Photo::query(req.authorized_user().is_some()) let photos: Vec<Photo> = Photo::query(req.authorized_user().is_some())
.filter(date.ge(thedate)) .filter(date.ge(thedate))
.filter(date.lt(thedate + ChDuration::days(1))) .filter(date.lt(thedate + ChDuration::days(1)))
.order((grade.desc(), date.asc())) .order((grade.desc(), date.asc()))
.limit(500) .limit(500)
.load(c).unwrap() .load(c).unwrap();
})
let mut stream = try!(res.start());
match templates::index(&mut stream, &title, lpath, user, photos) {
Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
}
} }
fn on_this_day<'mw>(req: &mut Request, fn on_this_day<'mw>(req: &mut Request,
@ -673,10 +678,11 @@ fn on_this_day<'mw>(req: &mut Request,
let now = time::now(); let now = time::now();
(now.tm_mon as u32 + 1, now.tm_mday as u32) (now.tm_mon as u32 + 1, now.tm_mday as u32)
}; };
render!(res, "templates/groups.tpl", { let mut stream = try!(res.start());
user: Option<String> = req.authorized_user(), match templates::groups(&mut stream,
title: String = format!("Photos from {} {}", day, monthname(month)), &format!("Photos from {} {}", day, monthname(month)),
groups: Vec<Group> = vec![],
req.authorized_user(),
SqlLiteral::new(format!( SqlLiteral::new(format!(
"select extract(year from date) y, count(*) c \ "select extract(year from date) y, count(*) c \
from photos where extract(month from date)={} \ from photos where extract(month from date)={} \
@ -706,7 +712,10 @@ fn on_this_day<'mw>(req: &mut Request,
photo: photo photo: photo
} }
}).collect() }).collect()
}) ) {
Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
}
} }
#[derive(Debug, Clone, RustcEncodable)] #[derive(Debug, Clone, RustcEncodable)]

View File

@ -1,6 +1,7 @@
@use ::{CSSLINK, Group}; @use ::{CSSLINK, Group, Link};
@use templates::head;
@(title: &str, head: Html<&str>, groups: Vec<Group>) @(title: &str, lpath: Vec<Link>, user: Option<String>, groups: Vec<Group>)
<!doctype html> <!doctype html>
<html> <html>
@ -10,7 +11,7 @@
@CSSLINK @CSSLINK
</head> </head>
<body> <body>
@head @:head(lpath, user)
<h1>@title</h1> <h1>@title</h1>
<div class="group"> <div class="group">

View File

@ -1,24 +0,0 @@
<!doctype html>
<html>
<head>
<title>{{title}}</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
{{{csslink}}}
</head>
<body>
{{> head}}
<h1>{{title}}</h1>
<div class="group">
{{#groups}}
<div class="item"><h2>{{title}}</h2>
<p><a href="{{url}}"><img src="/img/{{photo.id}}/s"></a></p>
<p>{{count}} pictures</p>
</div>
{{/groups}}
{{^groups}}
<p>Inga bilder.</p>
{{/groups}}
</div>
</body>
</html>

View File

@ -1,6 +1,6 @@
@use ::Link; @use ::Link;
@(lpath: Vec<Link>) @(lpath: Vec<Link>, user: Option<String>)
<header> <header>
<span><a href="/">Bilder</a> <span><a href="/">Bilder</a>
@ -10,9 +10,6 @@
<span>· <a href="/person/">Personer</a></span> <span>· <a href="/person/">Personer</a></span>
<span>· <a href="/place/">Platser</a></span> <span>· <a href="/place/">Platser</a></span>
<span>· <a href="/thisday">Denna dag</a></span> <span>· <a href="/thisday">Denna dag</a></span>
@* {{#user}}<span class="user">{{.}} @if let Some(u) = user {<span class="user">@u (<a href="/logout">log out</a>)</span>}
(<a href="/logout">log out</a>) else {<span class="user">(<a href="/login">log in</a>)</span>}
</span>{{/user}}
{{^user}}<span class="user">(<a href="/login">log in</a>)</span>{{/user}}
*@
</header> </header>

29
templates/index.rs.html Normal file
View File

@ -0,0 +1,29 @@
@use ::{CSSLINK, Link};
@use rphotos::models::Photo;
@use templates::head;
@(title: &str, lpath: Vec<Link>, user: Option<String>, photos: Vec<Photo>)
<!doctype html>
<html>
<head>
<title>@title</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
@CSSLINK
</head>
<body>
@:head(lpath, user)
<h1>@title</h1>
<div class="group">
@for p in photos {
<p class="item"><a href="/details/@p.id"><img src="/img/@p.id/s"></a></p>
}
@*
{{^photos}}
<p>Inga bilder.</p>
{{/photos}}
*@
</div>
</body>
</html>

View File

@ -1,18 +0,0 @@
<!doctype html>
<html>
<head>
<title>{{title}}</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
{{{csslink}}}
</head>
<body>
{{> head}}
<h1>{{title}}</h1>
<div class="group">
{{#photos}}
<p class="item"><a href="/details/{{id}}"><img src="/img/{{id}}/s"></a></p>
{{/photos}}
</div>
</body>
</html>