From b343dc73e5ea4aaf1314b6b277c3806f15ac0635 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Wed, 8 Apr 2015 12:33:40 +0200 Subject: moving feed views related code in views.feed and massive use of url_for --- bootstrap.py | 9 +- conf.py | 3 +- pyaggr3g470r/controllers/abstract.py | 9 +- pyaggr3g470r/templates/about.html | 2 +- pyaggr3g470r/templates/admin/user.html | 6 +- pyaggr3g470r/templates/feed.html | 4 +- pyaggr3g470r/templates/feeds.html | 8 +- pyaggr3g470r/templates/home.html | 18 ++-- pyaggr3g470r/templates/layout.html | 4 +- pyaggr3g470r/templates/management.html | 2 +- pyaggr3g470r/templates/unread.html | 4 +- pyaggr3g470r/views/feed.py | 149 ++++++++++++++++++++++++++++++--- pyaggr3g470r/views/views.py | 80 +----------------- runserver.py | 6 +- 14 files changed, 182 insertions(+), 122 deletions(-) diff --git a/bootstrap.py b/bootstrap.py index 3b76d0ac..9c2ce049 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -6,6 +6,7 @@ import os import conf import logging +from urllib.parse import urlsplit def set_logging(log_path, log_level=logging.INFO, log_format='%(asctime)s %(levelname)s %(message)s'): @@ -22,8 +23,12 @@ from flask.ext.sqlalchemy import SQLAlchemy # Create Flask application application = Flask('pyaggr3g470r') application.debug = conf.WEBSERVER_DEBUG -set_logging(conf.LOG_PATH, log_level=logging.DEBUG if conf.WEBSERVER_DEBUG - else logging.INFO) +scheme, domain, _, _, _ = urlsplit(conf.PLATFORM_URL) +application.config['SERVER_NAME'] = domain +application.config['PREFERRED_URL_SCHEME'] = scheme + +set_logging(conf.LOG_PATH, + log_level=logging.DEBUG if conf.WEBSERVER_DEBUG else logging.INFO) # Create dummy secrey key so we can use sessions application.config['SECRET_KEY'] = getattr(conf, 'WEBSERVER_SECRET', None) diff --git a/conf.py b/conf.py index 0d11cae7..3673850a 100644 --- a/conf.py +++ b/conf.py @@ -66,7 +66,8 @@ if not ON_HEROKU: WEBZINE_ROOT = PATH + "/pyaggr3g470r/var/export/" else: - PLATFORM_URL = os.environ.get('PLATFORM_URL', 'https://pyaggr3g470r.herokuapp.com/') + PLATFORM_URL = os.environ.get('PLATFORM_URL', + 'https://pyaggr3g470r.herokuapp.com/') ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL', '') RECAPTCHA_PUBLIC_KEY = os.environ.get('RECAPTCHA_PUBLIC_KEY', '') RECAPTCHA_PRIVATE_KEY = os.environ.get('RECAPTCHA_PRIVATE_KEY', '') diff --git a/pyaggr3g470r/controllers/abstract.py b/pyaggr3g470r/controllers/abstract.py index f1173817..8f0a8e3f 100644 --- a/pyaggr3g470r/controllers/abstract.py +++ b/pyaggr3g470r/controllers/abstract.py @@ -56,8 +56,8 @@ class AbstractController(object): if not obj: raise NotFound({'message': 'No %r (%r)' % (self._db_cls.__class__.__name__, filters)}) - if self.user_id is not None \ - and getattr(obj, self._user_id_key) != self.user_id: + + if not self._has_right_on(obj): raise Forbidden({'message': 'No authorized to access %r (%r)' % (self._db_cls.__class__.__name__, filters)}) return obj @@ -84,3 +84,8 @@ class AbstractController(object): db.session.delete(obj) db.session.commit() return obj + + def _has_right_on(self, obj): + # user_id == None is like being admin + return self.user_id is None \ + or getattr(obj, self._user_id_key, None) == self.user_id diff --git a/pyaggr3g470r/templates/about.html b/pyaggr3g470r/templates/about.html index 08b80fbb..901b3b35 100644 --- a/pyaggr3g470r/templates/about.html +++ b/pyaggr3g470r/templates/about.html @@ -17,7 +17,7 @@

{{ _('Help') }}

{{ _('If you have any problem, contact the administrator.') }}

{{ _('The documentation of the RESTful API is here.') }}

-

{{ _('You can subscribe to new feeds with a bookmarklet. Drag this link to your browser bookmarks.', bookmarklet='javascript:window.location="https://pyaggr3g470r.herokuapp.com/bookmarklet?url="+encodeURIComponent(document.location)') }}

+

{{ _('You can subscribe to new feeds with a bookmarklet. Drag this link to your browser bookmarks.', bookmarklet='javascript:window.location="%s?url="+encodeURIComponent(document.location)' % url_for('feed.bookmarklet', _external=True)) }}

{{ _('Donation') }}

diff --git a/pyaggr3g470r/templates/admin/user.html b/pyaggr3g470r/templates/admin/user.html index f20d53dd..317fef49 100644 --- a/pyaggr3g470r/templates/admin/user.html +++ b/pyaggr3g470r/templates/admin/user.html @@ -39,9 +39,9 @@ {{ feed.site_link }} {{ feed.articles.all()|count }} - - - + + + {% endfor %} diff --git a/pyaggr3g470r/templates/feed.html b/pyaggr3g470r/templates/feed.html index 268cbf7d..c0c92279 100644 --- a/pyaggr3g470r/templates/feed.html +++ b/pyaggr3g470r/templates/feed.html @@ -4,8 +4,8 @@

{{ feed.title }}

{% if feed.description %}

{{ feed.description }}

{% endif %} - - + +

diff --git a/pyaggr3g470r/templates/feeds.html b/pyaggr3g470r/templates/feeds.html index 460ae4a2..9c57bf42 100644 --- a/pyaggr3g470r/templates/feeds.html +++ b/pyaggr3g470r/templates/feeds.html @@ -1,7 +1,7 @@ {% extends "layout.html" %} {% block content %}

-

{{ _('You are subscribed to') }} {{ feeds.count() }} {{ _('feeds') }} · {{ _('Add a') }} {{ _('feed') }}

+

{{ _('You are subscribed to') }} {{ feeds.count() }} {{ _('feeds') }} · {{ _('Add a') }} {{ _('feed') }}

@@ -25,14 +25,14 @@ {% endif %} - + {% endfor %} diff --git a/pyaggr3g470r/templates/home.html b/pyaggr3g470r/templates/home.html index c20cacfa..390ac5f6 100644 --- a/pyaggr3g470r/templates/home.html +++ b/pyaggr3g470r/templates/home.html @@ -3,7 +3,7 @@ {% if feeds|count == 0 %}

{{ _("You don't have any feeds.") }}

-

{{ _('Add some') }}, {{ _('or') }} {{ _('upload an OPML file.') }}

+

{{ _('Add some') }}, {{ _('or') }} {{ _('upload an OPML file.') }}

{% else %}
{{ feed.title }}{{ feed.title }} {{ feed.site_link }} {{ feed.articles.count() }} - + - +