DocSavage / bloog

RESTful Blog for Google App Engine

This URL has Read+Write access

bloog / handlers / restful.py
100644 133 lines (118 sloc) 5.114 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# The MIT License
#
# Copyright (c) 2008 William T. Katz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
 
"""
RESTful Controller
 
We want our RESTful controllers to simply throw up their hands if they get
an unhandled HTTP verb. This is better for rich clients and server load
than throwing back lots of useless HTML.
 
These inherited methods should be overridden if there's a chance a human
browser is involved.
 
TODO: Return more information HTTP status codes that won't autotrip
browser login forms. For example, return status 405 (Method not allowed)
with an Allow header containing the list of valid methods.
"""
__author__ = 'William T. Katz'
 
from google.appengine.ext import webapp
 
import logging
 
# Some useful module methods
def send_successful_response(handler, response):
    # Response is probably just a URL.
    logging.debug("Sending successful response: %s", response)
    handler.response.out.write(response)
 
def get_sent_properties(request_func, propname_list):
    """
This maps request strings to values in a hash, optionally run through
a function with previous request values as parameters to the func.
1) key -> just read in the corresponding request value
2) tuple (key, func) -> Read the request value for the string key
and pass it through func
3) tuple (key, func, additional keys...) -> Get the request
values for the additional keys and pass them through func
before setting the key's value with the output.
If a key is not present in the request, then we do not insert a key
with None or empty string. The key is simply absent, therefore allowing
you to use the returned hash to initial a Model instance.
"""
    prop_hash = {}
    for item in propname_list:
        if isinstance(item, basestring):
            key = item
            value = request_func(item)
        elif isinstance(item, tuple):
            key = item[0]
            prop_func = item[1]
            if len(item) <= 2:
                value = prop_func(request_func(key))
            else:
                try:
                    addl_keys = map(prop_hash.get, item[2:])
                    value = prop_func(*addl_keys)
                except:
                    return None
        if value:
            prop_hash[key] = value
    return prop_hash
 
def methods_via_query_allowed(handler_method):
    """
A decorator to automatically re-route overloaded POSTs
that specify the real HTTP method in a _method query string.
 
To use it, decorate your post method like this:
 
import restful
...
@restful.methods_via_query_allowed
def post(self):
pass
 
The decorator will check for a _method query string or POST argument,
and if present, will redirect to delete(), put(), etc.
"""
    def redirect_if_needed(self, *args, **kwargs):
        real_verb = self.request.get('_method', None)
        if not real_verb and 'X-HTTP-Method-Override' in self.request.environ:
            real_verb = self.request.environ['X-HTTP-Method-Override']
        if real_verb:
            logging.debug("Redirected from POST. Detected method override = %s", real_verb)
            method = real_verb.upper()
            if method == 'HEAD':
                self.head(*args, **kwargs)
            elif method == 'PUT':
                self.put(*args, **kwargs)
            elif method == 'DELETE':
                self.delete(*args, **kwargs)
            elif method == 'TRACE':
                self.trace(*args, **kwargs)
            elif method == 'OPTIONS':
                self.head(*args, **kwargs)
            # POST and GET included for completeness
            elif method == 'POST':
                self.post(*args, **kwargs)
            elif method == 'GET':
                self.get(*args, **kwargs)
            else:
                self.error(405)
        else:
            handler_method(self, *args, **kwargs)
    return redirect_if_needed
 
class Controller(webapp.RequestHandler):
    def get(self, *params):
        self.redirect("/403.html")
 
    def head(self, *params):
        pass