"""Global Ginger exception and warning classes."""importoperatorfromginger.utils.hashableimportmake_hashable
[docs]classFieldDoesNotExist(Exception):"""The requested model field does not exist"""pass
[docs]classAppRegistryNotReady(Exception):"""The ginger.apps registry is not populated yet"""pass
[docs]classObjectDoesNotExist(Exception):"""The requested object does not exist"""silent_variable_failure=True
[docs]classMultipleObjectsReturned(Exception):"""The query returned multiple objects when only one was expected."""pass
[docs]classSuspiciousOperation(Exception):"""The user did something suspicious"""
classSuspiciousMultipartForm(SuspiciousOperation):"""Suspect MIME request in multipart form data"""passclassSuspiciousFileOperation(SuspiciousOperation):"""A Suspicious filesystem operation was attempted"""passclassDisallowedHost(SuspiciousOperation):"""HTTP_HOST header contains invalid value"""passclassDisallowedRedirect(SuspiciousOperation):"""Redirect to scheme not in allowed list"""passclassTooManyFieldsSent(SuspiciousOperation):""" The number of fields in a GET or POST request exceeded settings.DATA_UPLOAD_MAX_NUMBER_FIELDS. """passclassTooManyFilesSent(SuspiciousOperation):""" The number of fields in a GET or POST request exceeded settings.DATA_UPLOAD_MAX_NUMBER_FILES. """passclassRequestDataTooBig(SuspiciousOperation):""" The size of the request (excluding any file uploads) exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE. """pass
[docs]classRequestAborted(Exception):"""The request was closed before it was completed, or timed out."""pass
[docs]classBadRequest(Exception):"""The request is malformed and cannot be processed."""pass
[docs]classPermissionDenied(Exception):"""The user did not have permission to do that"""pass
[docs]classViewDoesNotExist(Exception):"""The requested view does not exist"""pass
[docs]classMiddlewareNotUsed(Exception):"""This middleware is not used in this server configuration"""pass
[docs]classImproperlyConfigured(Exception):"""Ginger is somehow improperly configured"""pass
[docs]classFieldError(Exception):"""Some kind of problem with a model field."""pass
NON_FIELD_ERRORS="__all__"
[docs]classValidationError(Exception):"""An error while validating data."""def__init__(self,message,code=None,params=None):""" The `message` argument can be a single error, a list of errors, or a dictionary that maps field names to lists of errors. What we define as an "error" can be either a simple string or an instance of ValidationError with its message attribute set, and what we define as list or dictionary can be an actual `list` or `dict` or an instance of ValidationError with its `error_list` or `error_dict` attribute set. """super().__init__(message,code,params)ifisinstance(message,ValidationError):ifhasattr(message,"error_dict"):message=message.error_dictelifnothasattr(message,"message"):message=message.error_listelse:message,code,params=message.message,message.code,message.paramsifisinstance(message,dict):self.error_dict={}forfield,messagesinmessage.items():ifnotisinstance(messages,ValidationError):messages=ValidationError(messages)self.error_dict[field]=messages.error_listelifisinstance(message,list):self.error_list=[]formessageinmessage:# Normalize plain strings to instances of ValidationError.ifnotisinstance(message,ValidationError):message=ValidationError(message)ifhasattr(message,"error_dict"):self.error_list.extend(sum(message.error_dict.values(),[]))else:self.error_list.extend(message.error_list)else:self.message=messageself.code=codeself.params=paramsself.error_list=[self]@propertydefmessage_dict(self):# Trigger an AttributeError if this ValidationError# doesn't have an error_dict.getattr(self,"error_dict")returndict(self)@propertydefmessages(self):ifhasattr(self,"error_dict"):returnsum(dict(self).values(),[])returnlist(self)defupdate_error_dict(self,error_dict):ifhasattr(self,"error_dict"):forfield,error_listinself.error_dict.items():error_dict.setdefault(field,[]).extend(error_list)else:error_dict.setdefault(NON_FIELD_ERRORS,[]).extend(self.error_list)returnerror_dictdef__iter__(self):ifhasattr(self,"error_dict"):forfield,errorsinself.error_dict.items():yieldfield,list(ValidationError(errors))else:forerrorinself.error_list:message=error.messageiferror.params:message%=error.paramsyieldstr(message)def__str__(self):ifhasattr(self,"error_dict"):returnrepr(dict(self))returnrepr(list(self))def__repr__(self):return"ValidationError(%s)"%selfdef__eq__(self,other):ifnotisinstance(other,ValidationError):returnNotImplementedreturnhash(self)==hash(other)def__hash__(self):ifhasattr(self,"message"):returnhash((self.message,self.code,make_hashable(self.params),))ifhasattr(self,"error_dict"):returnhash(make_hashable(self.error_dict))returnhash(tuple(sorted(self.error_list,key=operator.attrgetter("message"))))
[docs]classEmptyResultSet(Exception):"""A database query predicate is impossible."""pass
[docs]classFullResultSet(Exception):"""A database query predicate is matches everything."""pass
[docs]classSynchronousOnlyOperation(Exception):"""The user tried to call a sync-only function from an async context."""pass