From 6cc4bfdf8d67029b5038693dc99c27e669d0234c Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Fri, 1 Nov 2013 15:33:36 +0100 Subject: It is now possible to add a feed via a form for different users. --- pyaggr3g470r/__init__.py | 11 ++++++----- pyaggr3g470r/feedgetter.py | 14 +++++++------- pyaggr3g470r/forms.py | 16 +++++++++++++++- pyaggr3g470r/models.py | 18 +++++++++++++++--- pyaggr3g470r/templates/management.html | 18 ++++++++++++++++++ pyaggr3g470r/views.py | 27 ++++++++++++++++++++++----- 6 files changed, 83 insertions(+), 21 deletions(-) (limited to 'pyaggr3g470r') diff --git a/pyaggr3g470r/__init__.py b/pyaggr3g470r/__init__.py index a26135ac..4aa21687 100644 --- a/pyaggr3g470r/__init__.py +++ b/pyaggr3g470r/__init__.py @@ -5,7 +5,6 @@ import os from flask import Flask, session, g from flask.ext.mongoengine import MongoEngine -from flask.ext.login import LoginManager, AnonymousUserMixin from flask.ext.admin import Admin from flask.ext.admin.contrib.mongoengine import ModelView @@ -38,9 +37,11 @@ mail.init_app(app) admin = Admin(app, name='pyAggr3g470r') # Add administrative views here class UserView(ModelView): - column_filters = ['firstname', 'lastname'] - - column_searchable_list = ('firstname', 'lastname') + form_subdocuments = { + 'feeds': { + 'form_columns': ('title', 'link') + } + } class FeedView(ModelView): column_filters = ['title', 'link'] @@ -49,9 +50,9 @@ class ArticleView(ModelView): column_filters = ['title', 'link'] column_searchable_list = ('title', 'link') +#admin.add_view(UserView(User)) admin.add_view(FeedView(Feed)) admin.add_view(ArticleView(Article)) -#admin.add_view(UserView(User)) from pyaggr3g470r import views \ No newline at end of file diff --git a/pyaggr3g470r/feedgetter.py b/pyaggr3g470r/feedgetter.py index bae0cb8b..2b0c08f9 100644 --- a/pyaggr3g470r/feedgetter.py +++ b/pyaggr3g470r/feedgetter.py @@ -50,23 +50,23 @@ class FeedGetter(object): This class uses feedparser module from Mark Pilgrim. For each feed a new thread is launched. """ - def __init__(self): + def __init__(self, email): """ Initializes the database connection. """ #feedparser.USER_AGENT = conf.USER_AGENT feedparser.USER_AGENT = "pyAggr3g470r" + self.user = models.User.objects(email=email).first() def retrieve_feed(self): """ Parse the file 'feeds.lst' and launch a thread for each RSS feed. """ - feeds = models.Feed.objects() - for feed in feeds: + for feed in self.user.feeds: try: # launch a new thread for the RSS feed thread = threading.Thread(None, self.process, \ - None, (feed,)) + None, (feed, )) thread.start() list_of_threads.append(thread) except: @@ -88,8 +88,7 @@ class FeedGetter(object): articles = [] for article in a_feed['entries']: - exist = models.Article.objects(link=article.link).first() - if exist != None: + if article.link in [farticle.link for farticle in feed.articles]: continue description = "" @@ -137,7 +136,8 @@ class FeedGetter(object): """ feed.articles.extend(articles) feed.articles = sorted(feed.articles, key=lambda t: t.date, reverse=True) - feed.save() + #feed.save() + self.user.save() if __name__ == "__main__": diff --git a/pyaggr3g470r/forms.py b/pyaggr3g470r/forms.py index c7054b85..45d88e7f 100644 --- a/pyaggr3g470r/forms.py +++ b/pyaggr3g470r/forms.py @@ -23,4 +23,18 @@ class SigninForm(Form): return True else: self.email.errors.append("Invalid e-mail or password") - return False \ No newline at end of file + return False + +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") + + def __init__(self, *args, **kwargs): + Form.__init__(self, *args, **kwargs) + + def validate(self): + if not Form.validate(self): + return False + return True \ No newline at end of file diff --git a/pyaggr3g470r/models.py b/pyaggr3g470r/models.py index 0f7ab008..ba2730b9 100644 --- a/pyaggr3g470r/models.py +++ b/pyaggr3g470r/models.py @@ -14,7 +14,7 @@ class User(Document, UserMixin): lastname = StringField(required = True) email = EmailField(required=True, unique=True) pwdhash = StringField(required=True) - feeds = ListField(EmbeddedDocumentField('Feed')) + feeds = SortedListField(EmbeddedDocumentField('Feed'), ordering='title', reverse=True) created_at = DateTimeField(required=True, default=datetime.now) def get_id(self): @@ -35,7 +35,7 @@ class Feed(EmbeddedDocument): title = StringField(required=True) link = StringField(required=True) site_link = StringField(required=True) - mail = BooleanField() + mail = BooleanField(default=False) articles = ListField(ReferenceField('Article', dbref = False)) created_date = DateTimeField(required=True, default=datetime.now) @@ -75,7 +75,19 @@ if __name__ == "__main__": from werkzeug import generate_password_hash password = generate_password_hash("admin") user1 = User(firstname="Cédric", lastname="Bonhomme", \ - email="kimble.mandel@gmail.com", pwdhash=password) + email="kimble.mandel@gmail.com", pwdhash=generate_password_hash("admin")) + user1.save() + user2 = User(firstname="Carole", lastname="Niesel", \ + email="carole.niesel@gmail.com", pwdhash=generate_password_hash("secret")) + user2.save() + + user2 = User.objects(email="carole.niesel@gmail.com").first() + feed1user2 = Feed(title='Zaubette - une maitresse qui ouvre son cartable', \ + link='http://zaubette.eklablog.com/rss/', \ + site_link='http://zaubette.eklablog.com/', \ + mail = False) + user2.feeds.append(feed1user2) + user2.save() import mongodb diff --git a/pyaggr3g470r/templates/management.html b/pyaggr3g470r/templates/management.html index 65b589f8..ed7a5667 100644 --- a/pyaggr3g470r/templates/management.html +++ b/pyaggr3g470r/templates/management.html @@ -1,6 +1,24 @@ {% 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") }} +
+
+

