From e488df013ac33e4014bafb27417a8e87a762b3c0 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Sun, 10 Nov 2013 00:14:38 +0100 Subject: It is now possible to add and edit a feed. --- pyaggr3g470r/forms.py | 2 +- pyaggr3g470r/templates/edit_feed.html | 22 +++++++++++ pyaggr3g470r/templates/feed.html | 1 + pyaggr3g470r/templates/management.html | 18 --------- pyaggr3g470r/views.py | 70 ++++++++++++++++++++++------------ 5 files changed, 70 insertions(+), 43 deletions(-) create mode 100644 pyaggr3g470r/templates/edit_feed.html (limited to 'pyaggr3g470r') diff --git a/pyaggr3g470r/forms.py b/pyaggr3g470r/forms.py index 161d8d4c..e13dde96 100644 --- a/pyaggr3g470r/forms.py +++ b/pyaggr3g470r/forms.py @@ -54,7 +54,7 @@ class AddFeedForm(Form): title = TextField("Title", [validators.Required("Please enter a title.")]) link = TextField("Feed link", [validators.Required("Please enter a link.")]) site_link = TextField("Site link", [validators.Required("Please enter a site URL.")]) - submit = SubmitField("Add feed") + submit = SubmitField("Save") def __init__(self, *args, **kwargs): Form.__init__(self, *args, **kwargs) diff --git a/pyaggr3g470r/templates/edit_feed.html b/pyaggr3g470r/templates/edit_feed.html new file mode 100644 index 00000000..7bdf2717 --- /dev/null +++ b/pyaggr3g470r/templates/edit_feed.html @@ -0,0 +1,22 @@ +{% extends "layout.html" %} +{% block content %} +
+
+

{{ action }}

+
+ {{ form.hidden_tag() }} + + {{ form.title.label }} + {{ form.title(class_="form-control") }} {% for error in form.title.errors %} {{ error }}{% endfor %} + + {{ form.link.label }} + {{ form.link(class_="form-control") }} {% for error in form.link.errors %} {{ error }}{% endfor %} + + {{ form.site_link.label }} + {{ form.site_link(class_="form-control") }} {% for error in form.site_link.errors %} {{ error }}{% endfor %} + + {{ form.submit(class_="btn") }} +
+
+
+{% endblock %} \ No newline at end of file diff --git a/pyaggr3g470r/templates/feed.html b/pyaggr3g470r/templates/feed.html index 5808f233..e08ac159 100644 --- a/pyaggr3g470r/templates/feed.html +++ b/pyaggr3g470r/templates/feed.html @@ -4,6 +4,7 @@

{{ feed.title }}

+

This feed contains {{ feed.articles|count }} articles.

Address of the feed: {{ feed.link }}.

Address of the site: {{ feed.site_link }}.

diff --git a/pyaggr3g470r/templates/management.html b/pyaggr3g470r/templates/management.html index c1e5ba64..031e3e7e 100644 --- a/pyaggr3g470r/templates/management.html +++ b/pyaggr3g470r/templates/management.html @@ -1,24 +1,6 @@ {% extends "layout.html" %} {% block content %}
-
-

Add a feed

-
- {{ form.hidden_tag() }} - - {{ form.title.label }} - {{ form.title(class_="form-control") }} {% for error in form.title.errors %} {{ error }}{% endfor %} - - {{ form.link.label }} - {{ form.link(class_="form-control") }} {% for error in form.link.errors %} {{ error }}{% endfor %} - - {{ form.site_link.label }} - {{ form.site_link(class_="form-control") }} {% for error in form.site_link.errors %} {{ error }}{% endfor %} - - {{ form.submit(class_="btn") }} -
-
-

Your subscriptions

You are subscribed to {{ nb_feeds }} feeds.

diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index f0ed4103..6a232d0e 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -159,21 +159,6 @@ def delete(article_id=None): user.save() return redirect(url_for('home')) -@app.route('/delete_feed/', methods=['GET']) -@login_required -def delete_feed(feed_id=None): - user = models.User.objects(email=g.user.email).first() - # delete all articles (Document objects) - for feed in user.feeds: - if str(feed.oid) == feed_id: - for article in feed.articles: - article.delete() - feed.articles = [] - # delete the feed (EmbeddedDocument object) - user.feeds.remove(feed) - user.save() - return redirect(url_for('home')) - @app.route('/articles/', methods=['GET']) @login_required def articles(feed_id=None): @@ -215,19 +200,56 @@ def management(): return render_template('management.html', form=form, \ nb_feeds=nb_feeds, nb_articles=nb_articles, nb_unread_articles=nb_unread_articles) -@app.route('/add_feed/', methods=['GET', 'POST']) +@app.route('/edit_feed/', methods=['GET', 'POST']) +@app.route('/edit_feed/', methods=['GET', 'POST']) @login_required -def add_feed(): +def edit_feed(feed_id=None): + """ + Add or edit a feed. + """ user = models.User.objects(email=g.user.email).first() form = AddFeedForm() if request.method == 'POST': if form.validate() == False: - return render_template('management.html', form=form) - new_feed = models.Feed(title=form.title.data, link=form.link.data, site_link=form.site_link.data) + return render_template('edit_feed.html', form=form) + if feed_id != None: + # Edit an existing feed + for feed in user.feeds: + if str(feed.oid) == feed_id: + form.populate_obj(feed) + user.save() + flash('Feed "' + feed.title + '" successfully updated', 'success') + return redirect('/feed/'+feed_id) + else: + # Create a new feed + new_feed = models.Feed(title=form.title.data, link=form.link.data, site_link=form.site_link.data) + user.feeds.append(new_feed) + user.feeds = sorted(user.feeds, key=lambda t: t.title.lower()) + user.save() + return redirect(url_for('home')) - user.feeds.append(new_feed) - user.feeds = sorted(user.feeds, key=lambda t: t.title.lower()) - user.save() - return redirect(url_for('home')) - return render_template('management.html', form=form) \ No newline at end of file + if request.method == 'GET': + if feed_id != None: + for feed in user.feeds: + if str(feed.oid) == feed_id: + form = AddFeedForm(obj=feed) + return render_template('edit_feed.html', action="Edit the feed", form=form, feed=feed) + + # Return an empty form in order to create a new feed + return render_template('edit_feed.html', action="Add a feed", form=form) + +@app.route('/delete_feed/', methods=['GET']) +@login_required +def delete_feed(feed_id=None): + user = models.User.objects(email=g.user.email).first() + # delete all articles (Document objects) + for feed in user.feeds: + if str(feed.oid) == feed_id: + for article in feed.articles: + article.delete() + feed.articles = [] + # delete the feed (EmbeddedDocument object) + user.feeds.remove(feed) + user.save() + return redirect(url_for('home')) \ No newline at end of file -- cgit