Browse files

Added full error checking. Error messages on save should return an er…

…rors dict in the payload.
  • Loading branch information...
1 parent bf717c4 commit e37aeb0cada78e8e1df0af7e1778e9fa823f0797 @joestump joestump committed Apr 13, 2011
Showing with 10 additions and 4 deletions.
  1. +10 −4 ajax/endpoints.py
View
14 ajax/endpoints.py
@@ -71,20 +71,26 @@ class ModelEndpoint(BaseEndpoint):
def create(self, request):
record = self.model(**self._extract_data(request))
if self.can_create(request.user, record):
- record.save()
- return self._encode_record(record)
+ return self._encode_record(self._save(record))
else:
raise AJAXError(403, _("Access to endpoint is forbidden"))
+ def _save(self, record):
+ try:
+ record.full_clean()
+ record.save()
+ return record
+ except ValidationError, e:
+ raise AJAXError(400, _("Could not save model."), errors=e.message_dict)
+
@require_pk
def update(self, request):
record = self._get_record()
if self.can_update(request.user, record):
for key, val in self._extract_data(request).iteritems():
setattr(record, key, val)
- record.save()
- return self._encode_record(record)
+ return self._encode_record(self._save(record))
else:
raise AJAXError(403, _("Access to endpoint is forbidden"))

0 comments on commit e37aeb0

Please sign in to comment.