def wrapper(context, request): try: val = view_function(context, request) except Problem as e: comment = 'Problem found in action layer' status_int = e.status_int error_msg = e.error_msg except Exception as e: if hasattr(e, 'asdict') and callable(e.asdict): comment = 'Form validation error' status_int = 422 # Unprocessable Entity error_msg = first(e.asdict().values()) else: raise # Let this view-raised exception pass through else: if val is None: return Response(status_int=204) # No content elif isinstance(val, str): comment = 'View returned error msg as a string' status_int = 400 error_msg = val else: return val raise HTTPError( status_int=status_int, content_type='text/plain', body=error_msg, detail=error_msg, # could be shown to end users comment=comment, # not displayed to end users )
def ancestor_model(resource, cls, include_self=False): '''Returns a model instance of type ``cls`` found in the ``model`` attribute of the ancestors of ``resource``, or None. ''' def predicate(resource): return hasattr(resource, 'model') and isinstance(resource.model, cls) o = first(ancestor_finder(resource, predicate, include_self)) return o.model if o else None
def ancestor_model(resource, cls, include_self=False): """Find in ancestors a model instance of type ``cls``. The search is done in the ``model`` attribute of the ancestors of ``resource``. Returns None if not found. """ def predicate(resource): return hasattr(resource, 'model') and isinstance(resource.model, cls) o = first(ancestor_finder(resource, predicate, include_self)) return o.model if o else None
def _s(obj, peto: AbstractPeto, features=(), **kw) -> Sequence: if len(obj) == 0: return [] first_item = first(obj) if isinstance(first_item, primitive_types): return [jsonright(item, peto, features, **kw) for item in obj] # Below this line we assume we are dealing with a sequence of entities. # In this case we pivot data in order to save bandwidth. first_dict = jsonright(first_item, peto, features, **kw) ret = [[key] for key in first_dict] for entity in obj: adict = jsonright(entity, peto, features, **kw) for alist in ret: alist.append(jsonright(adict[alist[0]], peto, features, **kw)) return ret
def ancestor(resource, cls, include_self=False): """Return the first ancestor of ``resource`` that is of type ``cls``.""" def predicate(resource): return isinstance(resource, cls) return first(ancestor_finder(resource, predicate, include_self))
def first(self): """Return a matching entity, or None.""" return first(self)
def ancestor(resource, cls, include_self=False): '''Returns the first ancestor of ``resource`` that is of type ``cls``.''' def predicate(resource): return isinstance(resource, cls) return first(ancestor_finder(resource, predicate, include_self))