centralized tagging

This commit is contained in:
dave 2018-09-13 22:59:22 -07:00
parent b0f003b5e1
commit 063f30ef0b
5 changed files with 105 additions and 21 deletions

View File

@ -14,11 +14,6 @@ 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
@ -85,9 +80,47 @@ class PhotosWeb(object):
query = query.filter(PhotoSet.uuid == i)
yield self.render("map.html", images=query.all(), zoom=int(zoom))
@cherrypy.expose
def create_tags(self, fromdate=None, tag=None, newtag=None):
"""
Tag multiple items addressable in one of a couple ways
TODO move this somewhere better?
"""
s = self.session()
photos = None
num_photos = None
if fromdate:
dt = datetime.strptime(fromdate, "%Y-%m-%d")
dt_end = dt + timedelta(days=1)
photos = s.query(PhotoSet).filter(and_(PhotoSet.date >= dt,
PhotoSet.date < dt_end)).order_by(PhotoSet.date)
num_photos = s.query(func.count(PhotoSet.id)). \
filter(and_(PhotoSet.date >= dt, PhotoSet.date < dt_end)).order_by(PhotoSet.date).scalar()
if newtag:
# TODO validate uuid ?
s.add(Tag(title=newtag)) # TODO slug
# TODO generate slug now or in model?
s.commit()
# raise cherrypy.HTTPRedirect('/photo/{}/tag'.format(uuid), 302)
if tag: # Create the tag on all the photos
tag = s.query(Tag).filter(Tag.uuid == tag).first()
for photo in photos.all():
if 0 == s.query(func.count(TagItem.id)).filter(TagItem.tag_id == tag.id,
TagItem.set_id == photo.id).scalar():
s.add(TagItem(tag_id=tag.id, set_id=photo.id))
s.commit()
alltags = s.query(Tag).order_by(Tag.title).all()
yield self.render("create_tags.html", images=photos, alltags=alltags, num_photos=num_photos, fromdate=fromdate)
@cherrypy.popargs('date')
class DateView(object):
"""
View all the photos shot on a given date
"""
def __init__(self, master):
self.master = master
@ -222,6 +255,39 @@ class PhotoView(object):
raise cherrypy.HTTPRedirect('/photo/{}/tag'.format(uuid), 302)
@cherrypy.popargs('uuid')
class TagView(object):
"""
View the photos associated with a single tag
"""
def __init__(self, master):
self.master = master
# self._cp_config = {"tools.trailing_slash.on": False}
@cherrypy.expose
def index(self, uuid, page=0):
page = int(page)
pgsize = 100
s = self.master.session()
tag = s.query(Tag).filter(Tag.uuid == uuid).first()
numphotos = s.query(func.count(Tag.id)).join(TagItem).join(PhotoSet).filter(Tag.uuid == uuid).scalar()
photos = s.query(PhotoSet).join(TagItem).join(Tag).filter(Tag.uuid == uuid).offset(page * pgsize).limit(pgsize).all()
yield self.master.render("album.html", tag=tag, images=photos, total_items=numphotos, pgsize=100, page=page)
@cherrypy.expose
def op(self, uuid, op):
s = self.master.session()
tag = s.query(Tag).filter(Tag.uuid == uuid).first()
if op == "Demote to tag":
tag.is_album = 0
elif op == "Promote to album":
tag.is_album = 1
else:
raise Exception("Invalid op")
s.commit()
raise cherrypy.HTTPRedirect('/tag/{}'.format(tag.uuid), 302)
def main():
import argparse
import signal

View File

@ -3,7 +3,11 @@
{% set subtitle = "By date, descending" %}
{% block buttons %}
xxx
<form action="/tag/{{ tag.uuid }}/op" method="post">
{% if tag.is_album %}<input type="submit" class="secondary-button pure-button" name="op" value="Demote to tag" />{% else %}
<input type="submit" class="secondary-button pure-button" name="op" value="Promote to album" />{% endif %}
<input type="submit" class="secondary-button pure-button" name="op" value="Delete tag" />
</form>
{% endblock %}
{% block body %}

View File

@ -1,19 +1,27 @@
{% extends "page.html" %}
{% set title = "Placeholder Title" %}
{% set subtitle = image.uuid %}
{% set subtitle = "Foobar" %}
{% include "page-top.html" %}
{% block body %}
{% set preview = 9 %}
<div class="photo-tagging pure-g">
<div class="current-tags pure-u-1-3">
<h2>Images ({{ num_photos }})</h2>
<div>
{% for image in images %}{% if loop.index <= preview %}
<a href="/photo/{{ image.uuid }}">
<img src="/thumb/set/small/{{ image.uuid }}.jpg" />
</a>
{% endif %}{% endfor %}
</div>
{% if num_photos > preview %}<br clear="both" /><p>...and {{ num_photos - preview }} more</p>{% endif %}
<h2>Current Tags</h2>
{#<a href="/photo/{{ image.uuid }}">
<img src="/thumb/set/small/{{ image.uuid }}.jpg" />
</a>#}
<ul class="tags-picker">
{% for tagi in images[0].tags %}
<li>
<a href="/tags/{{ tagi.tag.uuid }}">{{ tagi.tag.title }}</a>
<a href="/tag/{{ tagi.tag.uuid }}">{{ tagi.tag.title }}</a>
</li>
{% endfor %}
</ul>
@ -23,7 +31,8 @@
<ul class="tags-picker">
{% for tag in alltags %}
<li>
<form action="/photo/{# image.uuid #}/tag_add" method="post">
<form action="/create_tags" method="post">
{% if fromdate %}<input type="hidden" name="fromdate" value="{{ fromdate }}" />{% endif %}
<input type="hidden" name="tag" value="{{ tag.uuid }}" />
<input class="submit-link" type="submit" value="{{ tag.title }}" />
</form>
@ -33,11 +42,12 @@
</div>
<div class="add-tags pure-u-1-3">
<h2>Add tag</h2>
<form action="/photo/{# image.uuid #}/tag_create" method="post" class="pure-form">
<input type="text" name="tag" placeholder="new tag name" />
<form action="/create_tags" method="post" class="pure-form">
{% if fromdate %}<input type="hidden" name="fromdate" value="{{ fromdate }}" />{% endif %}
<input type="text" name="newtag" placeholder="new tag name" />
<input type="submit" value="Add" class="pure-button pure-button-primary" />
</form>
</div>
</div>
{% include "page-bottom.html" %}
{% endblock %}

View File

@ -1,7 +1,12 @@
{% extends "page.html" %}
{% set title = "Placeholder Title" %}
{% set subtitle = image.uuid %}
{% include "page-top.html" %}
{% block buttons %}
xxx
{% endblock %}
{% block body %}
<div class="photo-view pure-g">
<div class="photo-preview pure-u-2-3">
@ -60,7 +65,7 @@
<ul class="tags-picker">
{% for tagi in image.tags %}
<li>
<a href="/tags/{{ tagi.tag.uuid }}">{{ tagi.tag.title }}</a>
<a href="/tag/{{ tagi.tag.uuid }}">{{ tagi.tag.title }}</a>
</li>
{% endfor %}
</ul>
@ -68,5 +73,4 @@
</div>
</div>
{% include "page-bottom.html" %}
{% endblock %}

View File

@ -5,7 +5,7 @@
{% block buttons %}
xxx
{% endblock %}
{% block body %}