Skip to content

Commit

Permalink
Allow adding/deleting entries in the web app for #239.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Oct 27, 2021
1 parent e1dba1b commit 8b8b344
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 10 deletions.
48 changes: 43 additions & 5 deletions src/reader/_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,21 @@ def preview():
)


@blueprint.route('/add-entry')
def add_entry():
reader = get_reader()

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

return render_template(
'add_entry.html',
feed=feed,
)


@blueprint.route('/feeds')
def feeds():
broken = request.args.get('broken')
Expand Down Expand Up @@ -458,10 +473,10 @@ def readererror_to_apierror(*args):
yield
except ReaderError as e:
category = None
if hasattr(e, 'url'):
category = (e.url,)
if hasattr(e, 'id'):
category += (e.id,)
if hasattr(e, 'object_id'):
category = e.object_id
if not isinstance(category, tuple):
category = (category,)
raise APIError(str(e), category) from e


Expand Down Expand Up @@ -574,7 +589,7 @@ def update_metadata(data):
get_reader().set_feed_metadata_item(feed_url, key, value)


@form_api
@form_api(really=True)
@readererror_to_apierror()
def delete_metadata(data):
feed_url = data['feed-url']
Expand Down Expand Up @@ -631,6 +646,29 @@ def update_feed(data):
get_reader().update_feed(feed_url)


@form_api(name='add-entry')
@readererror_to_apierror()
def add_entry_action(data):
feed_url = data['feed-url']
link = data['link'].strip()
title = data['title'].strip() or None
if not link:
raise APIError(f"invalid link: {link}", (feed_url,))

reader = get_reader()

# TODO: if we ever add other stuff, we should catch the various AttributeError/ValueError/TypeError from the conversion here
reader.add_entry(dict(feed_url=feed_url, id=link, link=link, title=title))


@form_api(really=True)
@readererror_to_apierror()
def delete_entry(data):
feed_url = data['feed-url']
entry_id = data['entry-id']
get_reader().delete_entry((feed_url, entry_id))


# for some reason, @blueprint.app_template_global does not work
@blueprint.app_template_global()
def additional_enclosure_links(enclosure, entry):
Expand Down
8 changes: 5 additions & 3 deletions src/reader/_app/api_thing.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ def dispatch(self):
return self.dispatch_json()
return "bad content type", 400

def __call__(self, func=None, *, really=False):
def register(f):
self.actions[f.__name__.replace('_', '-')] = f
def __call__(self, func=None, *, name=None, really=False):
def register(f, name=name):
if name is None:
name = f.__name__.replace('_', '-')
self.actions[name] = f
self.really[f] = really
return f

Expand Down
42 changes: 42 additions & 0 deletions src/reader/_app/templates/add_entry.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% extends "layout.html" %}

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


{% block page_title %}Add entry to {{ macros.feed_title(feed) }}{% endblock %}
{% block main_title %}Add entry to <b>{{ macros.feed_title(feed) }}</b>{% endblock %}


{% block body %}

<div>


<form action="{{ url_for('.form_api') }}" method="post">

<p><input type="text" name="link" placeholder="link" autocomplete="off">
<p><input type="text" name="title" placeholder="title" autocomplete="off">
<p><button type="submit" name="action" value="add-entry" autocomplete="off">add</button>

<input type="hidden" name='feed-url' value='{{ feed.url }}'>

{# TODO: maybe redirect to entry page? how? – we don't know the entry url yet #}
<input type="hidden" name='next' value='{{ url_for('.entries', feed=feed.url) }}'>

</form>


<ul class="controls">

{% for message in get_flashed_messages_by_prefix(
('add-entry', feed.url),
) %}
<li class="error">{{ message }}
{% endfor %}

</ul>


</div>

{% endblock %}
10 changes: 9 additions & 1 deletion src/reader/_app/templates/entries.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@
<li>
<a href="{{ url_for('.metadata', feed=feed.url) }}">update metadata</a>

<li>
<a href="{{ url_for('.add_entry', feed=feed.url) }}">add entry</a>

{# <!-- TODO: how do we redirect to the new feed after url update? --> #}
{{ macros.text_confirm_button('.form_api', 'change-feed-url', 'change feed URL', 'new-feed-url', 'new URL', leave_disabled=true, next=url_for('.entries'), context={'feed-url': feed.url}) }}

Expand Down Expand Up @@ -260,7 +263,7 @@ <h2><a href="{{ entry.link }}">
<li>
<a href="{{ url_for('.entries', feed=entry.feed.url) }}">{{ macros.feed_title(entry.feed) }}</a>
<li>
{%- set published = entry.published or entry.updated -%}
{%- set published = entry.published or entry.updated_not_none -%}
<span title="{{ published }}">{{ published | humanize_naturaltime }}</span>


Expand All @@ -285,6 +288,10 @@ <h2><a href="{{ entry.link }}">
{{ macros.simple_button('.form_api', 'mark-as-dont-care', "don't care", leave_disabled=true, next=next, context=context) }}
{% endif %}

{% if entry.added_by == 'user' %}
{{ macros.confirm_button('.form_api', 'delete-entry', 'delete', leave_disabled=true, next=url_for('.entries', **request.args), context=context) }}
{% endif %}


{% endif %} {# not read only #}

Expand All @@ -295,6 +302,7 @@ <h2><a href="{{ entry.link }}">
('mark-as-important', feed.url, entry.id),
('mark-as-unimportant', feed.url, entry.id),
('mark-as-dont-care', feed.url, entry.id),
('delete-entry', feed.url, entry.id),
) %}
<li class="error">{{ message }}
{% endfor %}
Expand Down
7 changes: 6 additions & 1 deletion src/reader/_app/templates/entry.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{% if entry.author %} by {{ entry.author }}{% endif %}
in <a href="{{ url_for('.entries', feed=entry.feed.url) }}">{{ macros.feed_title(entry.feed) }}</a>
<li>
{%- set published = entry.published or entry.updated -%}
{%- set published = entry.published or entry.updated_not_none -%}
<span title="{{ published }}">{{ published | humanize_naturaltime }}</span>

{% set next = url_for('.entry', **request.args) %}
Expand All @@ -40,13 +40,18 @@
{{ macros.simple_button('.form_api', 'mark-as-dont-care', "don't care", leave_disabled=true, next=next, context=context) }}
{% endif %}

{% if entry.added_by == 'user' %}
{{ macros.confirm_button('.form_api', 'delete-entry', 'delete', leave_disabled=true, next=url_for('.entries', **request.args), context=context) }}
{% endif %}


{% for message in get_flashed_messages_by_prefix(
('mark-as-read', feed.url, entry.id),
('mark-as-unread', feed.url, entry.id),
('mark-as-important', feed.url, entry.id),
('mark-as-unimportant', feed.url, entry.id),
('mark-as-dont-care', feed.url, entry.id),
('delete-entry', feed.url, entry.id),
) %}
<li class="error">{{ message }}
{% endfor %}
Expand Down

0 comments on commit 8b8b344

Please sign in to comment.