Exemple #1
0
  def run_query(self, query):
    res, last_model = query()
    results = []

    for obj_src, join_index, tree in res:
      model = None
      for obj, src in obj_src:
        if obj is not None:
          model = obj.__class__
          if hasattr(model, '_deferred') and model._deferred:
            model = model.__base__
          break

      if model is not None:
        model_name = model_registry.get_name(model)
      else:
        model_name = None

      d = {'model': model_name,
           'join_index': join_index,
           'objects': [(obj.pk, src) if obj is not None else (None, src) for obj, src in obj_src],
           'tree': tree
          }
      results.append(d)

    if last_model is not None:
      last_model = model_registry.get_name(last_model)

    return dict(last_model=last_model, results=results, computed_on=datetime.now())
Exemple #2
0
 def model_to_dict(cls):
     model_name = model_registry.get_name(cls)
     d = dict(model=model_name, relationships=[])
     for f in dir(cls):
         if model_registry.get_manager(model_name).is_rel_allowed(f):
             d['relationships'].append(f)
     return d
Exemple #3
0
 def object_to_dict(obj):
     d = {}
     for f in obj._meta.fields:
         d[f.column] = getattr(obj, f.column)
         if not type(
                 d[f.column]) in (long, int, float, bool, types.NoneType):
             d[f.column] = unicode(d[f.column])
         if type(f) == ForeignKey:
             v = getattr(obj, f.name)
             if v is not None:
                 model_name = model_registry.get_name(v.__class__)
                 d[f.column] = {
                     'model': model_name,
                     'id': v.pk,
                     '__str__': str(v)
                 }
                 try:
                     d[f.column]['url'] = model_registry.get_manager(
                         model_name).url_of(v)
                 except:
                     pass
     if 'id' not in d:
         pk = obj.pk
         if not type(pk) in (long, int, float, bool, types.NoneType):
             d['id'] = unicode(pk)
         else:
             d['id'] = pk
     return d
Exemple #4
0
 def model_to_dict(cls):
   model_name = model_registry.get_name(cls)
   d = dict(model=model_name, relationships=[])
   for f in dir(cls):
     if model_registry.get_manager(model_name).is_rel_allowed(f):
       d['relationships'].append(f)
   return d
Exemple #5
0
 def object_to_dict(obj):
   d = {}
   for f in obj._meta.fields:
     d[f.column] = getattr(obj, f.column)
     if not type(d[f.column]) in (long, int, float, bool, types.NoneType):
       d[f.column] = unicode(d[f.column])
     if type(f) == ForeignKey:
       v = getattr(obj, f.name)
       if v is not None:
         model_name = model_registry.get_name(v.__class__)
         d[f.column] = {
           'model': model_name,
           'id': v.pk,
           '__str__': str(v)
         }
         try:
           d[f.column]['url'] = model_registry.get_manager(model_name).url_of(v)
         except:
           pass
   if 'id' not in d:
     pk = obj.pk
     if not type(pk) in (long, int, float, bool, types.NoneType):
       d['id'] = unicode(pk)
     else:
       d['id'] = pk
   return d
Exemple #6
0
    def run_query(self, query):
        res, last_model = query()
        results = []

        for obj_src, join_index, tree in res:
            model = None
            for obj, src in obj_src:
                if obj is not None:
                    model = obj.__class__
                    if hasattr(model, '_deferred') and model._deferred:
                        model = model.__base__
                    break

            if model is not None:
                model_name = model_registry.get_name(model)
            else:
                model_name = None

            d = {
                'model':
                model_name,
                'join_index':
                join_index,
                'objects': [(obj.pk, src) if obj is not None else (None, src)
                            for obj, src in obj_src],
                'tree':
                tree
            }
            results.append(d)

        if last_model is not None:
            last_model = model_registry.get_name(last_model)

        return dict(last_model=last_model,
                    results=results,
                    computed_on=datetime.now())
Exemple #7
0
    def objects_to_dict(objects, ignore_excludes=False, follow_fk=True):
        if len(objects) == 0:
            return dict(fields=[], objects=[])

        fields = []
        fk = []
        is_model = True
        add_pk = False

        obj = objects[0]
        model_name = model_registry.get_name(obj.__class__)
        if ignore_excludes is True:
            excludes = []
        else:
            excludes = model_registry.get_manager(model_name).field_excludes

        if hasattr(obj, '_meta'):
            for f in obj._meta.fields:
                if f.column not in excludes:
                    fields.append(f.column)
                    fk.append(f.name if type(f) == ForeignKey else None)
            if 'id' not in fields:
                fields.append('pk')
                fk.append(None)
                add_pk = True
        else:
            is_model = False
            for f in obj.fields():
                fields.append(f)
                fk.append(None)

        packed = []
        urls = []
        for obj in objects:
            obj_url = model_registry.get_manager(model_name).url_of(obj)
            values = []

            for column, fk_name in zip(fields, fk):
                if is_model:
                    value = getattr(obj, column)
                else:
                    value = obj.get(column)

                if type(value) is Decimal:
                    value = float(value)
                elif not type(value) in (long, int, float, bool,
                                         types.NoneType):
                    value = unicode(value)
                if fk_name is not None and follow_fk is True:
                    v = getattr(obj, fk_name)
                    if v is not None:
                        fk_model_name = model_registry.get_name(v.__class__)
                        fk_url = None
                        try:
                            fk_url = model_registry.get_manager(
                                fk_model_name).url_of(v)
                        except:
                            fk_url = None
                        value = (fk_model_name, v.pk, str(v), fk_url)
                values.append(value)

            urls.append(obj_url)
            packed.append(values)

        if add_pk is True:
            assert fields[-1] == 'pk'
            fields[-1] = 'id'
        return dict(fields=fields, objects=packed, urls=urls)
Exemple #8
0
  def objects_to_dict(objects, ignore_excludes=False, follow_fk=True):
    if len(objects) == 0:
      return dict(fields=[], objects=[])

    fields = []
    fk = []
    is_model = True
    add_pk = False

    obj = objects[0]
    model_name = model_registry.get_name(obj.__class__)
    if ignore_excludes is True:
      excludes = []
    else:
      excludes = model_registry.get_manager(model_name).field_excludes

    if hasattr(obj, '_meta'):
      for f in obj._meta.fields:
        if f.column not in excludes:
          fields.append(f.column)
          fk.append(f.name if type(f) == ForeignKey else None)
      if 'id' not in fields:
        fields.append('pk')
        fk.append(None)
        add_pk = True
    else:
      is_model = False
      for f in obj.fields():
        fields.append(f)
        fk.append(None)

    packed = []
    urls = []
    for obj in objects:
      obj_url = model_registry.get_manager(model_name).url_of(obj)
      values = []

      for column, fk_name in zip(fields, fk):
        if is_model:
          value = getattr(obj, column)
        else:
          value = obj.get(column)

        if type(value) is Decimal:
          value = float(value)
        elif not type(value) in (long, int, float, bool, types.NoneType):
          value = unicode(value)
        if fk_name is not None and follow_fk is True:
          v = getattr(obj, fk_name)
          if v is not None:
            fk_model_name = model_registry.get_name(v.__class__)
            fk_url = None
            try:
              fk_url = model_registry.get_manager(fk_model_name).url_of(v)
            except:
              fk_url = None
            value = (fk_model_name, v.pk, str(v), fk_url)
        values.append(value)

      urls.append(obj_url)
      packed.append(values)

    if add_pk is True:
      assert fields[-1] == 'pk'
      fields[-1] = 'id'
    return dict(fields=fields, objects=packed, urls=urls)