def from_arguments(cls: Type[T], arguments: QueryString, default_to_descending: bool = False) -> T: order = arguments.optionally_parse_string(key='order', max_length=32) if order is None: if default_to_descending is False: raise BadRequest( 'Supply order=[ascending|descending] parameter') return cls(True) if order.lower() == 'ascending': return cls(True) if order.lower() == 'descending': return cls(False) raise BadRequest('Acceptable `order` values: ascending, descending')
def optionally_from_arguments(cls: Type[T], arguments: QueryString, max_length: int = 64, min_length: int = 3, fallback_to_wildcard: bool = False, key='fragment') -> Optional[T]: fragment = arguments.optionally_parse_string(key=key, max_length=max_length, min_length=min_length, allow_whitespace=True) if fragment is None and fallback_to_wildcard is False: return None if fragment is None and fallback_to_wildcard is True: return cls.with_wildcard() return cls(fragment)
def optionally_from_arguments( cls: Type[T], arguments: QueryString, available: Optional[Dict[str, T]] = None, key: str = 'order_by', fallback_to: Optional[T] = None) -> Optional[T]: if available is None: if not isinstance(cls.available, dict): raise RuntimeError('implement .available order by values') available = cls.available term = arguments.optionally_parse_string(key) if term is None: return fallback_to if term not in available.keys(): raise BadRequest('Invalid {k} value. Valid values: {v}'.format( k=key, v=', '.join(available.keys()))) return available[term]