Skip to content

Commit

Permalink
Allow editing entry/global tags in the web app.
Browse files Browse the repository at this point in the history
For #272.
  • Loading branch information
lemon24 committed Mar 11, 2022
1 parent cc91448 commit c87233a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Unreleased
* Remove the ``global_metadata`` experimental plugin
(superseded by global tags).

* In the web application, support editing entry and global metadata.
Fix broken delete metadata button.
Fix broken error flashing.


Version 2.9
-----------
Expand Down
48 changes: 35 additions & 13 deletions src/reader/_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,22 @@ def fancy_sort_key(data):
def metadata():
reader = get_reader()

feed_url = request.args['feed']
feed = reader.get_feed(feed_url, None)
if not feed:
abort(404)
feed = entry = None
if 'feed' in request.args:
if 'entry' not in request.args:
resource_id = request.args['feed']
feed = reader.get_feed(resource_id, None)
else:
resource_id = request.args['feed'], request.args['entry']
entry = reader.get_entry(resource_id, None)
if entry:
feed = entry.feed
if not feed:
abort(404)
else:
resource_id = ()

metadata = reader.get_tags(feed_url)
metadata = reader.get_tags(resource_id)

# Ensure flashed messages get removed from the session.
# https://github.com/lemon24/reader/issues/81
Expand All @@ -519,6 +529,7 @@ def metadata():
return stream_template(
'metadata.html',
feed=feed,
entry=entry,
metadata=metadata,
to_pretty_json=lambda t: yaml.safe_dump(t),
)
Expand Down Expand Up @@ -665,32 +676,43 @@ def update_feed_title(data):
get_reader().set_feed_user_title(feed_url, feed_title)


def _resource_id_from_data(data):
rv = ()
if 'feed-url' in data:
rv += (data['feed-url'],)
if 'entry-id' in data:
rv += (data['entry-id'],)
return rv


@form_api
@readererror_to_apierror()
def add_metadata(data):
feed_url = data['feed-url']
resource_id = _resource_id_from_data(data)
key = data['key']
get_reader().set_tag(feed_url, key, None)
get_reader().set_tag(resource_id, key, None)


@form_api
@readererror_to_apierror()
def update_metadata(data):
feed_url = data['feed-url']
resource_id = _resource_id_from_data(data)
key = data['key']
try:
value = yaml.safe_load(data['value'])
except yaml.YAMLError as e:
raise APIError("invalid JSON: {}".format(e), (feed_url, key)) from e
get_reader().set_tag(feed_url, key, value)
raise APIError("invalid JSON: {}".format(e), resource_id + (key,)) from e
get_reader().set_tag(resource_id, key, value)


@form_api(really=True)
# TODO: @form_api(really=True)
@form_api
@readererror_to_apierror()
def delete_metadata(data):
feed_url = data['feed-url']
resource_id = _resource_id_from_data(data)
print(resource_id)
key = data['key']
get_reader().delete_tag(feed_url, key)
get_reader().delete_tag(resource_id, key)


@form_api
Expand Down
3 changes: 3 additions & 0 deletions src/reader/_app/templates/entry.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
{{ macros.confirm_button('.form_api', 'delete-entry', 'delete', leave_disabled=true, next=url_for('.entries', **request.args), context=context) }}
{% endif %}

<li>
<a href="{{ url_for('.metadata', feed=entry.feed.url, entry=entry.id) }}">update metadata</a>

{% if content %}
{% set minutes = read_time(content).minutes %}
{% if minutes > 1 %}
Expand Down
1 change: 1 addition & 0 deletions src/reader/_app/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<a href="{{ url_for('reader.entries') }}">entries</a>
<a href="{{ url_for('reader.feeds') }}">feeds</a>
<a href="{{ url_for('reader.tags') }}">tags</a>
<a href="{{ url_for('reader.metadata') }}">metadata</a>

{{ macros.text_input_button_get(
'reader.preview', 'add feed', 'url', 'url',
Expand Down
49 changes: 44 additions & 5 deletions src/reader/_app/templates/metadata.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@

{% import "macros.html" as macros %}

{% block page_title %}Metadata for {{ macros.feed_title(feed) }}{% endblock %}
{% block main_title %}Metadata for {{ macros.feed_title(feed) }}{% endblock %}

{% macro make_title() %}
{% if feed %}
{% if not entry %}
Metadata for
<b><a href="{{ url_for('.entries', feed=feed.url) }}"
title="{{ macros.feed_title_secondary(feed) }}">
{{ macros.feed_title(feed) }}</a></b>
{% else %}
Metadata for
<b><a href="{{ url_for('.entry', feed=entry.feed.url, entry=entry.id) }}">
{{ entry.title or "untitled" }}</a></b>
{% endif %}
{% else %}
Global metadata
{% endif %}
{% endmacro %}

{% block page_title %}{{ make_title() | striptags }}{% endblock %}
{% block main_title %}{{ make_title() }}{% endblock %}


{% block body %}
Expand All @@ -13,14 +31,30 @@
<form action="{{ url_for('.form_api') }}" method="post">
<input type="text" name="key" placeholder="key" autocomplete="off">
<button type="submit" name="action" value="add-metadata" autocomplete="off">add</button>
{% if feed %}
<input type="hidden" name='feed-url' value='{{ feed.url }}'>
{% if entry %}
<input type="hidden" name='entry-id' value='{{ entry.id }}'>
{% endif %}
{% endif %}
<input type="hidden" name='next' value='{{ url_for('.metadata', **request.args) }}'>
</form>


<ul class="controls">

{% if feed %}
{% if entry %}
{% set resource_id = entry.object_id %}
{% else %}
{% set resource_id = (feed.url,) %}
{% endif %}
{% else %}
{% set resource_id = () %}
{% endif %}

{% for message in get_flashed_messages_by_prefix(
('add-metadata', feed.url),
('add-metadata',) + resource_id,
) %}
<li class="error">{{ message }}
{% endfor %}
Expand All @@ -38,7 +72,7 @@ <h2>{{ key or '""' }}</h2>
<ul class="controls">

{% for message in get_flashed_messages_by_prefix(
('update-metadata', feed.url, key),
('update-metadata',) + resource_id + (key,),
) %}
<li class="error">{{ message }}
{% endfor %}
Expand All @@ -52,14 +86,19 @@ <h2>{{ key or '""' }}</h2>
<button type="submit" name="action" value="delete-metadata" autocomplete="off">delete</button>
</p>
<input type="hidden" name='key' value='{{ key }}'>
{% if feed %}
<input type="hidden" name='feed-url' value='{{ feed.url }}'>
{% if entry %}
<input type="hidden" name='entry-id' value='{{ entry.id }}'>
{% endif %}
{% endif %}
<input type="hidden" name='next' value='{{ url_for('.metadata', **request.args) }}#metadata-{{ loop.index }}'>
</form>

</div>

{% else %}
<p>no metadata for this feed</p>
<p>no metadata for this resource</p>
{% endfor %}

{% endblock %}

0 comments on commit c87233a

Please sign in to comment.