add dates view

This commit is contained in:
dave 2018-09-11 20:35:04 -07:00
parent c952471b33
commit 69902d942d
5 changed files with 82 additions and 9 deletions

View File

@ -1,18 +1,24 @@
import os
import cherrypy
import logging
from datetime import datetime, timedelta
from photoapp.library import PhotoLibrary
from photoapp.types import Photo, PhotoSet, Tag, TagItem
from jinja2 import Environment, FileSystemLoader, select_autoescape
from sqlalchemy import desc
from sqlalchemy.exc import IntegrityError
from sqlalchemy import func
from sqlalchemy import func, and_
import math
APPROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../"))
def tplglobals():
print("###", cherrypy.request.path_info)
return dict(path=cherrypy.request.path_info)
class PhotosWeb(object):
def __init__(self, library, template_dir):
self.library = library
@ -24,6 +30,7 @@ class PhotosWeb(object):
self.thumb = ThumbnailView(self)
self.photo = PhotoView(self)
self.download = DownloadView(self)
self.date = DateView(self)
def render(self, template, **kwargs):
return self.tpl.get_template(template).render(**kwargs, **self.get_default_vars())
@ -45,7 +52,8 @@ class PhotosWeb(object):
"image/gif": "gif",
"application/octet-stream-xmp": "xmp",
"image/x-canon-cr2": "cr2",
"video/mp4": "mp4"}[mime]
"video/mp4": "mp4",
"video/quicktime": "mov"}[mime]
@cherrypy.expose
def index(self):
@ -57,7 +65,7 @@ class PhotosWeb(object):
page, pgsize = int(page), int(pgsize)
total_sets = s.query(func.count(PhotoSet.id)).first()[0]
images = s.query(PhotoSet).order_by(PhotoSet.date.desc()).offset(pgsize * page).limit(pgsize).all()
yield self.render("feed.html", images=[i for i in images], page=page, pgsize=int(pgsize), total_sets=total_sets)
yield self.render("feed.html", images=[i for i in images], page=page, pgsize=int(pgsize), total_sets=total_sets, **tplglobals())
@cherrypy.expose
def monthly(self):
@ -78,6 +86,37 @@ class PhotosWeb(object):
yield self.render("map.html", images=query.all(), zoom=int(zoom))
@cherrypy.popargs('date')
class DateView(object):
def __init__(self, master):
self.master = master
@cherrypy.expose
def index(self, date=None, page=0):
s = self.master.session()
if date:
page = int(page)
pgsize = 100
dt = datetime.strptime(date, "%Y-%m-%d")
dt_end = dt + timedelta(days=1)
total_sets = s.query(func.count(PhotoSet.id)). \
filter(and_(PhotoSet.date >= dt, PhotoSet.date < dt_end)).first()[0]
images = s.query(PhotoSet).filter(and_(PhotoSet.date >= dt,
PhotoSet.date < dt_end)).order_by(PhotoSet.date). \
offset(page * pgsize).limit(pgsize).all()
yield self.master.render("feed.html", page=page, pgsize=pgsize, total_sets=total_sets,
images=[i for i in images], **tplglobals())
return
images = s.query(PhotoSet, func.strftime('%Y-%m-%d',
PhotoSet.date).label('gdate'),
func.count('photos.id'),
func.strftime('%Y', PhotoSet.date).label('year'),
func.strftime('%m', PhotoSet.date).label('month'),
func.strftime('%d', PhotoSet.date).label('day')). \
group_by('gdate').order_by(desc('year'), 'month', 'day').all()
yield self.master.render("dates.html", images=images)
@cherrypy.popargs('item_type', 'thumb_size', 'uuid')
class ThumbnailView(object):
def __init__(self, master):

View File

@ -246,3 +246,12 @@ ul.pager {
}
}
}
.date-feed {
a {
padding-right: 15px;
}
.many {
font-weight: bold;
}
}

24
templates/dates.html Normal file
View File

@ -0,0 +1,24 @@
{% set title = "All photos" %}
{% set subtitle = "By date, descending" %}
{% include "page-top.html" %}
{% set locals = namespace() %}
<div class="date-feed">
{% set locals.year = "" %}
{% set locals.month = "" %}
{% for item, date, count, _, _, _ in images %}
{% if item.date.year != locals.year %}
{% set locals.year = item.date.year %}
<div class="feed-divider year"><h4>{{ item.date.year }}</h4></div>
{% endif %}
{% if item.date.month != locals.month %}
{% set locals.month = item.date.month %}
<div class="feed-divider month"><h4>{{ item.date.strftime("%B") }}</h4></div>
{% endif %}
<a class="date-item{% if count > 50 %} many{% endif %}" href="/date/{{ date }}">{{ date }} ({{ count }})</a>
{% endfor %}
</div>
{% include "page-bottom.html" %}

View File

@ -1,4 +1,4 @@
{% set title = "All photos" %}
{% set title = "Photos by date" %}
{% set subtitle = "By date, descending" %}
{% include "page-top.html" %}
@ -17,7 +17,7 @@
{% endif %}
<div class="photo">
<a href="/photo/{{ item.uuid }}">
<img src="thumb/set/feed/{{ item.uuid }}.jpg" />
<img src="/thumb/set/feed/{{ item.uuid }}.jpg" />
</a>
</div>
{% endfor %}
@ -26,21 +26,21 @@
<h6>Page</h6>
{% if page > 0 %}
<div class="nav-prev">
<a href="feed?page={{ page - 1 }}">Previous</a>
<a href="{{path}}?page={{ page - 1 }}">Previous</a>
</div>
{% endif %}
<div class="pages">
<ul class="pager">
{% for pgnum in range(0, total_pages) %}
<li{% if pgnum == page %} class="current"{% endif %}>
<a href="/feed?page={{ pgnum }}">{{ pgnum }}</a>
<a href="{{path}}?page={{ pgnum }}">{{ pgnum }}</a>
</li>
{% endfor %}
</ul>
</div>
{% if page + 1 < total_pages %}
<div class="nav-next">
<a href="feed?page={{ page + 1 }}">Next</a>
<a href="{{path}}?page={{ page + 1 }}">Next</a>
</div>
{% endif %}
</div>

View File

@ -15,7 +15,8 @@
<ul class="pure-menu-list">
<li class="pure-menu-item"><a href="/" class="pure-menu-link">All photos</a></li>
<li class="pure-menu-item"><a href="/albums" class="pure-menu-link">Albums</a></li>
<li class="pure-menu-item"><a href="/monthly" class="pure-menu-link">By month</a></li>
<li class="pure-menu-item"><a href="/date" class="pure-menu-link">Dates</a></li>
<li class="pure-menu-item"><a href="/monthly" class="pure-menu-link">Stats</a></li>
<li class="pure-menu-item"><a href="/map" class="pure-menu-link">Map</a></li>
<li class="pure-menu-item"><a href="/admin/trash" class="pure-menu-link">Trash</a></li>
<li class="pure-menu-heading">Albums</li>