def parse_my_projects_arg(): """Parse my-projects request argument and save result to g.my_projects""" try: g.my_projects = boolean_from_string(request.args['my-projects']) g.unused_args.discard('my-projects') except KeyError: g.my_projects = not g.is_admin except ValueError: raise exc.InvalidArgumentValue('my-projects', 'boolean', request.args.get('my-projects')) if not g.my_projects and not g.is_admin: raise exc.InvalidArgumentValue( 'my-projects', 'boolean', False, 'Only administrators are allowed ' 'to set my-projects to false')
def parse_my_projects_arg(): """Parse my-projects request argument and save result to g.my_projects""" try: g.my_projects = boolean_from_string(request.args['my-projects']) g.unused_args.discard('my-projects') except KeyError: g.my_projects = not g.is_admin except ValueError: raise exc.InvalidArgumentValue('my-projects', 'boolean', request.args.get('my-projects')) if not g.my_projects and not g.is_admin: raise exc.InvalidArgumentValue('my-projects', 'boolean', False, 'Only administrators are allowed ' 'to set my-projects to false')
def from_argument(self, value): return boolean_from_string(value, self._illegal_argument)
class ElementType(object): """Resource element type descriptor""" def __init__(self, name, typename, basic_search_matchers, add_search_matchers=None, sortby_names=None, is_nullable=False): self.name = name self.typename = typename self._is_nullable = is_nullable self._search_matchers = _matchers_plus( basic_search_matchers, add_search_matchers) if sortby_names is not None: self.sortby_names = sortby_names else: self.sortby_names = (name,) def _illegal_element(self, value, reason=None): raise exc.InvalidElementValue(self.name, self.typename, value, reason) def _illegal_argument(self, value, reason=None): raise exc.InvalidArgumentValue(self.name, self.typename, value, reason) def from_argument(self, value): """Parse string to this type internal representation""" self._illegal_argument(value) def from_request(self, value): """Check that value from request is correct for this type""" if self._is_nullable and value is None: return None return self._from_request_impl(value) def _from_request_impl(self, value): self._illegal_element(value) def get_search_matcher(self, filter_type): """Get search matcher by name. Matcher should take a value from result object (in internal representation, as returned by from_argument) and a value from match string, and return True or False. """ try: return self._search_matchers[filter_type] except KeyError: raise exc.InvalidRequest('No search filter %r defined ' 'for element %r of type %r' % (filter_type, self.name, self.typename)) def parse_search_argument(self, filter_type, value): """Parse search argument of type filter_type""" if filter_type == 'in': try: return [self.from_argument(elem) for elem in split_with_escape(value, '|', '\\')] except ValueError, e: self._illegal_argument(value, str(e)) elif filter_type == 'exists': return boolean_from_string(value, self._illegal_argument)