import uuid from schematics.models import Model from schematics.types import UUIDType, StringType from schematics.serialize import to_python, make_safe_python, blacklist, whitelist class Media(Model): id = UUIDType(print_name='_id') owner = UUIDType() title = StringType(max_length=40) class Options: roles = { 'meow': blacklist('owner'), 'woof': whitelist('id'), } m = Media() m.id = uuid.uuid4() m.id m.title = 'word' # {'owner': None, '_id': UUID('5e3634f5-d37a-4cc8-b16a-cb23b04a7553'), 'title': u'word'} print to_python(m) # {'_id': UUID('5e3634f5-d37a-4cc8-b16a-cb23b04a7553'), 'title': u'word'} print make_safe_python(Media, m, 'meow') # {'_id': UUID('5e3634f5-d37a-4cc8-b16a-cb23b04a7553')} print make_safe_python(Media, m, 'woof')
def test_owner_safe(self): owner_safe = make_safe_python(self.klass, self.instance, 'owner') self.assertEqual(self.owner_safe, owner_safe)
def test_public_safe(self): public_safe = make_safe_python(self.klass, self.instance, 'public') self.assertEqual(self.public_safe, public_safe)
'owner': blacklist(), 'public': whitelist('title', 'year'), } m = Movie(title='Some Movie', year=2011, personal_thoughts='It was pretty good') print 'MOVIE ]', ('-' * 40) print ' schema ::', to_jsonschema(m) print ' python ::', to_python(m) print ' json ::', to_json(m) print ' owner ::', make_safe_python(Movie, m, 'owner') print ' public ::', make_safe_json(Movie, m, 'public') print #movie_json = m.to_json() movie_json = make_safe_json(Movie, m, 'owner') print 'Movie as JSON ]', ('-' * 32) print ' json ::', movie_json print ### Reload movie movie_data = json.loads(movie_json) m2 = Movie(**movie_data)
class Options: roles = { 'owner': blacklist(), 'public': whitelist('title', 'year'), } m = Movie(title='Some Movie', year=2011, personal_thoughts='It was pretty good') print 'MOVIE ]', ('-' * 40) print ' schema ::', to_jsonschema(m) print ' python ::', to_python(m) print ' json ::', to_json(m) print ' owner ::', make_safe_python(Movie, m, 'owner') print ' public ::', make_safe_json(Movie, m, 'public') print #movie_json = m.to_json() movie_json = make_safe_json(Movie, m, 'owner') print 'Movie as JSON ]', ('-' * 32) print ' json ::', movie_json print ### Reload movie movie_data = json.loads(movie_json) m2 = Movie(**movie_data) print 'RESTORED MOVIE ]', ('-' * 31) print ' schema ::', to_jsonschema(m2)
from schematics.models import Model from schematics.types import UUIDType, StringType from schematics.serialize import to_python, make_safe_python, blacklist, whitelist class Media(Model): id = UUIDType(print_name='_id') owner = UUIDType() title = StringType(max_length=40) class Options: roles = { 'meow': blacklist('owner'), 'woof': whitelist('id'), } m = Media() m.id = uuid.uuid4() m.owner = uuid.uuid4() m.title = 'word' # {'owner': None, '_id': UUID('5e3634f5-d37a-4cc8-b16a-cb23b04a7553'), 'title': u'word'} print to_python(m) # {'_id': UUID('5e3634f5-d37a-4cc8-b16a-cb23b04a7553'), 'title': u'word'} print make_safe_python(Media, m, 'meow') # {'_id': UUID('5e3634f5-d37a-4cc8-b16a-cb23b04a7553')} print make_safe_python(Media, m, 'woof')