Skip to content

Instantly share code, notes, and snippets.

@ahmednuaman
Created April 28, 2011 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmednuaman/946538 to your computer and use it in GitHub Desktop.
Save ahmednuaman/946538 to your computer and use it in GitHub Desktop.
Prepare a GAE model for serialisation
#!/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