From 6bbe221ede3524487b6d59debf0587a42adc91be Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Tue, 19 Sep 2017 17:20:19 -0400 Subject: [PATCH 01/15] Add limit and order_by in base view --- app/controllers/base.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/base.py b/app/controllers/base.py index 8d5ad7a..7d1e1b3 100644 --- a/app/controllers/base.py +++ b/app/controllers/base.py @@ -54,6 +54,13 @@ class RestController(object): for k, f in self.filters.items(): if k in filter_data: q = q.filter(f(filter_data)) + + if 'order_by' in filter_data: + q = q.order_by(getattr(self.Model, filter_data['order_by'])) + + if 'limit' in filter_data: + q = q.limit(int(filter_data['limit'])) + return q def get_form(self, filter_data): -- GitLab From 19d7991c2137d433535ccd8ffa7ef975574390f3 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Thu, 21 Sep 2017 14:46:59 -0400 Subject: [PATCH 02/15] Add sort param to filter --- app/controllers/base.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/base.py b/app/controllers/base.py index 7d1e1b3..546e7de 100644 --- a/app/controllers/base.py +++ b/app/controllers/base.py @@ -56,7 +56,13 @@ class RestController(object): q = q.filter(f(filter_data)) if 'order_by' in filter_data: - q = q.order_by(getattr(self.Model, filter_data['order_by'])) + order_by = getattr(self.Model, filter_data['order_by']) + sort = '' + + if 'sort' in filter_data: + sort = filter_data['sort'] + + q = q.order_by('{} {}'.format(order_by, sort)) if 'limit' in filter_data: q = q.limit(int(filter_data['limit'])) -- GitLab From 0ec524151bd3636cbf61f71448cdcb79c92b5d23 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 25 Sep 2017 17:20:39 -0400 Subject: [PATCH 03/15] Add user to flask.g --- app/permissions/auth.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/permissions/auth.py b/app/permissions/auth.py index fb6bfbd..de95310 100644 --- a/app/permissions/auth.py +++ b/app/permissions/auth.py @@ -1,5 +1,5 @@ """Permissions to check authentication.""" -from flask import current_app +from flask import current_app, g from werkzeug.exceptions import Unauthorized from jose import jwt import json @@ -49,6 +49,7 @@ class AuthNeed(Permission): audience=API_AUDIENCE, issuer="https://"+AUTH0_DOMAIN+"/" ) + g.sub = payload['sub'] # For now we will print and return unauthorized. In the future # we will log these errors and the requester except jwt.ExpiredSignatureError: -- GitLab From fcc2f863920024a48b6c2751e2c826a9e029983f Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 9 Oct 2017 12:51:56 -0400 Subject: [PATCH 04/15] Update base controller class to use bpvalve --- app/controllers/base.py | 146 +++------------------------------------- requirements.txt | 1 + 2 files changed, 10 insertions(+), 137 deletions(-) diff --git a/app/controllers/base.py b/app/controllers/base.py index 546e7de..2cdbaf1 100644 --- a/app/controllers/base.py +++ b/app/controllers/base.py @@ -1,140 +1,12 @@ -from sqlalchemy.exc import IntegrityError -from werkzeug.datastructures import MultiDict -from werkzeug.exceptions import NotFound, BadRequest -from flask import current_app +"""Flask RestController wrapper""" +from bpvalve.flask.controllers import RestController as BaseRestController +from app.lib.database import db +from app.lib.red import redis -from ..lib.database import db, commit - -class RestController(object): - """A RESTful class for manipulating database objects. - - Controllers should be request-agnostic. The can be initialized and used - outside of a request context. This provides a convenient place to dump - complicated filters or additional data manipulation that happens before - or after a database fetch. +class RestController(BaseRestController): """ - # The model associated with this REST resource. - Model = None - # A dictionary of available filters on the resource. - # - # For each key in this dictionary, if the key is present in the dictionary, - # the value associated with that key (a function taking one argument) is - # passed the filter_data dictionary and the return value is used as a - # database filter. For example, - # - # { - # 'id_': lambda d: Model.id_ == d['id_'] - # } - # - # There is no requirement that the filter function only manipulate its - # key, so these filters can be arbitrarily complex as needed. Multiline - # filters should be defined as separate functions and referenced here. - filters = {} - - # The primary key to use to retrieve the model in get/put/delete requests. - key = 'id' - - def query(self, filter_data): - """Construct a query for the model. - - Args: - filter_data - A dictionary representing the query string - parameters. This is unused at the moment. - """ - return db.session.query(self.Model) - - def filter(self, q, filter_data): - """Parse a dictionary representing the query parameters into database - filters. - - q - A SQLAlchemy query. - filter_data - A dictionary representing the query string parameters. - """ - for k, f in self.filters.items(): - if k in filter_data: - q = q.filter(f(filter_data)) - - if 'order_by' in filter_data: - order_by = getattr(self.Model, filter_data['order_by']) - sort = '' - - if 'sort' in filter_data: - sort = filter_data['sort'] - - q = q.order_by('{} {}'.format(order_by, sort)) - - if 'limit' in filter_data: - q = q.limit(int(filter_data['limit'])) - - return q - - def get_form(self, filter_data): - """Get the WTForms form for the model.""" - pass - - def index(self, filter_data): - """Get a query for all models matching filter_data.""" - q = self.query(filter_data) - q = self.filter(q, filter_data) - return q.all() - - def get(self, id_, filter_data): - """Get a single model matching an id.""" - model = db.session.query(self.Model)\ - .filter(getattr(self.Model, self.key)==id_).first() - if not model: - raise NotFound - return model - - def create(self, data, filter_data): - """Creates a model, but does not add it to the database.""" - model = self.Model() - form = self.get_form(filter_data)(formdata=MultiDict(data)) - - if not form.validate(): - raise BadRequest(form.errors) - - form.populate_obj(model) - return model - - def post(self, data, filter_data): - """Post a new model from a dictionary.""" - model = self.create(data, filter_data) - db.session.add(model) - try: - commit() - except IntegrityError as e: - raise ( - BadRequest(str(e)) if current_app.config['DEBUG'] else - BadRequest) - - return model - - def put(self, id_, data, filter_data): - """Change an existing model using an id and dictionary data.""" - model = self.get(id_, filter_data) - form = self.get_form(filter_data)(formdata=MultiDict(data)) - - if not str(form.id.data) == str(id_): - raise BadRequest(['The id in the model and the uri do not match.']) - if not form.validate(): - raise BadRequest(form.errors) - - form.populate_obj(model) - db.session.add(model) - try: - commit() - except IntegrityError as e: - raise ( - BadRequest(str(e)) if current_app.config['DEBUG'] else - BadRequest) - - return model - - def delete(self, id_, filter_data): - """Delete a model given its id.""" - model = self.get(id_, filter_data) - db.session.delete(model) - commit() - return {} + Wrapper for BaseRestController + """ + db = db + redis = redis diff --git a/requirements.txt b/requirements.txt index 0a28abd..34ee5f4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ arrow==0.7.0 blessed==1.9.5 botocore==1.3.28 +https://github.com/Blocp/bpvalve@v1.1.0 cement==2.4.0 colorama==0.3.3 docker-py==1.1.0 -- GitLab From 13c6edcbd9e798bcc15bc5a0e2bbda167f8d66fe Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 9 Oct 2017 12:58:00 -0400 Subject: [PATCH 05/15] Update dev requirements --- requirements-dev.txt | 3 +++ setup.cfg | 9 +++++++++ 2 files changed, 12 insertions(+) create mode 100644 setup.cfg diff --git a/requirements-dev.txt b/requirements-dev.txt index 5660f02..2748f38 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,6 +1,9 @@ -r requirements.txt Flask-Testing==0.4.2 nose==1.3.7 +pycodestyle>=2.3.1 +pydocstyle>=1.1.1 pytest==2.9.1 pylint>=1.6.5 pylint-flask>=0.5 +yapf>=0.16.1 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..64a0dfb --- /dev/null +++ b/setup.cfg @@ -0,0 +1,9 @@ +[pycodestyle] +max-line-length=100 + +[isort] +line_length=100 + +[yapf] +based_on_style=pep8 +spaces_before_comment=1 -- GitLab From 7a46dd4eeb327d1e842b91e8a5447cb79d433bd5 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 9 Oct 2017 12:59:31 -0400 Subject: [PATCH 06/15] Use ssh for bpvalve --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 34ee5f4..e760f52 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ arrow==0.7.0 blessed==1.9.5 botocore==1.3.28 -https://github.com/Blocp/bpvalve@v1.1.0 +git+ssh://git@github.com/Blocp/bpvalve.git@v1.1.0 cement==2.4.0 colorama==0.3.3 docker-py==1.1.0 -- GitLab From 11331ead79c97369c82e3f656e88ed4be086220b Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 9 Oct 2017 14:03:17 -0400 Subject: [PATCH 07/15] Import UnprotectedRestView from bpvalve --- app/views/base.py | 69 +++-------------------------------------------- 1 file changed, 3 insertions(+), 66 deletions(-) diff --git a/app/views/base.py b/app/views/base.py index 1a175c5..4700da8 100644 --- a/app/views/base.py +++ b/app/views/base.py @@ -1,71 +1,8 @@ -import json -from werkzeug.exceptions import NotFound -from flask import jsonify, request, current_app -from flask.ext.classy import FlaskView -from app.lib.database import db -from app.lib.red import redis +from bpvalve.flask.views import UnprotectedRestView from app.permissions.auth import standard_login_need -class View(FlaskView): - """A base view to provide convenience methods to subclasses.""" - def request_json(self): - """Return json parsed from the request.""" - return json.loads(request.data.decode('utf-8')) - - def json(self, obj, status=200): - """A wrapper for Flask's jsonify().""" - response = jsonify(data=obj) - return response, status - - def parse(self, model): - """Parse the model into a dictionary.""" - return model.get_dictionary() - - -class UnprotectedRestView(View): - """A view wrapper for RESTful controllers that does not offer API - protection. - - Views should remain agnostic to the database (with the exception of - permissions). In general, the view is responsible for parsing the - request, checking permissions, calling the controller, and parsing that - return into a json output. - """ - def get_controller(self): - """Return a controller instance to use for database interactions.""" - pass - - def index(self): - """/ GET - Retrieve a list of resources.""" - return self.json([ - self.parse(m) for m in self.get_controller().index(request.args) - ]) - - def get(self, id_): - """/{id} GET - Retrieve a resource by id.""" - return self.json(self.parse( - self.get_controller().get(id_, request.args) - )) - - def post(self): - """/ POST - Post a new resource.""" - return self.json(self.parse( - self.get_controller().post(self.request_json(), request.args) - ), 201) - - def put(self, id_): - """/{id} PUT - Modify a resource by id.""" - return self.json(self.parse( - self.get_controller().put(id_, self.request_json(), request.args) - )) - - def delete(self, id_): - """/{id} DELETE - Delete a resource by id.""" - return self.json(self.get_controller().delete(id_, request.args), 204) - - class RestView(UnprotectedRestView): - """A view wrapper for RESTful controllers that _does_ offer API protection. - """ + """A view wrapper for RESTful controllers that _does_ offer API protection.""" + decorators = (standard_login_need,) -- GitLab From 3be40b7f4236da36d4326b0625f5ccd1dab528ed Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 9 Oct 2017 18:49:44 -0400 Subject: [PATCH 08/15] Add auth0 and update init of lib and RestController imports --- app/controllers/base.py | 4 ++-- app/lib/__init__.py | 3 +++ app/lib/auth0.py | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 app/lib/auth0.py diff --git a/app/controllers/base.py b/app/controllers/base.py index 2cdbaf1..cf86692 100644 --- a/app/controllers/base.py +++ b/app/controllers/base.py @@ -1,7 +1,6 @@ """Flask RestController wrapper""" from bpvalve.flask.controllers import RestController as BaseRestController -from app.lib.database import db -from app.lib.red import redis +from app.lib import db, redis, auth0_ class RestController(BaseRestController): @@ -10,3 +9,4 @@ class RestController(BaseRestController): """ db = db redis = redis + auth0 = auth0_ diff --git a/app/lib/__init__.py b/app/lib/__init__.py index e69de29..3fac200 100644 --- a/app/lib/__init__.py +++ b/app/lib/__init__.py @@ -0,0 +1,3 @@ +from .database import db +from .red import redis +from .auth0 import auth0_ diff --git a/app/lib/auth0.py b/app/lib/auth0.py new file mode 100644 index 0000000..3014a0a --- /dev/null +++ b/app/lib/auth0.py @@ -0,0 +1,19 @@ +from bpvalve.auth0 import Auth0Wrapper + + +class FlaskAuth0: + """Initilalization class for Auth0Wrapper""" + + def init_app(self, app): + """Intialize Auth0Wrapper""" + self.auth = Auth0Wrapper(app.config.get('AUTH0_DOMAIN'), + app.config.get('AUTH0_CLIENT_ID'), + app.config.get('AUTH0_CLIENT_SECRET'), + 'https://{}/api/v2/'.format(app.config.get('AUTH0_DOMAIN'))) + +auth0_ = FlaskAuth0() + + +def register(app): + """Configure the Auth0Wrapper based on the app configuration.""" + auth0_.init_app(app) -- GitLab From 1c2cc4239442a6301f58b7a207fc8ab7a2e43642 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 9 Oct 2017 18:50:26 -0400 Subject: [PATCH 09/15] Condense registeration of flask app --- app/__init__.py | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index da6671a..cbc3607 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -22,25 +22,15 @@ def create_app(config): app.logger.info('Setting up application...') - from .lib import database - database.register(app) - - from .lib import red - red.register(app) - from . import views views.register(app) - from .lib import exceptions - exceptions.register(app) - - from .lib import service - service.register(app) - - from .lib import session - session.register(app) + from app.lib import database, red, exceptions, service, session, auth0 + services = (database, red, exceptions, service, session, auth0) + for service in services: + service.register(app) - with app.app_context(): - database.db.create_all() + # with app.app_context(): + # database.db.create_all() return app -- GitLab From 6f2794035a842f14cc1912807cebffaf9a9867cd Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 9 Oct 2017 18:53:18 -0400 Subject: [PATCH 10/15] Update package init with module docstring --- app/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index cbc3607..dc103fa 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,3 +1,4 @@ +"""BP flask microservices""" import logging from logging.handlers import RotatingFileHandler from flask import Flask @@ -6,6 +7,7 @@ MEGABYTE = 10**6 LOG_FORMAT = '[%(asctime)s] %(pathname)s:%(lineno)d %(levelname)s - %(message)s' LOG_PATH = '/var/log/flask.log' + def create_app(config): """Set up the application.""" app = Flask(__name__) -- GitLab From 3c0e43c73c97c984b29ef65abd5b587a569e3f04 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Mon, 9 Oct 2017 18:56:40 -0400 Subject: [PATCH 11/15] Import RedisWrapper from bpvalve --- app/lib/red.py | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/app/lib/red.py b/app/lib/red.py index 43c02fa..2a55615 100644 --- a/app/lib/red.py +++ b/app/lib/red.py @@ -1,32 +1,4 @@ -"""A library for interacting with redis.""" -from redis import StrictRedis -from parse import parse - - -class RedisWrapper(object): - """A wrapper for Redis.""" - def get_database(self, database): - """Gets an actual database.""" - return StrictRedis(host=self.host, port=self.port, db=database) - - def init_app(self, app): - """Stores information about the redis database from the URI.""" - uri = app.config.get('REDIS_URI') - self.host, self.port = parse('redis://{}:{}/', uri) - - # key/tokens for applications that this service serves. - self.server_apps = self.get_database(0) - # key/tokens for applications that this service consumes. - self.client_apps = self.get_database(1) - # key/tokens for users - self.users = self.get_database(2) - - def flushdb(self): - """Flushes each database.""" - self.server_apps.flushdb() - self.client_apps.flushdb() - self.users.flushdb() - +from bpvalve.flask.lib.red import RedisWrapper redis = RedisWrapper() -- GitLab From db156712a8d0ad8cb5827d3d98e4d4503a4a7c72 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Tue, 10 Oct 2017 16:04:18 -0400 Subject: [PATCH 12/15] Add user model and form --- app/forms/base.py | 5 +++++ app/models/base.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/app/forms/base.py b/app/forms/base.py index 8118e93..a886f9c 100644 --- a/app/forms/base.py +++ b/app/forms/base.py @@ -9,6 +9,11 @@ class Form(wtf.Form): id = wtf.IntegerField() +class UserForm: + user_created = wtf.StringField(validators=[wtf.validators.Length(max=64)]) + user_modified = wtf.StringField(validators=[wtf.validators.Length(max=64)]) + + class AddressForm(object): """A form for validating address information.""" street_address = wtf.StringField( diff --git a/app/models/base.py b/app/models/base.py index e304c90..c4470e2 100644 --- a/app/models/base.py +++ b/app/models/base.py @@ -59,6 +59,11 @@ class Model(BaseModel): id = db.Column(db.Integer, primary_key=True) +class User: + user_created = db.Column(db.String(64)) + user_modified = db.Column(db.String(64)) + + class Tracked(object): """A mixin to include tracking datetime fields.""" created = db.Column(columns.Arrow, default=func.now()) -- GitLab From d95bb38326c602cf4c717a7b2a41ce837aff0c50 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Tue, 10 Oct 2017 16:10:58 -0400 Subject: [PATCH 13/15] Add legacy method to buildings route --- app/views/building.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/views/building.py b/app/views/building.py index 15fc763..529d939 100644 --- a/app/views/building.py +++ b/app/views/building.py @@ -11,6 +11,11 @@ class BuildingView(RestView): """Return an instance of the building controller.""" return BuildingController() + # FIXME: Legacy method, update REST funcs + def parse(self, model): + """Parse the model into a dictionary.""" + return model.get_dictionary() + def index(self): """/ GET - Retrieve a list of resources.""" # TODO: Add data key back to self.json -- GitLab From 3af59978fb45d3fee076d4d1b6d18034efc87ad7 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Tue, 10 Oct 2017 16:19:25 -0400 Subject: [PATCH 14/15] Add building group views --- app/controllers/bgroup.py | 15 +++++++++++++++ app/forms/bgroup.py | 10 ++++++++++ app/models/bgroup.py | 12 ++++++++++++ app/views/__init__.py | 4 +++- app/views/bgroup.py | 9 +++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 app/controllers/bgroup.py create mode 100644 app/forms/bgroup.py create mode 100644 app/models/bgroup.py create mode 100644 app/views/bgroup.py diff --git a/app/controllers/bgroup.py b/app/controllers/bgroup.py new file mode 100644 index 0000000..20c7fc5 --- /dev/null +++ b/app/controllers/bgroup.py @@ -0,0 +1,15 @@ +"""Building groups controller""" +from .base import RestController +from ..forms.bgroup import BGroupForm +from ..models.bgroup import BGroup + + +class BGroupController(RestController): + """The project controller.""" + Model = BGroup + + def get_form(self, filter_data): + """ + Get the WTForms form for the model. + """ + return BGroupForm diff --git a/app/forms/bgroup.py b/app/forms/bgroup.py new file mode 100644 index 0000000..44e04f7 --- /dev/null +++ b/app/forms/bgroup.py @@ -0,0 +1,10 @@ +import wtforms as wtf +from .base import Form, UserForm + + +class BGroupForm(UserForm, Form): + + name = wtf.StringField( + validators=[wtf.validators.Required(), + wtf.validators.Length(max=64)]) + priority = wtf.IntegerField(validators=[wtf.validators.NumberRange(1, 3)]) diff --git a/app/models/bgroup.py b/app/models/bgroup.py new file mode 100644 index 0000000..d1bf0b9 --- /dev/null +++ b/app/models/bgroup.py @@ -0,0 +1,12 @@ +from .base import Model, User +from ..lib.database import db + + +class BGroup(User, Model, db.Model): + __tablename__ = 'bgroup' + + id = db.Column(db.Integer, primary_key=True, server_default=db.text("nextval('bgroup_id_seq'::regclass)")) + name = db.Column(db.String(64)) + priority = db.Column(db.Integer) # FIXME: Foregin key to group_priority + time_created = db.Column(db.DateTime, server_default=db.text("timezone('utc'::text, now())")) + time_modified = db.Column(db.DateTime) diff --git a/app/views/__init__.py b/app/views/__init__.py index 3afb4b1..6d0df26 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -1,5 +1,6 @@ """Flask-classy views for the flask application.""" -from . import (building, +from . import (bgroup, + building, turk_hit, unapproved_point, unapproved_window_door, @@ -13,6 +14,7 @@ def register(app): in the application. """ building.BuildingView.register(app) + bgroup.BGroupView.register(app) turk_hit.TurkHitView.register(app) unapproved_point.UnapprovedPointView.register(app) unapproved_window_door.UnapprovedWindowDoorView.register(app) diff --git a/app/views/bgroup.py b/app/views/bgroup.py new file mode 100644 index 0000000..a9174a8 --- /dev/null +++ b/app/views/bgroup.py @@ -0,0 +1,9 @@ +"""Building group view""" +from ..views.base import RestView +from ..controllers.bgroup import BGroupController + + +class BGroupView(RestView): + + def get_controller(self): + return BGroupController() -- GitLab From 0beddd91d96115b949c7bf617ee69cafd0c74df7 Mon Sep 17 00:00:00 2001 From: Alessandro DiMarco Date: Tue, 17 Oct 2017 16:20:58 -0400 Subject: [PATCH 15/15] Fix bgroup comment --- app/controllers/bgroup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/bgroup.py b/app/controllers/bgroup.py index 20c7fc5..8bcfd81 100644 --- a/app/controllers/bgroup.py +++ b/app/controllers/bgroup.py @@ -5,7 +5,7 @@ from ..models.bgroup import BGroup class BGroupController(RestController): - """The project controller.""" + """The building group controller.""" Model = BGroup def get_form(self, filter_data): -- GitLab