Make /tags.json return tag objects instead of just easybake text

This commit is contained in:
voussoir 2017-03-04 19:59:16 -08:00
parent 93b878bb3e
commit b9b1879179
3 changed files with 22 additions and 15 deletions

View file

@ -49,12 +49,14 @@ def photo(p, include_albums=True, include_tags=True):
return j
def tag(t):
def tag(t, include_synonyms=False):
j = {
'id': t.id,
'name': t.name,
'qualified_name': t.qualified_name(),
}
if include_synonyms:
j['synonyms'] = list(t.synonyms())
return j
def user(u):

View file

@ -578,19 +578,19 @@ def get_search_json():
def get_tags_core(specific_tag=None):
try:
tags = P.export_tags(photodb.tag_export_easybake, specific_tag=specific_tag)
except exceptions.NoSuchTag:
flask.abort(404, 'That tag doesnt exist')
tags = tags.split('\n')
tags = [t for t in tags if t != '']
tags = [(t, t.split('.')[-1].split('+')[0]) for t in tags]
if specific_tag is None:
tags = P.get_tags()
else:
tags = specific_tag.walk_children()
tags = list(tags)
return tags
@site.route('/tags')
@site.route('/tags/<specific_tag>')
@session_manager.give_token
def get_tags_html(specific_tag=None):
if specific_tag is not None:
specific_tag = P_tag(specific_tag, response_type='html')
tags = get_tags_core(specific_tag)
session = session_manager.get(request)
return flask.render_template('tags.html', tags=tags, session=session)
@ -599,8 +599,10 @@ def get_tags_html(specific_tag=None):
@site.route('/tags/<specific_tag>.json')
@session_manager.give_token
def get_tags_json(specific_tag=None):
if specific_tag is not None:
specific_tag = P_tag(specific_tag, response_type='json')
tags = get_tags_core(specific_tag)
tags = [t[0] for t in tags]
tags = [jsonify.tag(tag, include_synonyms=True) for tag in tags]
return jsonify.make_json_response(tags)

View file

@ -65,14 +65,17 @@ body
<div id="left">
<ul>
{% for tag in tags %}
{% set qualname = tag.qualified_name() %}
<li>
<a target="_blank" class="tag_object" href="/search?tag_musts={{tag[1]}}">{{tag[0]}}</a>
{% if "+" in tag[0] %}
<button class="remove_tag_button" onclick="delete_tag_synonym('{{tag[0]}}', receive_callback);"></button>
{% else %}
<button class="remove_tag_button" onclick="delete_tag('{{tag[0]}}', receive_callback);"></button>
{% endif %}
<a target="_blank" class="tag_object" href="/search?tag_musts={{tag[1]}}">{{qualname}}</a><!--
--><button class="remove_tag_button" onclick="delete_tag('{{tag[0]}}', receive_callback);"></button>
</li>
{% for synonym in tag.synonyms() %}
<li>
<a target="_blank" class="tag_object" href="/search?tag_musts={{tag.name}}">{{qualname + "+" + synonym}}</a>
<button class="remove_tag_button" onclick="delete_tag_synonym('{{synonym}}', receive_callback);"></button>
</li>
{% endfor %}
{% endfor %}
</ul>
</div>