Created
April 28, 2011 15:14
-
-
Save ahmednuaman/946538 to your computer and use it in GitHub Desktop.
Prepare a GAE model for serialisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import logging | |
import urllib | |
def serialise_model(m, d): | |
# let's serialise this bad boy | |
ps = m.properties() | |
# check if we're dealing with a list or not | |
if isinstance( d, list ): | |
# prepare list for return | |
r = [ ] | |
# cycle through the list | |
for i in d: | |
r.append( _serialise_model_cycle( ps, i ) ) | |
# return list | |
return r | |
else: | |
# just cycle through the data | |
return _serialise_model_cycle( ps, d ) | |
def _serialise_model_cycle(ps, d): | |
# prepare the return dict | |
r = { } | |
# we cycle through the properties of the model | |
for k, p in ps.iteritems(): | |
v = getattr( d, k ) | |
# if the value isn't empty, we add it to the list | |
if v is not None and v != '': | |
# check if we can escape the value | |
if isinstance( v, bool ) is False: | |
v = urllib.unquote_plus( v ) | |
# set the value | |
r[ k ] = v | |
# don't forget the key | |
r[ 'key' ] = str( d.key() ) | |
# return the data | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment