user5258689 user5258689 - 7 months ago 71
Python Question

how to subclass google app engine ndb property to support python subclassed objects

from this article http://stackoverflow.com/a/32107024/5258689

I have a dict() subclass - that allows me to do dict.key (use dot to access keys i mean) - as follows:

class Permissions(dict):
"""
Example:
m = Map({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer'])
"""
def __init__(self, *args, **kwargs):
super(Permissions, self).__init__(*args, **kwargs)
for arg in args:
if isinstance(arg, dict):
for k, v in arg.iteritems():
self[k] = v

if kwargs:
for k, v in kwargs.iteritems():
self[k] = v

def __getattr__(self, attr):
return self.get(attr)

def __setattr__(self, key, value):
self.__setitem__(key, value)

def __setitem__(self, key, value):
super(Permissions, self).__setitem__(key, value)
self.__dict__.update({key: value})

def __delattr__(self, item):
self.__delitem__(item)

def __delitem__(self, key):
super(Permissions, self).__delitem__(key)
del self.__dict__[key]


my question is how to create my own PermessionsPropery() ? or what property to extend so I can create that ?

I am willing to use this property in my subclassed User object to add school name as key and permission as dict value, ex(user can have permissions in multiple schools):

from webapp2_extras.appengine.auth.models import User as webapp2User
class User(webapp2User):
permissions = PermissionsProperty()

u = User(permissions=Permissions({"school1": {"teacher": True}}))


then I check for user's permissions like:

if user.permissions[someshcool].teacher:
#do stuff.....

#or
if user.permissions.someschool.teacher:
#do stuff.....


I've tried to follow this doc https://cloud.google.com/appengine/docs/python/ndb/subclassprop
with no profit !

so is it even possible ? and if so, how ?
thank you...

Answer

App Engine's ndb package doesn't support saving dictionaries directly, but json can be saved in a JsonProperty, and dictionaries are easily encoded as json, so the simplest implementation is a subclass of JsonProperty that returns a Permissions instance when accessed.

class PermissionsProperty(ndb.JsonProperty):

    def _to_base_type(self, value):
        return dict(value)

    def _from_base_type(self, value):
        return Permissions(value)

This implementation is incomplete though, because JsonProperty will accept values that aren't Permissions instances, so you need to add a _validate method to ensure that what you're saving is the right type of object.

class PermissionsProperty(ndb.JsonProperty):

    def _to_base_type(self, value):
        return dict(value)

    def _from_base_type(self, value):
        return Permissions(value)

    def _validate(self, value):
        if not isinstance(value, Permissions):
            raise TypeError('Expected Permissions instance, got %r', % value)