/gdata/alt/app_engine.py

http://radioappz.googlecode.com/ · Python · 101 lines · 50 code · 6 blank · 45 comment · 5 complexity · 5b61db5f77ea282786150f59e42ddcba MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2009 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Provides functions to persist serialized auth tokens in the datastore.
  17. The get_token and set_token functions should be used in conjunction with
  18. gdata.gauth's token_from_blob and token_to_blob to allow auth token objects
  19. to be reused across requests. It is up to your own code to ensure that the
  20. token key's are unique.
  21. """
  22. __author__ = 'j.s@google.com (Jeff Scudder)'
  23. from google.appengine.ext import db
  24. from google.appengine.api import memcache
  25. class Token(db.Model):
  26. """Datastore Model which stores a serialized auth token."""
  27. t = db.BlobProperty()
  28. def get_token(unique_key):
  29. """Searches for a stored token with the desired key.
  30. Checks memcache and then the datastore if required.
  31. Args:
  32. unique_key: str which uniquely identifies the desired auth token.
  33. Returns:
  34. A string encoding the auth token data. Use gdata.gauth.token_from_blob to
  35. convert back into a usable token object. None if the token was not found
  36. in memcache or the datastore.
  37. """
  38. token_string = memcache.get(unique_key)
  39. if token_string is None:
  40. # The token wasn't in memcache, so look in the datastore.
  41. token = Token.get_by_key_name(unique_key)
  42. if token is None:
  43. return None
  44. return token.t
  45. return token_string
  46. def set_token(unique_key, token_str):
  47. """Saves the serialized auth token in the datastore.
  48. The token is also stored in memcache to speed up retrieval on a cache hit.
  49. Args:
  50. unique_key: The unique name for this token as a string. It is up to your
  51. code to ensure that this token value is unique in your application.
  52. Previous values will be silently overwitten.
  53. token_str: A serialized auth token as a string. I expect that this string
  54. will be generated by gdata.gauth.token_to_blob.
  55. Returns:
  56. True if the token was stored sucessfully, False if the token could not be
  57. safely cached (if an old value could not be cleared). If the token was
  58. set in memcache, but not in the datastore, this function will return None.
  59. However, in that situation an exception will likely be raised.
  60. Raises:
  61. Datastore exceptions may be raised from the App Engine SDK in the event of
  62. failure.
  63. """
  64. # First try to save in memcache.
  65. result = memcache.set(unique_key, token_str)
  66. # If memcache fails to save the value, clear the cached value.
  67. if not result:
  68. result = memcache.delete(unique_key)
  69. # If we could not clear the cached value for this token, refuse to save.
  70. if result == 0:
  71. return False
  72. # Save to the datastore.
  73. if Token(key_name=unique_key, t=token_str).put():
  74. return True
  75. return None
  76. def delete_token(unique_key):
  77. # Clear from memcache.
  78. memcache.delete(unique_key)
  79. # Clear from the datastore.
  80. Token(key_name=unique_key).delete()