Subscriptions

You are subscribed to {{ nb_feeds }} feeds.

diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index 12cff975..9c816990 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -8,7 +8,7 @@ from flask.ext.login import LoginManager, login_user, logout_user, login_require from collections import defaultdict -from forms import SigninForm +from forms import SigninForm, AddFeedForm from pyaggr3g470r import app, db @@ -74,14 +74,13 @@ def logout(): @login_required def home(): user = g.user - #feeds = models.User.objects(email=g.user.email).order_by('title').fields(slice__feeds__articles=9).first().feeds feeds = models.User.objects(email=g.user.email).fields(slice__feeds__articles=9).first().feeds return render_template('home.html', user=user, feeds=feeds) @app.route('/fetch/', methods=['GET']) @login_required def fetch(): - feed_getter = feedgetter.FeedGetter() + feed_getter = feedgetter.FeedGetter(g.user.email) feed_getter.retrieve_feed() return redirect(url_for('home')) @@ -156,8 +155,26 @@ def unread(): @app.route('/management/', methods=['GET']) @login_required def management(): + form = AddFeedForm() user = models.User.objects(email=g.user.email).first() nb_feeds = len(user.feeds) - nb_articles = sum([len(feed) for feed in user.feeds]) + nb_articles = sum([len(feed.articles) for feed in user.feeds]) nb_unread_articles = sum([len([article for article in feed.articles if not article.readed]) for feed in user.feeds]) - return render_template('management.html', nb_feeds=nb_feeds, nb_articles=nb_articles, nb_unread_articles=nb_unread_articles) \ No newline at end of file + 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']) +@login_required +def add_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) + + user.feeds.append(new_feed) + user.save() + return redirect(url_for('home')) + return render_template('management.html', form=form) \ No newline at end of file -- cgit