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