开发者

Exposing a "dumbed-down", read-only instance of a Model in GAE

开发者 https://www.devze.com 2022-12-28 17:27 出处:网络
Does anyone know a clever way, in Google App Engine, to return a wrapped Model instance that only exposes a few of the original properties, and does not allow saving the instance back to the datastore

Does anyone know a clever way, in Google App Engine, to return a wrapped Model instance that only exposes a few of the original properties, and does not allow saving the instance back to the datastore?

I'm not looking for ways of actually enforcing these rules, obviously it'll still be possible to change the instance by digging through its __dict__ etc. I just want a way to avoid accidental exposure/changing of data.

My initial thought was to do this (I want to do this for a public version of a User model):

class PublicUser(db.Model):
    display_name = db.StringProperty()

    @classmethod
    def kind(cls):
        return 'User'

    def put(self):
        raise SomeError()

Unfortunately, GAE maps the kind to a class early on, so if I do PublicUser.get_by_id(1) I will actually get a User instance back, not a PublicUser instance.

Also, the idea is that it should at least appear to be a Model instance so that I can pass it around to code that does not know about the fact that it is a "dumbed-down" version. Ultimately I want to do this so that I can use my generic data exposure functions on the read-only version, so that they only expose public information about the user.


Update

I went with icio's solution. Here's the code I wrote for copying the properties from the User instance over to a PublicUser instance:

class User(db.Model):
    # ...
    # code
    # ...

    def as_public(self):
        """Returns a PublicUser version of this object.
        
        """
        props = self.properties()

        pu = PublicUser()
        for prop in pu.properties().values():
            # Only copy properties that exist for both the PublicUser model and
            # the User model.
            if prop.name in props:
                # Thi开发者_StackOverflow中文版s line of code sets the property of the PublicUser
                # instance to the value of the same property on the User
                # instance.
                prop.__set__(pu, props[prop.name].__get__(self, type(self)))

        return pu

Please comment if this isn't a good way of doing it.


Could you not create a method within your User class which instantiates a ReadOnlyUser object and copies the values of member variables over as appropriate? Your call would be something like User.get_by_id(1).readonly() with the readonly method defined in the following form:

class User(db.Model):
    def readonly(self):
        return ReadOnlyUser(self.name, self.id);

Or you could perhaps have your User class extend another class with methods to do this automatically based on some static vars listing properties to copy over, or something.

P.S. I don't code in Python

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号