0

I placed a _post_put_hook into one of my NDB model types so that that whenever an entity of that type were put, it would invalidate a memcache key. This key is made up with the urlsafe version of the settings key. However, when this code runs, it says this:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\rpc.py", line 68, in get
    result = func(*args)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\rpc.py", line 154, in pub_refreshSandbox
    team_key = s.create.team("Cool Group")
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\GlobalUtilities.py", line 534, in team
    new_team.put()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2902, in _put
    return self._put_async(**ctx_options).get_result()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 320, in get_result
    self.check_success()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 315, in check_success
    self.wait()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 299, in wait
    if not ev.run1():
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\eventloop.py", line 219, in run1
    delay = self.run0()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\eventloop.py", line 181, in run0
    callback(*args, **kwds)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 454, in _on_future_completion
    self._help_tasklet_along(gen, val)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 368, in _help_tasklet_along
    self.set_result(result)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 264, in set_result
    callback(*args, **kwds)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\DataModels.py", line 182, in _post_put_hook
    tools.expireCache('allteams-' + self.settings.get().websafe)
AttributeError: 'KeyProperty' object has no attribute 'get'

Here is the relevant model class:

class Team(ndb.Expando):
    name = ndb.StringProperty()
    show_team = ndb.BooleanProperty()
    settings = ndb.KeyProperty()

    @classmethod
    def _post_put_hook(self, future):
        memcache.delete('allteams-' + self.settings.get().websafe)

Ideas?

rhefner1
  • 464
  • 4
  • 13

1 Answers1

3

in this case self.settings is not the actual key but the Models property because this is a classmethod and not an instance method. you need to work on the future object.
here the docs: https://developers.google.com/appengine/docs/python/ndb/futureclass

in this case:

@classmethod
def _post_put_hook(self, future):
    entitykey = future.get_result()
    entity    = entitykey.get()
    memcache.delete('allteams-' + entity.settings.get().websafe)

not sure what websafe does for you. maybe you mean entity.settings.urlsafe() ?

aschmid00
  • 7,038
  • 2
  • 47
  • 66
  • That worked! Thanks. Sorry, websafe is a simple property I added that returns key.urlsafe(). I wanted to insert the keys into a web template, but I don't think you can call functions within a template. Anyway, no matter. – rhefner1 Apr 25 '12 at 18:06
  • ok i see, in this case its better to call entity.settings.urlsafe() because you save rpc calls and the code will run quicker. you can call functions in some template languages but not in other ones. so it depends what language you are using. – aschmid00 Apr 25 '12 at 18:07