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::pg::PgConnection;
use chrono::naive::date::NaiveDate;
use std::str::from_utf8;
use rphotos::models::{Camera, Person, Photo, Place, Tag};
@ -513,10 +512,7 @@ fn all_years<'mw>(req: &mut Request,
}).collect();
let mut stream = try!(res.start());
// TODO Use a proper sub-template
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) {
match templates::groups(&mut stream, "All photos", Vec::new(), user, groups) {
Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
}
@ -566,10 +562,7 @@ fn months_in_year<'mw>(req: &mut Request,
}).collect();
let mut stream = try!(res.start());
// TODO Use a proper sub-template
let mut headvec = Vec::new();
templates::head(&mut headvec, Vec::new()).unwrap();
match templates::groups(&mut stream, &title, Html(from_utf8(&headvec).unwrap()), groups) {
match templates::groups(&mut stream, &title, Vec::new(), user, groups) {
Ok(()) => Ok(Halt(stream)),
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};
let c: &PgConnection = &req.db_conn();
render!(res, "templates/groups.tpl", {
user: Option<String> = req.authorized_user(),
lpath: Vec<Link> = vec![Link::year(year)],
title: String = format!("Photos from {} {}", monthname(month), year),
groups: Vec<Group> =
let user: Option<String> = req.authorized_user();
let lpath: Vec<Link> = vec![Link::year(year)];
let title: String = format!("Photos from {} {}", monthname(month), year);
let groups: Vec<Group> =
SqlLiteral::new(format!(
"select cast(extract(day from date) as int) d, count(*) c \
from photos where extract(year from date)={} \
@ -616,8 +608,13 @@ fn days_in_month<'mw>(req: &mut Request,
count: count,
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,
@ -626,16 +623,19 @@ fn all_null_date<'mw>(req: &mut Request,
use rphotos::schema::photos::dsl::{date, path};
let c: &PgConnection = &req.db_conn();
render!(res, "templates/index.tpl", {
user: Option<String> = req.authorized_user(),
lpath: Vec<Link> = vec![],
title: &'static str = "Photos without a date",
photos: Vec<Photo> = Photo::query(req.authorized_user().is_some())
.filter(date.is_null())
.order(path.asc())
.limit(500)
.load(c).unwrap()
})
let mut stream = try!(res.start());
match templates::index(&mut stream,
&"Photos without a date",
vec![],
req.authorized_user(),
Photo::query(req.authorized_user().is_some())
.filter(date.is_null())
.order(path.asc())
.limit(500)
.load(c).unwrap()) {
Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
}
}
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};
let c: &PgConnection = &req.db_conn();
render!(res, "templates/index.tpl", {
user: Option<String> = req.authorized_user(),
lpath: Vec<Link> = vec![Link::year(year),
Link::month(year, month)],
title: String = format!("Photos from {} {} {}",
day, monthname(month), year),
photos: Vec<Photo> = Photo::query(req.authorized_user().is_some())
let user: Option<String> = req.authorized_user();
let lpath: Vec<Link> = vec![Link::year(year),
Link::month(year, month)];
let title: String = format!("Photos from {} {} {}",
day, monthname(month), year);
let photos: Vec<Photo> = Photo::query(req.authorized_user().is_some())
.filter(date.ge(thedate))
.filter(date.lt(thedate + ChDuration::days(1)))
.order((grade.desc(), date.asc()))
.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,
@ -673,11 +678,12 @@ fn on_this_day<'mw>(req: &mut Request,
let now = time::now();
(now.tm_mon as u32 + 1, now.tm_mday as u32)
};
render!(res, "templates/groups.tpl", {
user: Option<String> = req.authorized_user(),
title: String = format!("Photos from {} {}", day, monthname(month)),
groups: Vec<Group> =
SqlLiteral::new(format!(
let mut stream = try!(res.start());
match templates::groups(&mut stream,
&format!("Photos from {} {}", day, monthname(month)),
vec![],
req.authorized_user(),
SqlLiteral::new(format!(
"select extract(year from date) y, count(*) c \
from photos where extract(month from date)={} \
and extract(day from date)={}{} group by y order by y desc",
@ -706,7 +712,10 @@ fn on_this_day<'mw>(req: &mut Request,
photo: photo
}
}).collect()
})
) {
Ok(()) => Ok(Halt(stream)),
Err(e) => stream.bail(format!("Problem rendering template: {:?}", e))
}
}
#[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>
<html>
@ -10,7 +11,7 @@
@CSSLINK
</head>
<body>
@head
@:head(lpath, user)
<h1>@title</h1>
<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;
@(lpath: Vec<Link>)
@(lpath: Vec<Link>, user: Option<String>)
<header>
<span><a href="/">Bilder</a>
@ -10,9 +10,6 @@
<span>· <a href="/person/">Personer</a></span>
<span>· <a href="/place/">Platser</a></span>
<span>· <a href="/thisday">Denna dag</a></span>
@* {{#user}}<span class="user">{{.}}
(<a href="/logout">log out</a>)
</span>{{/user}}
{{^user}}<span class="user">(<a href="/login">log in</a>)</span>{{/user}}
*@
@if let Some(u) = user {<span class="user">@u (<a href="/logout">log out</a>)</span>}
else {<span class="user">(<a href="/login">log in</a>)</span>}
</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>