Allow templates to get objects instead of json

This commit is contained in:
voussoir 2016-12-20 16:33:40 -08:00
parent 232f8231e0
commit d5bc65c8f2
7 changed files with 81 additions and 64 deletions

View file

@ -244,10 +244,6 @@ def favicon():
def get_album_core(albumid):
album = P_album(albumid)
album = jsonify.album(album)
album['sub_albums'] = [P_album(x) for x in album['sub_albums']]
album['sub_albums'].sort(key=lambda x: (x.title or x.id).lower())
album['sub_albums'] = [jsonify.album(x, minimal=True) for x in album['sub_albums']]
return album
@site.route('/album/<albumid>')
@ -258,7 +254,6 @@ def get_album_html(albumid):
response = flask.render_template(
'album.html',
album=album,
photos=album['photos'],
session=session,
view=request.args.get('view', 'grid'),
)
@ -268,6 +263,10 @@ def get_album_html(albumid):
@session_manager.give_token
def get_album_json(albumid):
album = get_album_core(albumid)
album = jsonify.album(album)
album['sub_albums'] = [P_album(x) for x in album['sub_albums']]
album['sub_albums'].sort(key=lambda x: (x.title or x.id).lower())
album['sub_albums'] = [jsonify.album(x, minimal=True) for x in album['sub_albums']]
return jsonify.make_json_response(album)
@ -285,7 +284,6 @@ def get_album_tar(albumid):
def get_albums_core():
albums = P.get_albums()
albums = [a for a in albums if a.parent() is None]
albums = [jsonify.album(album, minimal=True) for album in albums]
return albums
@site.route('/albums')
@ -299,6 +297,7 @@ def get_albums_html():
@session_manager.give_token
def get_albums_json():
albums = get_albums_core()
albums = [jsonify.album(album, minimal=True) for album in albums]
return jsonify.make_json_response(albums)
@ -338,14 +337,12 @@ def get_file(photoid):
def get_photo_core(photoid):
photo = P_photo(photoid)
photo = jsonify.photo(photo)
return photo
@site.route('/photo/<photoid>', methods=['GET'])
@session_manager.give_token
def get_photo_html(photoid):
photo = get_photo_core(photoid)
photo['tags'].sort(key=lambda x: x['qualified_name'])
session = session_manager.get(request)
return flask.render_template('photo.html', photo=photo, session=session)
@ -353,6 +350,7 @@ def get_photo_html(photoid):
@session_manager.give_token
def get_photo_json(photoid):
photo = get_photo_core(photoid)
photo = jsonify.photo(photo)
photo = jsonify.make_json_response(photo)
return photo
@ -449,15 +447,14 @@ def get_search_core():
#print(search_kwargs)
with warnings.catch_warnings(record=True) as catcher:
photos = list(P.search(**search_kwargs))
photos = [jsonify.photo(photo, include_albums=False) for photo in photos]
warns = [str(warning.message) for warning in catcher]
#print(warns)
# TAGS ON THIS PAGE
total_tags = set()
for photo in photos:
for tag in photo['tags']:
total_tags.add(tag['qualified_name'])
for tag in photo.tags():
total_tags.add(tag.qualified_name())
total_tags = sorted(total_tags)
# PREV-NEXT PAGE URLS
@ -515,6 +512,7 @@ def get_search_html():
@session_manager.give_token
def get_search_json():
search_results = get_search_core()
search_results['photos'] = [jsonify.photo(photo, include_albums=False) for photo in search_results['photos']]
#search_kwargs = search_results['search_kwargs']
#qualname_map = search_results['qualname_map']
include_qualname_map = request.args.get('include_map', False)

View file

@ -32,7 +32,7 @@ def photo(p, include_albums=True, include_tags=True):
'ratio': p.ratio,
'area': p.area,
'bytes': p.bytes,
'duration_str': helpers.seconds_to_hms(p.duration) if p.duration is not None else None,
'duration_str': p.duration_string(),
'duration': p.duration,
'bytes_str': p.bytestring(),
'has_thumbnail': bool(p.thumbnail),

View file

@ -367,6 +367,11 @@ class Photo(ObjectBase):
self.photodb.log.debug('Committing - delete photo')
self.photodb.commit()
def duration_string(self):
if self.duration is None:
return None
return helpers.seconds_to_hms(self.duration)
@decorators.time_me
def generate_thumbnail(self, *, commit=True, **special):
'''
@ -606,6 +611,11 @@ class Photo(ObjectBase):
self.__reinit__()
def sorted_tags(self):
tags = self.tags()
tags.sort(key=lambda x: x.qualified_name())
return tags
def tags(self):
'''
Return the tags assigned to this Photo.

View file

