"George Sakkis" <[EMAIL PROTECTED]> writes:

> On Apr 3, 10:15 am, [EMAIL PROTECTED] wrote:
>
>> On Tue, 3 Apr 2007 at 12:53, George Sakkis wrote:
>> > Ok, I see. So the way I now handle it is by checking if
>> > cherrypy.request.params.get('tg_format') == 'json' and return the
>> > appropriate dict. Kind of kludge but works for now.
>>
>> Fortunately, that is an unneeded kludge :)
>>
>> Take a look at the json.py file in your quickstart directory.  You can
>> do something like:
>>
>>      @jsonify.when('isinstance(obj, YourClass)')
>>      def jsonify_YourClass(obj):
>>          return <your appropriate dict>
>
> Nice, that's good to know. Actually if I go down this road, I'd like
> to generalize it for any elixir Entity instances rather than
> separately for each class. Of course there are issues such as which
> attributes to serialize (all the 'public' data attributes seems a
> reasonable default) and whether the serialization should be shallow or
> deep (or more generally allow shallow/deep semantics per attribute
> rather than global). Ugh, seems a lot of work to get it right and
> cover all cases... nevermind :)

This works with SQL Alchemy (and Elixir, you just have to attach these methods
to its base class --- I've tested and used it):

###############################################################################
class SABase(object):
    def __init__(self, *args, **kwargs):
        super(SABase, self).__init__(*args, **kwargs)


def toDict(self):
    """
    Returns a dictionary with all data belonging to the current object
    """
    d = {}
    for k in self.c.keys():
        d[k] = getattr(self, k)
    return d
SABase.toDict = toDict
###############################################################################


Then you create your classes inheriting from SABase instead of object (using
the standard identity classes from TG as an example):

################################################################################
class Visit(SABase):
    @classmethod
    def lookup_visit(cls, visit_key):
        return Visit.get(visit_key)


class VisitIdentity(object):
    pass

class Group(SABase):
    def __repr__(self):
        return self.group_name
################################################################################


This way you can say (pseudocode)

     v = model.Visit.get(key)
     v.toDict()

to get a dictionary with all columns from the visits table.  This dictionary
can then be returned through any means you want, including JSON.

The above suggestion becomes, then, something along the lines of:


      @jsonify.when('isinstance(SABase, YourClass)')
      def jsonify_YourClass(obj):
          return obj.toDict()



Be seeing you,
-- 
Jorge Godoy      <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to