def _ensure_positive_int(string, field, cutoff=None): msg = f'{field} must be a positive integer' try: value = int(string) except ValueError: raise exceptions.ValidationError(detail=msg) if value < 0: raise exceptions.ValidationError(detail=msg) if cutoff is not None: return min(value, cutoff) return value
def _get_namespace(self, data): """Get collecton namespace from filename.""" ns_name = data['filename'].namespace try: return models.Namespace.objects.get(name=ns_name) except models.Namespace.DoesNotExist: raise exceptions.ValidationError( f'Namespace "{ns_name}" does not exist.')
def get_format_type(self, request): format = request.query_params.get('type') if format is None: return None format = format.lower() if format not in ALLOWED_FORMAT_TYPES: raise exceptions.ValidationError( f'{repr(format)} is not a valid format type.') return format
def get_order_by(self, request): param = request.query_params.get(self.ordering_param, self.default_ordering) order = 'asc' if param.startswith('-'): order = 'desc' param = param[1:] if param not in self.allowed_ordering: raise exceptions.ValidationError( f'{repr(param)} is not a valid ordering parameter value.') return param, order