diff --git a/app/__init__.py b/app/__init__.py index 9d6a2f9f85fa94497235775b7835ecfb32f5a70b..dc103fa2f517a1a3731ecb86483afcd5e3ad0cbd 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__) @@ -22,22 +24,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 app.lib import database, red, exceptions, service, session, auth0 + services = (database, red, exceptions, service, session, auth0) + for service in services: + service.register(app) - from .lib import session - session.register(app) + # with app.app_context(): + # database.db.create_all() return app diff --git a/app/controllers/base.py b/app/controllers/base.py index 8d5ad7a4e14950a5c4592ec4ac490d6aee659d9b..cf86692dd25dcda3e91e3b7f0defdc7a89b4c12b 100644 --- a/app/controllers/base.py +++ b/app/controllers/base.py @@ -1,127 +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 import db, redis, auth0_ -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)) - 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 + auth0 = auth0_ diff --git a/app/controllers/bgroup.py b/app/controllers/bgroup.py new file mode 100644 index 0000000000000000000000000000000000000000..8bcfd815c207755cc8ba62625d1964e2c36011d9 --- /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 building group controller.""" + Model = BGroup + + def get_form(self, filter_data): + """ + Get the WTForms form for the model. + """ + return BGroupForm diff --git a/app/forms/base.py b/app/forms/base.py index 0ef9f496e458521180b7e8cc18afb3e29b8b2253..05dee303f059c2581741ea14446f09ab5a8860bf 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/forms/bgroup.py b/app/forms/bgroup.py new file mode 100644 index 0000000000000000000000000000000000000000..44e04f7c569f46b396f6c9856469b033d8e2da22 --- /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/lib/__init__.py b/app/lib/__init__.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..3fac2006f832124ac650884f896339cd69d51619 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 0000000000000000000000000000000000000000..3014a0a877acf02778d047e4944312048c488128 --- /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) diff --git a/app/lib/red.py b/app/lib/red.py index 43c02faf1d524b8b363f3be2c208604a3f8daaa8..2a556155c5c276ab349bb58fc3e3512eeadc7790 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() diff --git a/app/models/base.py b/app/models/base.py index 886367d7a7f76c004f84e6ab592485519d0cfd8d..290243c4a881f99b705599485b0dc714e1ca1a3d 100644 --- a/app/models/base.py +++ b/app/models/base.py @@ -64,6 +64,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()) diff --git a/app/models/bgroup.py b/app/models/bgroup.py new file mode 100644 index 0000000000000000000000000000000000000000..d1bf0b967985039804042c7a68b51e4ea4304472 --- /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/permissions/auth.py b/app/permissions/auth.py index fb6bfbd95db87013f9f0278b67761024044bbd1b..de953107ad8d8e983a530cff360bdc6a9d95128c 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: diff --git a/app/views/__init__.py b/app/views/__init__.py index 3afb4b15076e9ab2cfe79a9ef2acee7bda871c3f..6d0df26a202567f8e985bb1359200b9b683c0912 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/base.py b/app/views/base.py index 1a175c57e9aaae6e9b01a3da64eeecfb763279b4..4700da8cccec2b0ca3e7b047f29277ac4361303d 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,) diff --git a/app/views/bgroup.py b/app/views/bgroup.py new file mode 100644 index 0000000000000000000000000000000000000000..a9174a8e2830563dfe787347a41fab3626cd29dd --- /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() diff --git a/app/views/building.py b/app/views/building.py index 15fc763c9fefb9a8a564eddb4fa32c4a16911a2e..529d939b5629f233aae687a94cccae3fb86b842c 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 diff --git a/requirements-dev.txt b/requirements-dev.txt index 5660f022283738e06640ee95df95506320277c59..2748f38cc5c9e3ba6e533abbf10af2f473d9aca9 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/requirements.txt b/requirements.txt index 1d372b124cd347d5d8c7b9db5a9559179068560e..56b3e1c977f812659b0bd388adf4e36067ec6426 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ beautifulsoup4==4.4.1 blessed==1.9.5 boto3==1.4.4 botocore==1.5.48 +git+ssh://git@github.com/Blocp/bpvalve.git@v1.1.0 cement==2.4.0 colorama==0.3.3 docker-py==1.1.0 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000000000000000000000000000000000000..64a0dfb03c1ac2943c7e7bc75475d5d685df4f9f --- /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