Jesper Nøhr avatar Jesper Nøhr committed 8ba536f

user handler, event handler, better handling of fields, types, methodfactory with get/post/put/delete on the api

Comments (0)

Files changed (1)


-import types, decimal, yaml
+import types, decimal, yaml, copy
 from django.db.models.query import QuerySet
 from django.db.models import Model
 from django.utils import simplejson
 class Emitter(object):
     def __init__(self, payload, typemapper):
         self.typemapper = typemapper
-        if isinstance(payload, QuerySet):
-   = tuple([m for m in payload.all()])
-        elif isinstance(payload, Model):
-   = payload
-        elif isinstance(payload, Exception):
-            raise payload
-        elif isinstance(payload, basestring):
-   = str(payload)
-        else:
-            raise ValueError("Can't emit this.")
+ = payload
     def construct(self):
             ret = None
-            if isinstance(thing, (tuple, list)):
+            if isinstance(thing, (tuple, list, QuerySet)):
                 ret = _list(thing)
             elif isinstance(thing, dict):
                 ret = _dict(thing)
             return ret
         def _fk(data, field):
-            related = getattr(data,
-            if not related:
-                if field.rel.field_name ==
-                    related = related._get_pk_val()
-                else:
-                    related = getattr(related, field.rel.field_name)
-            return _any(related)
+            return _any(getattr(data,
         def _m2m(data, field):
             return [ _model(m) for m in getattr(data, ]
             if type(data) in self.typemapper.keys():
                 v = lambda f: getattr(data, f.attname)
-                want_fields = self.typemapper.get(type(data)).fields
+                want_fields = copy.copy(list(self.typemapper.get(type(data)).fields))
                 for f in data._meta.local_fields:
                     if f.serialize:
                         if not f.rel:
                             if f.attname in want_fields:
                                 ret[f.attname] = _any(v(f))
+                                want_fields.remove(f.attname)
                             if f.attname[:-3] in want_fields:
                                 ret[] = _fk(data, f)
+                                want_fields.remove(
                 for mf in data._meta.many_to_many:
                     if mf.serialize:
                         if mf.attname in want_fields:
                             ret[] = _m2m(data, mf)
+                            want_fields.remove(
+                # try to get the remainder of fields
+                for maybe_field in want_fields:
+                    pass # do something here?
 class JSONEmitter(Emitter):
+    # TODO: callback functions
     def render(self):
         return simplejson.dumps(self.construct(), cls=DateTimeAwareJSONEncoder)
 class YAMLEmitter(Emitter):
     def render(self):
-        return yaml.dump(self.construct())
+        return yaml.safe_dump(self.construct())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.