|
3 | 3 | from werkzeug.wrappers import Response as ResponseBase |
4 | 4 | from werkzeug.exceptions import BadRequest |
5 | 5 |
|
6 | | -from collections import OrderedDict |
7 | | - |
8 | 6 | from .args import use_args |
9 | 7 | from .marshalling import marshal_with |
10 | 8 |
|
11 | 9 | from ..utilities import unpack, get_docstring, get_summary, merge |
12 | 10 | from ..representations import DEFAULT_REPRESENTATIONS |
13 | | -from ..find import current_labthing |
| 11 | +from ..find import current_labthing, current_thing |
14 | 12 | from ..event import PropertyStatusEvent |
15 | 13 | from ..schema import Schema, ActionSchema, build_action_schema |
16 | | -from ..tasks import default_pool |
| 14 | +from ..tasks import Pool |
17 | 15 | from ..deque import Deque, resize_deque |
18 | 16 | from ..json.schemas import schema_to_json |
19 | 17 | from .. import fields |
@@ -46,8 +44,9 @@ def __init__(self, *args, **kwargs): |
46 | 44 | MethodView.__init__(self, *args, **kwargs) |
47 | 45 |
|
48 | 46 | # Set the default representations |
49 | | - # TODO: Inherit from parent LabThing. See original flask_restful implementation |
50 | | - self.representations = OrderedDict(DEFAULT_REPRESENTATIONS) |
| 47 | + self.representations = ( |
| 48 | + current_thing.representations if current_thing else DEFAULT_REPRESENTATIONS |
| 49 | + ) |
51 | 50 |
|
52 | 51 | @classmethod |
53 | 52 | def get_apispec(cls): |
@@ -152,6 +151,7 @@ class ActionView(View): |
152 | 151 | # Internal |
153 | 152 | _cls_tags = {"actions"} |
154 | 153 | _deque = Deque() # Action queue |
| 154 | + _emergency_pool = Pool() |
155 | 155 |
|
156 | 156 | def get(self): |
157 | 157 | queue_schema = build_action_schema(self.schema, self.args)(many=True) |
@@ -247,8 +247,9 @@ def dispatch_request(self, *args, **kwargs): |
247 | 247 | if self.schema: |
248 | 248 | meth = marshal_with(self.schema)(meth) |
249 | 249 |
|
| 250 | + # Try to find a pool on the current LabThing, but fall back to Views emergency pool |
| 251 | + pool = current_thing.actions if current_thing else self._emergency_pool |
250 | 252 | # Make a task out of the views `post` method |
251 | | - pool = current_labthing().actions if current_labthing else default_pool |
252 | 253 | task = pool.spawn(meth, *args, **kwargs) |
253 | 254 |
|
254 | 255 | # Keep a copy of the raw, unmarshalled JSON input in the task |
@@ -380,8 +381,8 @@ def dispatch_request(self, *args, **kwargs): |
380 | 381 | self, "__name__", "unknown" |
381 | 382 | ) |
382 | 383 |
|
383 | | - if current_labthing(): |
384 | | - current_labthing().message( |
| 384 | + if current_thing: |
| 385 | + current_thing.message( |
385 | 386 | PropertyStatusEvent(property_name), property_value, |
386 | 387 | ) |
387 | 388 |
|
|
0 commit comments