Machine-readable docstrings

parent 0d8fe499
from flask import Flask, redirect, jsonify, abort, request, url_for, make_response, g
from webargs.flaskparser import use_args
from flask import Flask, redirect, request, url_for, make_response, g
from webargs import fields
from webargs.flaskparser import use_args
from where.model import Session, Point, Category, Field
from where.model.field_types import FieldType
from where.validation import PointSchema, CategorySchema, FieldSchema
from where.validation import PointSchema, CategorySchema, BaseSchema
app = Flask(__name__)
......@@ -13,7 +12,7 @@ app = Flask(__name__)
@app.before_request
def create_local_db_session():
g.db_session = Session()
@app.after_request
def destroy_local_db_session(resp):
......@@ -29,7 +28,6 @@ def destroy_local_db_session(resp):
@app.route('/')
def index():
print(PointSchema().Meta.model)
return """
<head>
</head>
......@@ -121,12 +119,12 @@ def test_data():
@app.route('/category/<int:id>')
def get_category(id):
def get_category(id: int):
return get_resource(CategorySchema(), id)
@app.route('/category/<int:id>/children')
def get_category_children(data, id):
def get_category_children(data: dict, id: int):
data = dict(request.args)
data['parent_id'] = id
return search_resource(PointSchema(), data)
......@@ -142,7 +140,7 @@ def search_point(args):
@use_args(PointSchema)
def create_point(args):
return create_resource(PointSchema(), args, 'get_point')
@app.route('/point/<int:id>', methods=['GET'])
def get_point(id):
......@@ -154,14 +152,14 @@ def del_point(id):
return delete_resource(PointSchema(), id)
#TODO: Validate this
# TODO: Validate this
@app.route('/point/<int:id>', methods=['PUT'])
def edit_point(id):
return edit_resource(PointSchema(), id, request.get_json())
@app.route('/point/<int:id>/children', methods=['GET'])
def get_point_children(id):
def get_point_children(id: int):
data = dict(request.args)
data['parent_id'] = id
return search_resource(PointSchema(), data)
......@@ -170,19 +168,17 @@ def get_point_children(id):
# Helper functions:
def create_resource(schema, data, get_function):
'''
def create_resource(schema: CategorySchema, data: dict, get_function: str):
"""
Create the resource specified by the given model class and initialized with the data
dict, returning an appropriate JSON response.
dict, returning an appropriate JSON response.
Parameters:
schema (object): The class of the model for this resource
data (dict) : The initial data for this resource stored as a dictionary
get_function (string): The name of the view function (as a string) that gets a single
:param schema: The class of the model for this resource
:param data: The initial data for this resource stored as a dictionary
:param get_function: The name of the view function (as a string) that gets a single
instance of this resource. This is used for the response Location header.
Returns: a Flask Response object
'''
:return: a Flask Response object
"""
resource = schema.Meta.model(**data)
g.db_session.add(resource)
g.db_session.commit()
......@@ -192,34 +188,31 @@ def create_resource(schema, data, get_function):
return response
def get_resource(schema, id):
'''
def get_resource(schema: BaseSchema, id: int):
"""
Get a single resource of the specified model class by its ID.
Parameters:
schema (object): The class of the model for this resource
id (int) : The id of this resource
Returns: a Flask Response object
'''
:param schema: The class of the model for this resource
:param id: The id of this resource
:return: a Flask Response object
"""
resource = g.db_session.query(schema.Meta.model).get(id)
resp = (None, 404) if resource is None else \
(schema.dump(resource, many=False), 200)
return make_response(resp)
def edit_resource(schema, id, data):
'''
def edit_resource(schema: BaseSchema, id: int, data: dict):
"""
Modify the resource of the specified model class and id with the data from
data. Does not perform data validation.
Parameters:
schema (object): The class of the model for this resource
id (int) : The id of this resource
data (dict) : The new data for this resource stored as a dictionary
:param schema: The class of the model for this resource
:param id: The id of this resource
:param data: The new data for this resource stored as a dictionary
Returns: a Flask Response object
'''
"""
resource = g.db_session.query(schema.Meta.model).get(id)
for attr in data:
setattr(resource, attr, data[attr])
......@@ -229,16 +222,14 @@ def edit_resource(schema, id, data):
def delete_resource(schema, id):
'''
Delete the resource of the specified model class and id and return the
"""
Delete the resource of the specified model class and id and return the
appropriate response.
Parameters:
schema (object): The class of the model for this resource
id (int) : The id of this resource
Returns: a Flask Response object
'''
:param schema: The class of the model for this resource
:param id: The id of this resource
:return: a Flask Response object
"""
resource = g.db_session.query(schema.Meta.model).get(id)
g.db_session.delete(resource)
g.db_session.commit()
......@@ -247,18 +238,16 @@ def delete_resource(schema, id):
def search_resource(schema, data):
'''
"""
Search the database for a list of instances of the specified model class
that have the attributes given in data and return the appropriate JSON
response. Does not perform validation on search parameters.
Parameters:
schema (object): The class of the model for this resource
data (dict) : A dictionary containing search parameters
Returns:
a Flask Response object
'''
:param schema: The class of the model for this resource
:param data: A dictionary containing search parameters
:return: a Flask Response object
"""
# TODO: returns 404 when accessing children - i think it should just return an empty array
query = g.db_session.query(schema.Meta.model).filter_by(**data)
......
from sqlalchemy.ext.declarative import as_declarative
from sqlalchemy import Column, Integer, String, Float, JSON, ForeignKey, Enum
from sqlalchemy.ext.declarative import as_declarative
from sqlalchemy.orm import relationship, validates
from . import FieldType
@as_declarative()
class Base(object):
pass
......@@ -31,9 +32,8 @@ class Point(Base):
# Need to load category first or else attribute validation will fail
if 'category' in kwargs:
self.category = kwargs.pop('category')
super(Point, self).__init__(**kwargs)
super(Point, self).__init__(**kwargs)
@validates('attributes')
def validate_data(self, _, data):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment