OLD | NEW |
(Empty) | |
| 1 """ |
| 2 Global Django exception and warning classes. |
| 3 """ |
| 4 from django.utils import six |
| 5 from django.utils.encoding import force_text |
| 6 |
| 7 |
| 8 class FieldDoesNotExist(Exception): |
| 9 """The requested model field does not exist""" |
| 10 pass |
| 11 |
| 12 |
| 13 class DjangoRuntimeWarning(RuntimeWarning): |
| 14 pass |
| 15 |
| 16 |
| 17 class AppRegistryNotReady(Exception): |
| 18 """The django.apps registry is not populated yet""" |
| 19 pass |
| 20 |
| 21 |
| 22 class ObjectDoesNotExist(Exception): |
| 23 """The requested object does not exist""" |
| 24 silent_variable_failure = True |
| 25 |
| 26 |
| 27 class MultipleObjectsReturned(Exception): |
| 28 """The query returned multiple objects when only one was expected.""" |
| 29 pass |
| 30 |
| 31 |
| 32 class SuspiciousOperation(Exception): |
| 33 """The user did something suspicious""" |
| 34 |
| 35 |
| 36 class SuspiciousMultipartForm(SuspiciousOperation): |
| 37 """Suspect MIME request in multipart form data""" |
| 38 pass |
| 39 |
| 40 |
| 41 class SuspiciousFileOperation(SuspiciousOperation): |
| 42 """A Suspicious filesystem operation was attempted""" |
| 43 pass |
| 44 |
| 45 |
| 46 class DisallowedHost(SuspiciousOperation): |
| 47 """HTTP_HOST header contains invalid value""" |
| 48 pass |
| 49 |
| 50 |
| 51 class DisallowedRedirect(SuspiciousOperation): |
| 52 """Redirect to scheme not in allowed list""" |
| 53 pass |
| 54 |
| 55 |
| 56 class TooManyFieldsSent(SuspiciousOperation): |
| 57 """ |
| 58 The number of fields in a GET or POST request exceeded |
| 59 settings.DATA_UPLOAD_MAX_NUMBER_FIELDS. |
| 60 """ |
| 61 pass |
| 62 |
| 63 |
| 64 class RequestDataTooBig(SuspiciousOperation): |
| 65 """ |
| 66 The size of the request (excluding any file uploads) exceeded |
| 67 settings.DATA_UPLOAD_MAX_MEMORY_SIZE. |
| 68 """ |
| 69 pass |
| 70 |
| 71 |
| 72 class PermissionDenied(Exception): |
| 73 """The user did not have permission to do that""" |
| 74 pass |
| 75 |
| 76 |
| 77 class ViewDoesNotExist(Exception): |
| 78 """The requested view does not exist""" |
| 79 pass |
| 80 |
| 81 |
| 82 class MiddlewareNotUsed(Exception): |
| 83 """This middleware is not used in this server configuration""" |
| 84 pass |
| 85 |
| 86 |
| 87 class ImproperlyConfigured(Exception): |
| 88 """Django is somehow improperly configured""" |
| 89 pass |
| 90 |
| 91 |
| 92 class FieldError(Exception): |
| 93 """Some kind of problem with a model field.""" |
| 94 pass |
| 95 |
| 96 |
| 97 NON_FIELD_ERRORS = '__all__' |
| 98 |
| 99 |
| 100 class ValidationError(Exception): |
| 101 """An error while validating data.""" |
| 102 def __init__(self, message, code=None, params=None): |
| 103 """ |
| 104 The `message` argument can be a single error, a list of errors, or a |
| 105 dictionary that maps field names to lists of errors. What we define as |
| 106 an "error" can be either a simple string or an instance of |
| 107 ValidationError with its message attribute set, and what we define as |
| 108 list or dictionary can be an actual `list` or `dict` or an instance |
| 109 of ValidationError with its `error_list` or `error_dict` attribute set. |
| 110 """ |
| 111 |
| 112 # PY2 can't pickle naive exception: http://bugs.python.org/issue1692335. |
| 113 super(ValidationError, self).__init__(message, code, params) |
| 114 |
| 115 if isinstance(message, ValidationError): |
| 116 if hasattr(message, 'error_dict'): |
| 117 message = message.error_dict |
| 118 # PY2 has a `message` property which is always there so we can't |
| 119 # duck-type on it. It was introduced in Python 2.5 and already |
| 120 # deprecated in Python 2.6. |
| 121 elif not hasattr(message, 'message' if six.PY3 else 'code'): |
| 122 message = message.error_list |
| 123 else: |
| 124 message, code, params = message.message, message.code, message.p
arams |
| 125 |
| 126 if isinstance(message, dict): |
| 127 self.error_dict = {} |
| 128 for field, messages in message.items(): |
| 129 if not isinstance(messages, ValidationError): |
| 130 messages = ValidationError(messages) |
| 131 self.error_dict[field] = messages.error_list |
| 132 |
| 133 elif isinstance(message, list): |
| 134 self.error_list = [] |
| 135 for message in message: |
| 136 # Normalize plain strings to instances of ValidationError. |
| 137 if not isinstance(message, ValidationError): |
| 138 message = ValidationError(message) |
| 139 if hasattr(message, 'error_dict'): |
| 140 self.error_list.extend(sum(message.error_dict.values(), [])) |
| 141 else: |
| 142 self.error_list.extend(message.error_list) |
| 143 |
| 144 else: |
| 145 self.message = message |
| 146 self.code = code |
| 147 self.params = params |
| 148 self.error_list = [self] |
| 149 |
| 150 @property |
| 151 def message_dict(self): |
| 152 # Trigger an AttributeError if this ValidationError |
| 153 # doesn't have an error_dict. |
| 154 getattr(self, 'error_dict') |
| 155 |
| 156 return dict(self) |
| 157 |
| 158 @property |
| 159 def messages(self): |
| 160 if hasattr(self, 'error_dict'): |
| 161 return sum(dict(self).values(), []) |
| 162 return list(self) |
| 163 |
| 164 def update_error_dict(self, error_dict): |
| 165 if hasattr(self, 'error_dict'): |
| 166 for field, error_list in self.error_dict.items(): |
| 167 error_dict.setdefault(field, []).extend(error_list) |
| 168 else: |
| 169 error_dict.setdefault(NON_FIELD_ERRORS, []).extend(self.error_list) |
| 170 return error_dict |
| 171 |
| 172 def __iter__(self): |
| 173 if hasattr(self, 'error_dict'): |
| 174 for field, errors in self.error_dict.items(): |
| 175 yield field, list(ValidationError(errors)) |
| 176 else: |
| 177 for error in self.error_list: |
| 178 message = error.message |
| 179 if error.params: |
| 180 message %= error.params |
| 181 yield force_text(message) |
| 182 |
| 183 def __str__(self): |
| 184 if hasattr(self, 'error_dict'): |
| 185 return repr(dict(self)) |
| 186 return repr(list(self)) |
| 187 |
| 188 def __repr__(self): |
| 189 return 'ValidationError(%s)' % self |
| 190 |
| 191 |
| 192 class EmptyResultSet(Exception): |
| 193 """A database query predicate is impossible.""" |
| 194 pass |
OLD | NEW |