Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # For google-api-python-client
- from google.appengine.api import users
- from google.appengine.ext import ndb
- from oauth2client import appengine
- class CurrentUserEmailProperty(ndb.StringProperty):
- def _set_value(self, entity, value):
- if value is None:
- current_user = users.get_current_user()
- if current_user is not None:
- value = current_user.email()
- super(CurrentUserEmailProperty, self)._set_value(entity, value)
- def _fix_up(self, cls, code_name):
- """Internal helper called to register the property with the model class.
- Overrides the _set_attributes method on the model class to interject this
- attribute in to the keywords passed to it. Since the method _set_attributes
- is called by the model class constructor to set values, this -- in congress
- with the custom defined _set_value -- will make sure this property always
- gets set when an instance is created, even if not passed in.
- """
- original_set_attributes = cls._set_attributes
- def CustomSetAttributes(setattr_self, kwds):
- if self._code_name not in kwds:
- kwds[self._code_name] = None
- original_set_attributes(setattr_self, kwds)
- cls._set_attributes = CustomSetAttributes
- super(CurrentUserEmailProperty, self)._fix_up(cls, code_name)
- class CustomCredentialsModel(appengine.CredentialsNDBModel):
- user_email = CurrentUserEmailProperty(required=True, indexed=False)
- appengine.CredentialsModel = CustomCredentialsModel
Advertisement
Add Comment
Please, Sign In to add comment