diff options
author | François Schmidts <francois.schmidts@gmail.com> | 2015-03-01 14:08:02 +0100 |
---|---|---|
committer | François Schmidts <francois.schmidts@gmail.com> | 2015-03-03 22:23:47 +0100 |
commit | a4fb151ea53d8054cc8e3fb309395c8fa0e23aaf (patch) | |
tree | 6ce8c39978c83d22d2508da67f00c90232819855 /pyaggr3g470r | |
parent | new crawler with cache control and error handling (diff) | |
download | newspipe-a4fb151ea53d8054cc8e3fb309395c8fa0e23aaf.tar.gz newspipe-a4fb151ea53d8054cc8e3fb309395c8fa0e23aaf.tar.bz2 newspipe-a4fb151ea53d8054cc8e3fb309395c8fa0e23aaf.zip |
fixing/restoring logging level
Diffstat (limited to 'pyaggr3g470r')
-rw-r--r-- | pyaggr3g470r/decorators.py | 13 | ||||
-rwxr-xr-x | pyaggr3g470r/lib/client.py | 16 | ||||
-rw-r--r-- | pyaggr3g470r/lib/crawler.py | 7 | ||||
-rw-r--r-- | pyaggr3g470r/lib/exceptions.py | 13 | ||||
-rw-r--r-- | pyaggr3g470r/search.py | 2 | ||||
-rw-r--r-- | pyaggr3g470r/static/js/articles.js | 8 | ||||
-rw-r--r-- | pyaggr3g470r/templates/layout.html | 10 | ||||
-rw-r--r-- | pyaggr3g470r/views/api/common.py | 33 | ||||
-rw-r--r-- | pyaggr3g470r/views/article.py | 2 | ||||
-rw-r--r-- | pyaggr3g470r/views/feed.py | 6 |
10 files changed, 33 insertions, 77 deletions
diff --git a/pyaggr3g470r/decorators.py b/pyaggr3g470r/decorators.py index 9bae626d..9e8f9c0b 100644 --- a/pyaggr3g470r/decorators.py +++ b/pyaggr3g470r/decorators.py @@ -9,7 +9,6 @@ from flask.ext.babel import gettext from flask.ext.login import login_required from pyaggr3g470r.models import Feed -from pyaggr3g470r.lib.exceptions import PyAggError def async(f): @@ -43,20 +42,8 @@ def feed_access_required(func): return decorated -def handle_pyagg_error(func): - @wraps(func) - def wrapper(*args, **kwargs): - try: - return func(*args, **kwargs) - except PyAggError as error: - flash(gettext(error.default_message), 'warning') - return redirect(url_for('home')) - return wrapper - - def pyagg_default_decorator(func): @login_required - @handle_pyagg_error @wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) diff --git a/pyaggr3g470r/lib/client.py b/pyaggr3g470r/lib/client.py deleted file mode 100755 index 6b2fc9ae..00000000 --- a/pyaggr3g470r/lib/client.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -import json -import requests -import conf - - -def get_client(email, password): - client = requests.session() - client.get(conf.PLATFORM_URL + 'api/csrf', verify=False, - data=json.dumps({'email': email, - 'password': password})) - return client - - -def get_articles(client): - return client.get(conf.PLATFORM_URL + 'api/v1.0/articles/').json diff --git a/pyaggr3g470r/lib/crawler.py b/pyaggr3g470r/lib/crawler.py index 6697e4c3..de770934 100644 --- a/pyaggr3g470r/lib/crawler.py +++ b/pyaggr3g470r/lib/crawler.py @@ -10,6 +10,7 @@ from requests_futures.sessions import FuturesSession from pyaggr3g470r.lib.utils import default_handler logger = logging.getLogger(__name__) +API_ROOT = "api/v2.0/" def extract_id(entry, keys=[('link', 'link'), @@ -52,7 +53,7 @@ class AbstractCrawler: if data is None: data = {} method = getattr(self.session, method) - return method("%sapi/v1.0/%s" % (self.url, urn), + return method("%s%s%s" % (self.url, API_ROOT, urn), auth=self.auth, data=json.dumps(data, default=default_handler), headers={'Content-Type': 'application/json'}) @@ -193,7 +194,7 @@ class CrawlerScheduler(AbstractCrawler): headers=self.prepare_headers(feed)) future.add_done_callback(FeedCrawler(feed, self.auth).callback) - def run(self): + def run(self, **kwargs): logger.debug('retreving fetchable feed') - future = self.query_pyagg('get', 'feeds/fetchable') + future = self.query_pyagg('get', 'feeds/fetchable', kwargs) future.add_done_callback(self.callback) diff --git a/pyaggr3g470r/lib/exceptions.py b/pyaggr3g470r/lib/exceptions.py deleted file mode 100644 index 30c71a5c..00000000 --- a/pyaggr3g470r/lib/exceptions.py +++ /dev/null @@ -1,13 +0,0 @@ -class PyAggError(Exception): - status_code = None - default_message = '' - - -class Forbidden(PyAggError): - status_code = 403 - default_message = 'You do not have the rights to access that resource' - - -class NotFound(PyAggError): - status_code = 404 - default_message = 'Resource was not found' diff --git a/pyaggr3g470r/search.py b/pyaggr3g470r/search.py index 89fa0860..a7f780df 100644 --- a/pyaggr3g470r/search.py +++ b/pyaggr3g470r/search.py @@ -102,7 +102,7 @@ def delete_article(user_id, feed_id, article_id): try: ix = open_dir(indexdir) except (EmptyIndexError, OSError): - raise EmptyIndexError + return writer = ix.writer() document = And([Term("user_id", user_id), Term("feed_id", feed_id), Term("article_id", article_id)]) diff --git a/pyaggr3g470r/static/js/articles.js b/pyaggr3g470r/static/js/articles.js index 51273f3d..312a5cb6 100644 --- a/pyaggr3g470r/static/js/articles.js +++ b/pyaggr3g470r/static/js/articles.js @@ -18,6 +18,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +API_ROOT = 'api/v2.0/' + if (typeof jQuery === 'undefined') { throw new Error('Requires jQuery') } +function ($) { @@ -78,7 +80,7 @@ if (typeof jQuery === 'undefined') { throw new Error('Requires jQuery') } // Encode your data as JSON. data: data, // This is the type of data you're expecting back from the server. - url: "/api/v1.0/articles/"+article_id, + url: API_ROOT + "article/" + article_id, success: function (result) { //console.log(result); }, @@ -114,7 +116,7 @@ if (typeof jQuery === 'undefined') { throw new Error('Requires jQuery') } // Encode your data as JSON. data: data, // This is the type of data you're expecting back from the server. - url: "/api/v1.0/articles/"+article_id, + url: API_ROOT + "article/" + article_id, success: function (result) { //console.log(result); }, @@ -132,7 +134,7 @@ if (typeof jQuery === 'undefined') { throw new Error('Requires jQuery') } // sends the updates to the server $.ajax({ type: 'DELETE', - url: "/api/v1.0/articles/"+article_id, + url: API_ROOT + "article/" + article_id, success: function (result) { //console.log(result); }, diff --git a/pyaggr3g470r/templates/layout.html b/pyaggr3g470r/templates/layout.html index 4dc62350..6b929bf3 100644 --- a/pyaggr3g470r/templates/layout.html +++ b/pyaggr3g470r/templates/layout.html @@ -7,9 +7,9 @@ <meta name="description" content="pyAggr3g470r is a web-based news aggregator." /> <meta name="author" content="" /> <title>{% if head_title %}{{ head_title }} - {% endif %}pyAggr3g470r</title> - <link rel="shortcut icon" href="{{ url_for('.static', filename='img/favicon.png') }}" /> + <link rel="shortcut icon" href="{{ url_for('static', filename='img/favicon.png') }}" /> <!-- Bootstrap core CSS --> - <link href="{{ url_for('.static', filename = 'css/bootstrap.css') }}" rel="stylesheet" media="screen" /> + <link href="{{ url_for('static', filename = 'css/bootstrap.css') }}" rel="stylesheet" media="screen" /> <!-- Add custom CSS here --> <style> body { @@ -155,9 +155,9 @@ <!-- Bootstrap core JavaScript --> <!-- Placed at the end of the document so the pages load faster --> - <script src="{{ url_for('.static', filename = 'js/jquery.js') }}"></script> - <script src="{{ url_for('.static', filename = 'js/bootstrap.js') }}"></script> - <script src="{{ url_for('.static', filename = 'js/articles.js') }}"></script> + <script src="{{ url_for('static', filename = 'js/jquery.js') }}"></script> + <script src="{{ url_for('static', filename = 'js/bootstrap.js') }}"></script> + <script src="{{ url_for('static', filename = 'js/articles.js') }}"></script> <script type="text/javascript" class="source"> if (window.location.href.indexOf("filter_=all") > -1){ $("#tab-all").attr('class', "active"); diff --git a/pyaggr3g470r/views/api/common.py b/pyaggr3g470r/views/api/common.py index a9d35411..a136645c 100644 --- a/pyaggr3g470r/views/api/common.py +++ b/pyaggr3g470r/views/api/common.py @@ -2,12 +2,12 @@ import json import logging import dateutil.parser from functools import wraps +from werkzeug.exceptions import Unauthorized from flask import request, g, session, Response from flask.ext.restful import Resource, reqparse from pyaggr3g470r.lib.utils import default_handler from pyaggr3g470r.models import User -from pyaggr3g470r.lib.exceptions import PyAggError logger = logging.getLogger(__name__) @@ -18,36 +18,31 @@ def authenticate(func): """ @wraps(func) def wrapper(*args, **kwargs): + logged_in = False if not getattr(func, 'authenticated', True): - return func(*args, **kwargs) - + logged_in = True # authentication based on the session (already logged on the site) - if 'email' in session or g.user.is_authenticated(): - return func(*args, **kwargs) - - # authentication via HTTP only - auth = request.authorization - try: + elif 'email' in session or g.user.is_authenticated(): + logged_in = True + else: + # authentication via HTTP only + auth = request.authorization user = User.query.filter(User.nickname == auth.username).first() if user and user.check_password(auth.password) \ and user.activation_key == "": g.user = user - except Exception: - return Response('<Authentication required>', 401, - {'WWWAuthenticate': - 'Basic realm="Login Required"'}) - return func(*args, **kwargs) + logged_in = True + + if logged_in: + return func(*args, **kwargs) + raise Unauthorized({'WWWAuthenticate': 'Basic realm="Login Required"'}) return wrapper def to_response(func): def wrapper(*args, **kwargs): status_code = 200 - try: - result = func(*args, **kwargs) - except PyAggError as error: - return Response(json.dumps(error, default=default_handler), - status=status_code) + result = func(*args, **kwargs) if isinstance(result, Response): return result elif isinstance(result, tuple): diff --git a/pyaggr3g470r/views/article.py b/pyaggr3g470r/views/article.py index 21858a33..66cc0f37 100644 --- a/pyaggr3g470r/views/article.py +++ b/pyaggr3g470r/views/article.py @@ -21,7 +21,7 @@ def articles(feed_id=None, nb_articles=-1): if len(feed.articles.all()) <= nb_articles: nb_articles = -1 if nb_articles == -1: - feed.articles = feed.article.limit(nb_articles) + feed.articles = feed.articles.limit(nb_articles) return render_template('articles.html', feed=feed, nb_articles=nb_articles) diff --git a/pyaggr3g470r/views/feed.py b/pyaggr3g470r/views/feed.py index fa9dc23d..2af502a7 100644 --- a/pyaggr3g470r/views/feed.py +++ b/pyaggr3g470r/views/feed.py @@ -28,15 +28,15 @@ def feed(feed_id=None): top_words = utils.top_words(articles, n=50, size=int(word_size)) tag_cloud = utils.tag_cloud(top_words) - today = datetime.datetime.now() + today = datetime.now() try: last_article = articles[0].date first_article = articles[-1].date delta = last_article - first_article average = round(float(len(articles)) / abs(delta.days), 2) except: - last_article = datetime.datetime.fromtimestamp(0) - first_article = datetime.datetime.fromtimestamp(0) + last_article = datetime.fromtimestamp(0) + first_article = datetime.fromtimestamp(0) delta = last_article - first_article average = 0 elapsed = today - last_article |