@ -3,7 +3,7 @@
<head>
{% import "photo_card.html" as photo_card %}
{% import "header.html" as header %}
<title>Album {{album["title"]}}</title>
<title>Album {{album.title}}</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="/static/common.css">
@ -24,29 +24,31 @@ p
<body>
{{header.make_header(session=session)}}
<div id="content_body">
<h2>{{album["title"]}}</h2>
<p>{{album["description"]}}</p>
{% set parent=album["parent"] %}
<h2>{{album.title}}</h2>
<p>{{album.description}}</p>
{% set parent=album.parent() %}
{% if parent %}
<h3>Parent: <a href="/album/{{parent["id"]}}">{{parent["id"] + " " + parent.title}}</a></h3>
<h3>Parent: <a href="/album/{{parent.id}}">{{parent.id + " " + parent.title}}</a></h3>
{% else %}
<h3>Parent: <a href="/albums">Albums</a></h3>
{% endif %}
{% if album["sub_albums"] %}
{% set sub_albums = album.children() %}
{% if sub_albums %}
<h3>Sub-albums</h3>
<ul>
{% for sub_album in album["sub_albums"] %}
<li><a href="/album/{{sub_album["id"]}}">
{% if sub_album["title"] %}
{{sub_album["title"]}}
{% for sub_album in sub_albums %}
<li><a href="/album/{{sub_album.id}}">
{% if sub_album.title %}
{{sub_album.title}}
{% else %}
{{sub_album["id"]}}
{{sub_album.id}}
{% endif %}</a>
</li>
{% endfor %}
</ul>
{% endif %}
<span><a href="/album/{{album["id"]}}.tar">(download .tar)</a></span>
<span><a href="/album/{{album.id}}.tar">(download .tar)</a></span>
{% set photos = album.photos() %}
{% if photos %}
<h3>Photos</h3>
<ul>
@ -62,7 +64,7 @@ p
<script type="text/javascript">
function submit_tag(callback)
{
add_photo_tag('{{album["id"]}}', add_tag_box.value, callback);
add_photo_tag('{{album.id}}', add_tag_box.value, callback);
add_tag_box.value = "";
}
</script>

View file

@ -19,12 +19,12 @@
{{header.make_header(session=session)}}
<div id="content_body">
{% for album in albums %}
{% if album["title"] %}
{% set title=album["id"] + " " + album["title"] %}
{% if album.title %}
{% set title=album.id + " " + album.title %}
{% else %}
{% set title=album["id"] %}
{% set title=album.id %}
{% endif %}
<div><a href="/album/{{album["id"]}}">{{album["id"] + " " + album["title"]}}</a></div>
<div><a href="/album/{{album.id}}">{{album.id + " " + album.title}}</a></div>
{% endfor %}
</div>
</body>

View file

