Views per year/month/day.

This commit is contained in:
Rasmus Kaj 2015-12-25 18:32:11 +01:00
parent d2adab3ecb
commit ba094853a2
2 changed files with 75 additions and 9 deletions

View File

@ -10,11 +10,13 @@ extern crate hyper;
extern crate time; extern crate time;
extern crate chrono; extern crate chrono;
mod models; use chrono::UTC;
use chrono::offset::TimeZone;
use chrono::{Duration as ChDuration};
use chrono::Datelike;
use hyper::header::{Expires, HttpDate}; use hyper::header::{Expires, HttpDate};
use image::open as image_open; use image::open as image_open;
use image::{FilterType, ImageFormat, GenericImage}; use image::{FilterType, ImageFormat, GenericImage};
use models::{Entity, Photo, Tag, Person, Place, query_for};
use nickel::{MediaType, Nickel, StaticFilesHandler}; use nickel::{MediaType, Nickel, StaticFilesHandler};
use plugin::{Pluggable}; use plugin::{Pluggable};
use rustc_serialize::Encodable; use rustc_serialize::Encodable;
@ -22,6 +24,9 @@ use rustorm::query::Query;
use std::path::PathBuf; use std::path::PathBuf;
use time::Duration; use time::Duration;
mod models;
use models::{Entity, Photo, Tag, Person, Place, query_for};
mod env; mod env;
use env::{dburl, env_or, photos_dir}; use env::{dburl, env_or, photos_dir};
@ -131,12 +136,22 @@ fn main() {
req.orm_get_related(&photo, "photo_place").unwrap(), req.orm_get_related(&photo, "photo_place").unwrap(),
tags: Vec<Tag> = tags: Vec<Tag> =
req.orm_get_related(&photo, "photo_tag").unwrap(), req.orm_get_related(&photo, "photo_tag").unwrap(),
date: String = time: String = match photo.date {
if let Some(d) = photo.date { Some(d) => d.format("%T").to_string(),
d.to_rfc3339() None => "".to_string()
} else { },
"".to_string() year: i32 = match photo.date {
}, Some(d) => d.year(),
None => 0
},
month: u32 = match photo.date {
Some(d) => d.month(),
None => 0
},
day: u32 = match photo.date {
Some(d) => d.day(),
None => 0
},
photo: Photo = photo photo: Photo = photo
}); });
} }
@ -210,6 +225,53 @@ fn main() {
} }
} }
} }
get "/:year/" => |req, res| {
if let Ok(year) = req.param("year").unwrap().parse::<i32>() {
let date = UTC.ymd(year, 1, 1).and_hms(0,0,0);
return render!(res, "templates/index.tpl", {
photos: Vec<Photo> = query_for::<Photo>()
.filter_gte("date", &date)
.filter_lt("date", &(date + ChDuration::days(366)))
.desc_nulls_last("grade")
.asc_nulls_last("date")
.limit(36)
.collect(req.db_conn()).unwrap()
});
}
}
get "/:year/:month/" => |req, res| {
if let Ok(year) = req.param("year").unwrap().parse::<i32>() {
if let Ok(month) = req.param("month").unwrap().parse::<u32>() {
let date = UTC.ymd(year, month, 1).and_hms(0,0,0);
return render!(res, "templates/index.tpl", {
photos: Vec<Photo> = query_for::<Photo>()
.filter_gte("date", &date)
.filter_lt("date", &(date + ChDuration::days(31)))
.desc_nulls_last("grade")
.asc_nulls_last("date")
.limit(36)
.collect(req.db_conn()).unwrap()
});
}
}
}
get "/:year/:month/:day" => |req, res| {
if let Ok(year) = req.param("year").unwrap().parse::<i32>() {
if let Ok(month) = req.param("month").unwrap().parse::<u32>() {
if let Ok(day) = req.param("day").unwrap().parse::<u32>() {
let date = UTC.ymd(year, month, day).and_hms(0,0,0);
return render!(res, "templates/index.tpl", {
photos: Vec<Photo> = query_for::<Photo>()
.filter_gte("date", &date)
.filter_lt("date", &(date + ChDuration::days(1)))
.desc_nulls_last("grade")
.asc_nulls_last("date")
.collect(req.db_conn()).unwrap()
});
}
}
}
}
}); });
server.listen(&*env_or("RPHOTOS_LISTEN", "127.0.0.1:6767")); server.listen(&*env_or("RPHOTOS_LISTEN", "127.0.0.1:6767"));

View File

@ -12,7 +12,11 @@
<p><a href="/img/{{photo.id}}/l">{{photo.path}}</a></p> <p><a href="/img/{{photo.id}}/l">{{photo.path}}</a></p>
<p><img src="/img/{{photo.id}}/m"></p> <p><img src="/img/{{photo.id}}/m"></p>
{{#photo.grade}}<p>Betyg: {{.}}</p>{{/photo.grade}} {{#photo.grade}}<p>Betyg: {{.}}</p>{{/photo.grade}}
<p>{{date}}</p> {{#photo.date}}<p>
<a href="/{{year}}/">{{year}}</a>-<a
href="/{{year}}/{{month}}/">{{month}}</a>-<a
href="/{{year}}/{{month}}/{{day}}">{{day}}</a>
{{time}}</p>{{/photo.date}}
<p>People: {{#people}}<a href="/person/{{slug}}">{{name}}</a>, {{/people}}</p> <p>People: {{#people}}<a href="/person/{{slug}}">{{name}}</a>, {{/people}}</p>
<p>Places: {{#places}}<a href="/place/{{slug}}">{{place}}</a>, {{/places}}</p> <p>Places: {{#places}}<a href="/place/{{slug}}">{{place}}</a>, {{/places}}</p>
<p>Tags: {{#tags}}<a href="/tag/{{slug}}">{{tag}}</a>, {{/tags}}</p> <p>Tags: {{#tags}}<a href="/tag/{{slug}}">{{tag}}</a>, {{/tags}}</p>