PageRenderTime 38ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/source/decorators.rst

https://code.google.com/p/kay-framework/
ReStructuredText | 98 lines | 73 code | 25 blank | 0 comment | 0 complexity | a21ae93c1d98b64789d62fc2db47136d MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. =============================
  2. Decorators
  3. =============================
  4. .. module:: kay.utils.decorators
  5. View Decorators
  6. =============================
  7. Kay includes a number of decorators that can be applied to views
  8. which make development in the appengine environment easier. These
  9. decorators provide support for functionality for views that is
  10. needed often and can be reused.
  11. .. function:: maintenance_check(endpoint='_internal/maintenance_page')
  12. The ``maintenance_check()`` decorator checks if appengine is in
  13. maintenance mode and if so redirects the user to a maintenance page.
  14. By default the maintenance page is at the url routing endpoint
  15. '_internal/maintenance_page' but this is configurable by providing
  16. the endpoint argument to the decorator.
  17. ::
  18. @maintenance_check
  19. def my_view(request):
  20. # ...
  21. return response
  22. .. function:: cron_only()
  23. The ``cron_only()`` decorator allows you to specify that the view
  24. should only be accessed by the Appengine cron service. The ``cron_only()``
  25. decorator checks the appropriate HTTP headers and if the process is
  26. being accessed by somewhere other than the Appengine cron service then
  27. a 403 response is returned. However, for development purposes the
  28. ``cron_only()`` decorator allows one exception. If :attr:`DEBUG` is
  29. ``True`` and the application is running on the development server
  30. then the request is allowed.
  31. ::
  32. @cron_only
  33. def my_cron_view(request):
  34. # ...
  35. return response
  36. Utility Decorators
  37. =============================
  38. .. function:: retry_on_timeout(retries=3, secs=1)
  39. The ``retry_on_timeout()`` decorator allows the wrapped function to be
  40. called multiple times if a datastore API timeout occurs. The wrapped
  41. function should be `ideponent <http://en.wikipedia.org/wiki/Idempotence>`_.
  42. This means that the it shouldn't breakthings if the function called
  43. multiple times.
  44. ::
  45. @retry_on_timeout(retries=5)
  46. def my_writer_func():
  47. # Some datastore operation
  48. return
  49. .. function:: auto_adapt_to_methods()
  50. ``auto_adapt_to_methods()`` is a utility decorator that wraps other
  51. decorators. It allows decorators to auto adapt to methods to which
  52. self is passed.
  53. ::
  54. @auto_adapt_to_methods
  55. def my_decorator(func):
  56. def new_func():
  57. # ...
  58. return
  59. return new_func
  60. .. function:: memcache_property(key_f, expire=0)
  61. A decorator that converts a function into a lazy property. The
  62. function wrapped is called the first time to retrieve the result
  63. and then that calculated result is used the next time you access
  64. the value. The decorator takes one manditory key factory function
  65. that takes the owning object as it's only argument and returns
  66. a key to be used to store in memcached::
  67. class Foo(db.Model):
  68. @memcached_property(lambda o: "Foo:%s:foo" % o.key().name())
  69. def foo(self):
  70. # calculate something important here
  71. return 42
  72. The class has to have a `__dict__` in order for this property to
  73. work.