From 5028a1831c1c5bc42869f2624e2042c5dcc75698 Mon Sep 17 00:00:00 2001 From: Rasmus Kaj Date: Mon, 20 Nov 2017 12:42:01 +0100 Subject: [PATCH] Add timecluster grouping for place views. Just as for people and tag views. --- src/server/mod.rs | 59 ++++++++++++++++++++++++++--------------- templates/place.rs.html | 9 ++++--- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index dac56f1..3165552 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -450,27 +450,44 @@ fn place_one<'mw>( let c: &PgConnection = &req.db_conn(); if let Ok(place) = places.filter(slug.eq(tslug)).first::(c) { use schema::photo_places::dsl::{photo_id, photo_places, place_id}; - use schema::photos::dsl::{date, grade, id}; - return res.ok(|o| { - templates::place( - o, - req, - &Photo::query(req.authorized_user().is_some()) - .filter( - id.eq_any( - photo_places - .select(photo_id) - .filter(place_id.eq(place.id)), - ), - ) - .order( - (grade.desc().nulls_last(), date.desc().nulls_last()), - ) - .load(c) - .unwrap(), - &place, - ) - }); + use schema::photos::dsl::{date, id}; + let photos = Photo::query(req.authorized_user().is_some()).filter( + id.eq_any(photo_places.select(photo_id).filter(place_id.eq(place.id))), + ); + let photos = if let Some(from_date) = query_date(req, "from") { + photos.filter(date.ge(from_date)) + } else { + photos + }; + let photos = if let Some(to_date) = query_date(req, "to") { + photos.filter(date.le(to_date)) + } else { + photos + }; + let photos = photos.order(date.desc().nulls_last()).load(c).unwrap(); + if let Some(groups) = split_to_groups(&photos) { + return res.ok(|o| { + let path = req.path_without_query().unwrap_or("/"); + templates::place( + o, + req, + &groups + .iter() + .map(|g| PhotoLink::for_group(g, path)) + .collect::>(), + &place, + ) + }); + } else { + return res.ok(|o| { + templates::place( + o, + req, + &photos.iter().map(PhotoLink::from).collect::>(), + &place, + ) + }); + } } res.not_found("Not a place") } diff --git a/templates/place.rs.html b/templates/place.rs.html index 2726dac..578b3b7 100644 --- a/templates/place.rs.html +++ b/templates/place.rs.html @@ -1,10 +1,11 @@ @use nickel::Request; -@use models::{Photo, Place}; -@use templates::{page_base, img_link}; +@use models::{Place}; +@use server::PhotoLink; +@use templates::{page_base, photo_link}; -@(req: &Request, photos: &[Photo], place: &Place) +@(req: &Request, photos: &[PhotoLink], place: &Place) @:page_base(req, &format!("Photos from {}", place.place_name), &[], {
- @for p in photos {@:img_link(p)} + @for p in photos {@:photo_link(p)}
})