Commits

Matthew Schinckel committed b37c2c9

Refactored view.pre_serialize into bind_form

Comments (0)

Files changed (2)

repose/serializers/base.py

         for name in form.fields.keys():
             if name in form.hidden_fields():
                 continue
-            result[name] = form.data.get(name) or form.initial.get(name)
-            if not result[name] and hasattr(form.instance, name):
-                result[name] = getattr(form.instance, name)
-            elif not hasattr(form.instance, name):
-                # How to handle a non-model field?
-                raise Exception("Non-model field not handled.")
+            if name in form.data:
+                result[name] = form.data[name]
+            elif name in form.initial:
+                result[name] = form.initial[name]
+            else:
+                if not hasattr(form, 'instance'):
+                    raise Exception('Unable to find attribute "%s": no instance.' % name)
+                else:
+                    if hasattr(form.instance, name):
+                        result[name] = getattr(form.instance, name)
+                    else:
+                        raise Exception('Attribute "%s" not found on model "%s"' % (
+                            name, form.instance.__class__.__name__
+                        ))
             if callable(result[name]):
                 result[name] = result[name]()
         
         else:
             if self.etag_function:
                 etag = self.etag_function(response)
-            form = self.pre_serialize(response)
+            form = self.bind_form(response)
             serializer = serializers.get_serializer(content_type)
             response = http.OK(
                 serializer.serialize(form, request=request, template_name=self.template_name), 
             result['detail'] = detail
         return result
     
-    def pre_serialize(self, data):
+    def bind_form(self, data):
         # TODO Remove the reliance on isinstance().
         if isinstance(data, (QuerySet, list, tuple)):
-            return [self.pre_serialize(x) for x in data]
+            return [self.bind_form(x) for x in data]
         if isinstance(data, BaseForm):
             form = data
         elif isinstance(data, models.Model):
     def get_form(self):
         pass
     
-    def pre_serialize(self, data):
+    def bind_form(self, data):
         return data
     
     def etag_function(self, data):
         else:
             location = None
         
-        return http.Created(self.pre_serialize(forms), 
+        return http.Created(self.bind_form(forms), 
             content_type=request.META.get('HTTP_ACCEPT'),
             location=location)