diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2016-04-19 22:10:52 +0200 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2016-04-19 22:10:52 +0200 |
commit | 8f4418c2b0b1027032594bf8d02e68bcc0ad6316 (patch) | |
tree | de8b1a3127a105997023a818939d26b75e783bc9 /src/web/views | |
parent | Added Flask-Restless dependency. (diff) | |
download | newspipe-8f4418c2b0b1027032594bf8d02e68bcc0ad6316.tar.gz newspipe-8f4418c2b0b1027032594bf8d02e68bcc0ad6316.tar.bz2 newspipe-8f4418c2b0b1027032594bf8d02e68bcc0ad6316.zip |
Added a blueprint for the Flask-Restless feed api.
Diffstat (limited to 'src/web/views')
-rw-r--r-- | src/web/views/api/v3/__init__.py | 4 | ||||
-rw-r--r-- | src/web/views/api/v3/article.py | 21 | ||||
-rw-r--r-- | src/web/views/api/v3/common.py | 52 | ||||
-rw-r--r-- | src/web/views/api/v3/feed.py | 30 |
4 files changed, 73 insertions, 34 deletions
diff --git a/src/web/views/api/v3/__init__.py b/src/web/views/api/v3/__init__.py index 76aa1f19..04dd28ad 100644 --- a/src/web/views/api/v3/__init__.py +++ b/src/web/views/api/v3/__init__.py @@ -1,3 +1,3 @@ -from web.views.api.v3 import article +from web.views.api.v3 import article, feed -__all__ = ['article'] +__all__ = ['article', 'feed'] diff --git a/src/web/views/api/v3/article.py b/src/web/views/api/v3/article.py index ebd15d24..1f6e757a 100644 --- a/src/web/views/api/v3/article.py +++ b/src/web/views/api/v3/article.py @@ -1,14 +1,29 @@ +from flask.ext.login import current_user from web import models from bootstrap import application, manager +from web.controllers import ArticleController +from web.views.api.v3.common import AbstractProcessor from web.views.api.v3.common import url_prefix, auth_func -from web.views.api.v3.common import get_single_preprocessor, get_many_preprocessor +class ArticleProcessor(AbstractProcessor): + def get_single_preprocessor(self, instance_id=None, **kw): + # Check if the user is authorized to modify the specified + # instance of the model. + contr = ArticleController(current_user.id) + article = contr.get(id=instance_id) + if not self.is_authorized_to_modify(current_user, article): + raise ProcessingException(description='Not Authorized', code=401) + + +article_processor = ArticleProcessor() blueprint_article = manager.create_api_blueprint(models.Article, url_prefix=url_prefix, methods=['GET', 'POST', 'PUT', 'DELETE'], - preprocessors=dict(GET_SINGLE=[auth_func, get_single_preprocessor], - GET_MANY=[auth_func, get_many_preprocessor], + preprocessors=dict(GET_SINGLE=[auth_func, + article_processor.get_single_preprocessor], + GET_MANY=[auth_func, + article_processor.get_many_preprocessor], PUT_SINGLE=[auth_func], POST=[auth_func], DELETE=[auth_func])) diff --git a/src/web/views/api/v3/common.py b/src/web/views/api/v3/common.py index 84e1f104..9ff7d96e 100644 --- a/src/web/views/api/v3/common.py +++ b/src/web/views/api/v3/common.py @@ -1,38 +1,32 @@ from flask.ext.login import current_user from flask.ext.restless import ProcessingException -from web.controllers import ArticleController - url_prefix = '/api/v3' - -def is_authorized_to_modify(user, obj): - return user.id == obj.user_id - def auth_func(*args, **kw): if not current_user.is_authenticated: raise ProcessingException(description='Not authenticated!', code=401) -def get_single_preprocessor(instance_id=None, **kw): - # Check if the user is authorized to modify the specified - # instance of the model. - contr = ArticleController(current_user.id) - article = contr.get(id=instance_id) - if not is_authorized_to_modify(current_user, article): - raise ProcessingException(description='Not Authorized', - code=401) - -def get_many_preprocessor(search_params=None, **kw): - """Accepts a single argument, `search_params`, which is a dictionary - containing the search parameters for the request. - - """ - filt = dict(name="user_id", - op="eq", - val=current_user.id) - - # Check if there are any filters there already. - if "filters" not in search_params: - search_params["filters"] = [] - - search_params["filters"].append(filt) +class AbstractProcessor(): + + def is_authorized_to_modify(self, user, obj): + return user.id == obj.user_id + + def get_single_preprocessor(self, instance_id=None, **kw): + # Check if the user is authorized to modify the specified + # instance of the model. + pass + + def get_many_preprocessor(self, search_params=None, **kw): + """Accepts a single argument, `search_params`, which is a dictionary + containing the search parameters for the request. + """ + filt = dict(name="user_id", + op="eq", + val=current_user.id) + + # Check if there are any filters there already. + if "filters" not in search_params: + search_params["filters"] = [] + + search_params["filters"].append(filt) diff --git a/src/web/views/api/v3/feed.py b/src/web/views/api/v3/feed.py new file mode 100644 index 00000000..ef1b415f --- /dev/null +++ b/src/web/views/api/v3/feed.py @@ -0,0 +1,30 @@ +from flask.ext.login import current_user +from web import models +from bootstrap import application, manager +from web.controllers import FeedController +from web.views.api.v3.common import AbstractProcessor +from web.views.api.v3.common import url_prefix, auth_func + +class FeedProcessor(AbstractProcessor): + def get_single_preprocessor(self, instance_id=None, **kw): + # Check if the user is authorized to modify the specified + # instance of the model. + contr = FeedController(current_user.id) + feed = contr.get(id=instance_id) + if not self.is_authorized_to_modify(current_user, feed): + raise ProcessingException(description='Not Authorized', code=401) + + +feed_processor = FeedProcessor() + +blueprint_feed = manager.create_api_blueprint(models.Feed, + url_prefix=url_prefix, + methods=['GET', 'POST', 'PUT', 'DELETE'], + preprocessors=dict(GET_SINGLE=[auth_func, + feed_processor.get_single_preprocessor], + GET_MANY=[auth_func, + feed_processor.get_many_preprocessor], + PUT_SINGLE=[auth_func], + POST=[auth_func], + DELETE=[auth_func])) +application.register_blueprint(blueprint_feed) |