0

from this article https://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...

Community
  • 1
  • 1
M-Dahab
  • 389
  • 7
  • 11

2 Answers2

1

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)
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • that is so correct !! thank you sir, but jinja2 is not able to render Permissions object so I convert it to dict befor render it, and i dont need to render it any way. thank you very much @snakecharmerb – M-Dahab May 02 '16 at 14:31
  • @user5258689 If you feel that my answer helped you, you could [accept my answer](http://meta.stackexchange.com/a/5235). – snakecharmerb May 02 '16 at 14:34
0

as @snakecharmerb said, it works with JasonProperty and also as another solution i found is here ObjectProperty()

that supports any python object by pickle it and save it to datastore and reverse!

and here's my code after:

class Permissions(dict):
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 __getstate__(self):
    return self.__dict__

def __setstate__(self, d):
    self.__dict__.update(d)

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]


class PermissionsProperty(ndb.PickleProperty):

    def _to_base_type(self, value):
        return pickle.dumps(value)

    def _from_base_type(self, value):
        return pickle.loads(value)

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

note that I added getstate() and setstate() in order for pickling correctily other wise it gives a pickling error.

M-Dahab
  • 389
  • 7
  • 11
  • Why don't you just use a PickleProperty: https://cloud.google.com/appengine/docs/python/ndb/entity-property-reference – new name May 03 '16 at 00:23
  • that's also correct, i understand and that's what i did use, but just to make it more understandable i just call it ObjectProperty() as the thread said also, thank you very much. – M-Dahab May 03 '16 at 05:32