def normalize_urn(self, value): if self.request.user.get_org().is_anon: raise InvalidQueryError("URN lookups not allowed for anonymous organizations") try: return URN.identity(URN.normalize(value)) except ValueError: raise InvalidQueryError("Invalid URN: %s" % value)
def check_query(self, params): # check user hasn't provided values for more than one of any exclusive params if sum([(1 if params.get(p) else 0) for p in self.exclusive_params]) > 1: raise InvalidQueryError( "You may only specify one of the %s parameters" % ", ".join(self.exclusive_params))
def delete(self, request, *args, **kwargs): self.lookup_values = self.get_lookup_values() if not self.lookup_values: raise InvalidQueryError( "URL must contain one of the following parameters: " + ", ".join(sorted(self.lookup_params.keys()))) instance = self.get_object() self.perform_destroy(instance) return Response(status=status.HTTP_204_NO_CONTENT)
def get_lookup_values(self): """ Extracts lookup_params from the request URL, e.g. {"uuid": "123..."} """ lookup_values = {} for param, field in self.lookup_params.items(): if param in self.request.query_params: param_value = self.request.query_params[param] # try to normalize URN lookup values if param == "urn": param_value = self.normalize_urn(param_value) lookup_values[field] = param_value if len(lookup_values) > 1: raise InvalidQueryError( "URL can only contain one of the following parameters: " + ", ".join(sorted(self.lookup_params.keys()))) return lookup_values
def get_uuid_param(self, name): param = self.request.query_params.get(name) try: return UUID(param) if param is not None else None except ValueError: raise InvalidQueryError("Value for %s must be a valid UUID" % name)
def get_int_param(self, name): param = self.request.query_params.get(name) try: return int(param) if param is not None else None except ValueError: raise InvalidQueryError("Value for %s must be an integer" % name)