@ -6,9 +6,9 @@
<meta charset="UTF-8">
<link rel="stylesheet" href="/static/common.css">
<script src="/static/common.js"></script>
{% set filename = photo["id"] + "." + photo["extension"] %}
{% set filename = photo.id + "." + photo.extension %}
{% set link = "/file/" + filename %}
{% set mimetype=photo["mimetype"] %}
{% set mimetype=photo.mimetype() %}
<style>
#content_body
@ -94,10 +94,11 @@
<!-- TAG INFO -->
<h4>Tags</h4>
<ul id="this_tags">
{% for tag in photo['tags'] %}
{% set tags = photo.sorted_tags() %}
{% for tag in tags %}
<li>
<a class="tag_object" href="/search?tag_musts={{tag["name"]}}">{{tag["qualified_name"]}}</a>
<button class="remove_tag_button" onclick="remove_photo_tag('{{photo["id"]}}', '{{tag["name"]}}', receive_callback);"></button>
<a class="tag_object" href="/search?tag_musts={{tag.name}}">{{tag.qualified_name()}}</a>
<button class="remove_tag_button" onclick="remove_photo_tag('{{photo.id}}', '{{tag.name}}', receive_callback);"></button>
</li>
{% endfor %}
<li>
@ -109,24 +110,29 @@
<!-- METADATA & DOWNLOAD -->
<h4>File info</h4>
<ul id="metadata">
{% if photo["width"] %}
<li>Dimensions: {{photo["width"]}}x{{photo["height"]}} px</li>
<li>Aspect ratio: {{photo["ratio"]}}</li>
<li>Size: {{photo["bytes_str"]}}</li>
{% if photo.author_id %}
{% set author = photo.author() %}
<li>Author: <a href="/user/{{author.username}}">{{author.username}}</a>
{% endif %}
{% if photo["duration"] %}
<li>Duration: {{photo["duration_str"]}}</li>
{% if photo.width %}
<li>Dimensions: {{photo.width}}x{{photo.height}} px</li>
<li>Aspect ratio: {{photo.ratio}}</li>
<li>Size: {{photo.bytestring()}}</li>
{% endif %}
<li><a href="/file/{{photo["id"]}}.{{photo["extension"]}}?download=1">Download as {{photo["id"]}}.{{photo["extension"]}}</a></li>
<li><a href="/file/{{photo["id"]}}.{{photo["extension"]}}?download=1&original_filename=1">Download as "{{photo["filename"]}}"</a></li>
{% if photo.duration %}
<li>Duration: {{photo.duration_str}}</li>
{% endif %}
<li><a href="/file/{{photo.id}}.{{photo.extension}}?download=1">Download as {{photo.id}}.{{photo.extension}}</a></li>
<li><a href="/file/{{photo.id}}.{{photo.extension}}?download=1&original_filename=1">Download as "{{photo.basename}}"</a></li>
</ul>
<!-- CONTAINING ALBUMS -->
{% if photo["albums"] %}
{% set albums = photo.albums() %}
{% if albums %}
<h4>Albums containing this photo</h4>
<ul id="containing albums">
{% for album in photo["albums"] %}
<li><a href="/album/{{album["id"]}}">{{album["title"]}}</a></li>
{% for album in albums %}
<li><a href="/album/{{album.id}}">{{album.title}}</a></li>
{% endfor %}
{% endif %}
</ul>
@ -142,7 +148,7 @@
<div id="photo_img_holder"><img id="photo_img" src="{{link}}" onclick="toggle_hoverzoom()" onload="this.style.opacity=0.99"></div>
<!-- <img src="{{link}}"> -->
{% elif mimetype == "video" %}
<video src="{{link}}" controls preload=none {%if photo["has_thumbnail"]%}poster="/thumbnail/{{photo["id"]}}.jpg"{%endif%}></video>
<video src="{{link}}" controls preload=none {%if photo.thumbnail%}poster="/thumbnail/{{photo.id}}.jpg"{%endif%}></video>
{% elif mimetype == "audio" %}
<audio src="{{link}}" controls></audio>
{% else %}
@ -261,7 +267,7 @@ function receive_callback(response)
}
function submit_tag(callback)
{
add_photo_tag('{{photo["id"]}}', add_tag_box.value, callback);
add_photo_tag('{{photo.id}}', add_tag_box.value, callback);
add_tag_box.value = "";
}
</script>

View file

@ -16,20 +16,20 @@
{% if view == "list" %}
<div class="photo_card_list">
<a target="_blank" href="/photo/{{photo["id"]}}">{{photo["filename"]}}</a>
<a target="_blank" href="/photo/{{photo.id}}">{{photo.basename}}</a>
</div>
{% else %}
<div class="photo_card_grid">
<div class="photo_card_grid_thumb">
<a target="_blank" href="/photo/{{photo["id"]}}">
<a target="_blank" href="/photo/{{photo.id}}">
<img height="150"
{% if photo["has_thumbnail"] %}
src="/thumbnail/{{photo["id"]}}.jpg"
{% if photo.thumbnail %}
src="/thumbnail/{{photo.id}}.jpg"
{% else %}
{% set choice =
thumbnails.get(photo["extension"],
thumbnails.get(photo["mimetype"],
thumbnails.get(photo.extension,
thumbnails.get(photo.mimetype,
'other'))
%}
src="/static/basic_thumbnails/{{choice}}.png"
@ -37,23 +37,24 @@
</a>
</div>
<div class="photo_card_grid_info">
<a target="_blank" href="/photo/{{photo["id"]}}">{{photo["filename"]}}</a>
<a target="_blank" href="/photo/{{photo.id}}">{{photo.basename}}</a>
<span class="photo_card_grid_file_metadata">
{% if photo["width"] %}
{{photo["width"]}}x{{photo["height"]}},
{% if photo.width %}
{{photo.width}}x{{photo.height}},
{% endif %}
{% if photo["duration"] %}
{{photo["duration_str"]}},
{% if photo.duration %}
{{photo.duration_string()}},
{% endif %}
{{photo["bytes_str"]}}
{{photo.bytestring()}}
</span>
<span class="photo_card_grid_tags">
{% if photo["tags"] %}
{% set tags=[] %}
{% for tag in photo["tags"] %}
{% do tags.append(tag["name"]) %}
{% set tags = photo.tags() %}
{% set tag_names = [] %}
{% for tag in tags %}
{% do tag_names.append(tag.name) %}
{% endfor %}
<span title="{{", ".join(tags)}}">T</span>
{% if tags %}
<span title="{{", ".join(tag_names)}}">T</span>
{% endif %}
</span>
</div>