Generate links at pre_dump time
This commit is contained in:
parent
d13f8d8b8c
commit
4793de1283
4 changed files with 547 additions and 101 deletions
44
openflexure_microscope/common/flask_labthings/decorators.py
Normal file
44
openflexure_microscope/common/flask_labthings/decorators.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
from webargs.flaskparser import use_args, use_kwargs
|
||||
from functools import wraps
|
||||
from flask import make_response
|
||||
|
||||
|
||||
def unpack(value):
|
||||
"""Return a three tuple of data, code, and headers"""
|
||||
if not isinstance(value, tuple):
|
||||
return value, 200, {}
|
||||
|
||||
try:
|
||||
data, code, headers = value
|
||||
return data, code, headers
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
try:
|
||||
data, code = value
|
||||
return data, code, {}
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
return value, 200, {}
|
||||
|
||||
|
||||
class marshal_with(object):
|
||||
def __init__(self, schema):
|
||||
"""
|
||||
:param schema: a dict of whose keys will make up the final
|
||||
serialized response output
|
||||
"""
|
||||
self.schema = schema
|
||||
|
||||
def __call__(self, f):
|
||||
@wraps(f)
|
||||
def wrapper(*args, **kwargs):
|
||||
resp = f(*args, **kwargs)
|
||||
if isinstance(resp, tuple):
|
||||
data, code, headers = unpack(resp)
|
||||
print((data, code, headers))
|
||||
return make_response(self.schema.jsonify(data), code, headers)
|
||||
else:
|
||||
return make_response(self.schema.jsonify(resp))
|
||||
return wrapper
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
from flask import abort
|
||||
from flask import abort, url_for
|
||||
|
||||
from openflexure_microscope.common.flask_labthings.schema import Schema
|
||||
from openflexure_microscope.common.flask_labthings import fields
|
||||
|
|
@ -6,6 +6,8 @@ from openflexure_microscope.common.labthings_core import tasks
|
|||
from openflexure_microscope.common.flask_labthings.resource import Resource
|
||||
from openflexure_microscope.common.flask_labthings.utilities import description_from_view
|
||||
|
||||
from marshmallow import pre_dump
|
||||
|
||||
|
||||
class TaskList(Resource):
|
||||
def get(self):
|
||||
|
|
@ -43,16 +45,17 @@ class TaskSchema(Schema):
|
|||
_start_time = fields.String(data_key="start_time")
|
||||
_end_time = fields.String(data_key="end_time")
|
||||
|
||||
# TODO: Add HTTP methods
|
||||
links = fields.Hyperlinks(
|
||||
{
|
||||
# TODO: Automate this somewhat
|
||||
@pre_dump
|
||||
def generate_links(self, data, **kwargs):
|
||||
data.links = {
|
||||
"self": {
|
||||
"href": fields.AbsoluteUrlFor(TaskResource, id="<id>"),
|
||||
"href": url_for(TaskResource.endpoint, id=data.id, _external=True),
|
||||
"mimetype": "application/json",
|
||||
**description_from_view(TaskResource)
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
return data
|
||||
|
||||
|
||||
task_schema = TaskSchema()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue