From d5bc65c8f2519215e3c30e30126a4c4849aca549 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 20 Dec 2016 16:33:40 -0800 Subject: [PATCH] Allow templates to get objects instead of json --- etiquette.py | 20 +++++++++---------- jsonify.py | 2 +- objects.py | 10 ++++++++++ templates/album.html | 28 ++++++++++++++------------ templates/albums.html | 8 ++++---- templates/photo.html | 42 ++++++++++++++++++++++----------------- templates/photo_card.html | 35 ++++++++++++++++---------------- 7 files changed, 81 insertions(+), 64 deletions(-) diff --git a/etiquette.py b/etiquette.py index c04f4b5..e841057 100644 --- a/etiquette.py +++ b/etiquette.py @@ -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/') @@ -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/', 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) diff --git a/jsonify.py b/jsonify.py index 1354f1f..3418a38 100644 --- a/jsonify.py +++ b/jsonify.py @@ -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), diff --git a/objects.py b/objects.py index ac186d8..7715281 100644 --- a/objects.py +++ b/objects.py @@ -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. diff --git a/templates/album.html b/templates/album.html index 90e6cee..19b4302 100644 --- a/templates/album.html +++ b/templates/album.html @@ -3,7 +3,7 @@ {% import "photo_card.html" as photo_card %} {% import "header.html" as header %} - Album {{album["title"]}} + Album {{album.title}} @@ -24,29 +24,31 @@ p {{header.make_header(session=session)}}
-

{{album["title"]}}

-

{{album["description"]}}

- {% set parent=album["parent"] %} +

{{album.title}}

+

{{album.description}}

+ {% set parent=album.parent() %} {% if parent %} -

Parent: {{parent["id"] + " " + parent.title}}

+

Parent: {{parent.id + " " + parent.title}}

{% else %}

Parent: Albums

{% endif %} - {% if album["sub_albums"] %} + {% set sub_albums = album.children() %} + {% if sub_albums %}

Sub-albums

{% endif %} - (download .tar) + (download .tar) + {% set photos = album.photos() %} {% if photos %}

Photos

    @@ -62,7 +64,7 @@ p diff --git a/templates/albums.html b/templates/albums.html index 839ddec..bfb8677 100644 --- a/templates/albums.html +++ b/templates/albums.html @@ -19,12 +19,12 @@ {{header.make_header(session=session)}}
    {% 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 %} - + {% endfor %}
    diff --git a/templates/photo.html b/templates/photo.html index 1ac346f..d9eb689 100644 --- a/templates/photo.html +++ b/templates/photo.html @@ -6,9 +6,9 @@ - {% set filename = photo["id"] + "." + photo["extension"] %} + {% set filename = photo.id + "." + photo.extension %} {% set link = "/file/" + filename %} - {% set mimetype=photo["mimetype"] %} + {% set mimetype=photo.mimetype() %}