style nit
[invirt/packages/invirt-web.git] / code / view.py
1 import os, sys
2
3 import cherrypy
4 from mako.template import Template
5 from mako.lookup import TemplateLookup
6 import simplejson
7 import datetime, decimal
8 from StringIO import StringIO
9 from invirt.config import structs as config
10 from webcommon import State
11
12 class MakoHandler(cherrypy.dispatch.LateParamPageHandler):
13     """Callable which sets response.body."""
14     
15     def __init__(self, template, next_handler, content_type='text/html; charset=utf-8'):
16         self.template = template
17         self.next_handler = next_handler
18         self.content_type = content_type
19     
20     def __call__(self):
21         env = globals().copy()
22         env.update(self.next_handler())
23         cherrypy.response.headers['Content-Type'] = self.content_type
24         return self.template.render(**env)
25         
26
27 class MakoLoader(object):
28     
29     def __init__(self):
30         self.lookups = {}
31
32     def get_lookup(self, directories, module_directory=None,
33                      collection_size=-1, imports=[], **kwargs):
34         # Find the appropriate template lookup.
35         key = (tuple(directories), module_directory)
36         try:
37             lookup = self.lookups[key]
38         except KeyError:
39             lookup = TemplateLookup(directories=directories,
40                                     module_directory=module_directory,
41                                     collection_size=collection_size,
42                                     default_filters=['decode.utf8'],
43                                     input_encoding='utf-8',
44                                     output_encoding='utf-8',
45                                     imports=imports,
46                                     )
47             self.lookups[key] = lookup
48         return lookup
49
50     def __call__(self, filename, directories, module_directory=None,
51                  collection_size=-1, content_type='text/html; charset=utf-8',
52                  imports=[]):
53         cherrypy.request.lookup = lookup = self.get_lookup(directories, module_directory,
54                                                            collection_size, imports)
55         
56         # Replace the current handler.
57         cherrypy.request.template = t = lookup.get_template(filename)
58         cherrypy.request.handler = MakoHandler(t, cherrypy.request.handler, content_type)
59
60 cherrypy.tools.mako = cherrypy.Tool('on_start_resource', MakoLoader())
61
62 def revertStandardError():
63     """Move stderr to stdout, and return the contents of the old stderr."""
64     errio = sys.stderr
65     if not isinstance(errio, StringIO):
66         return ''
67     sys.stderr = sys.stdout
68     errio.seek(0)
69     return errio.read()
70
71 def catchStderr():
72     old_handler = cherrypy.request.handler
73     def wrapper(*args, **kwargs):
74         sys.stderr = StringIO()
75         ret = old_handler(*args, **kwargs)
76         e = revertStandardError()
77         if e:
78             if isinstance(ret, dict):
79                 ret["error_text"] = e
80         return ret
81     if old_handler:
82         cherrypy.request.handler = wrapper
83
84 cherrypy.tools.catch_stderr = cherrypy.Tool('before_handler', catchStderr)
85
86 class JSONEncoder(simplejson.JSONEncoder):
87         def default(self, obj):
88                 if isinstance(obj, datetime.datetime):
89                         return str(obj)
90                 elif isinstance(obj, decimal.Decimal):
91                         return float(obj)
92                 else:
93                         return simplejson.JSONEncoder.default(self, obj)
94
95 def jsonify_tool_callback(*args, **kwargs):
96     if not cherrypy.request.cached:
97         response = cherrypy.response
98         response.headers['Content-Type'] = 'text/javascript'
99         response.body = JSONEncoder().iterencode(response.body)
100
101 cherrypy.tools.jsonify = cherrypy.Tool('before_finalize', jsonify_tool_callback, priority=30)
102
103 def require_login():
104     """If the user isn't logged in, raise 403 with an error."""
105     if cherrypy.request.login is False:
106         raise cherrypy.HTTPError(403,
107             "You are not authorized to access that resource")
108
109 cherrypy.tools.require_login = cherrypy.Tool('on_start_resource', require_login, priority=150)
110
111 def require_POST():
112     """If the request isn't a POST request, raise 405 Method Not Allowed"""
113     if cherrypy.request.method != "POST":
114         raise cherrypy.HTTPError(405,
115                                  "You must submit this request with POST")
116
117 cherrypy.tools.require_POST = cherrypy.Tool('on_start_resource', require_POST, priority=150)
118
119 def remote_user_login():
120     """Get the current user based on the SSL or GSSAPI environment variables"""
121     environ = cherrypy.request.wsgi_environ
122     user = environ.get('REMOTE_USER')
123     if user is None:
124         return
125     else:
126         cherrypy.request.login = None # clear what cherrypy put there
127
128     if environ.get('AUTH_TYPE') == 'Negotiate':
129         # Convert the krb5 principal into a krb4 username
130         if not user.endswith('@%s' % config.kerberos.realm):
131             cherrypy.request.login = False # failed to login
132         else:
133             cherrypy.request.login = user.split('@')[0].replace('/', '.')
134     else:
135         cherrypy.request.login = user
136
137 cherrypy.tools.remote_user_login = cherrypy.Tool('on_start_resource', remote_user_login, priority=50)
138
139 def invirtwebstate_init():
140     """Initialize the cherrypy.request.state object from Invirt"""
141     if not hasattr(cherrypy.request, "state"):
142         cherrypy.request.state = State(cherrypy.request.login)
143
144 cherrypy.tools.invirtwebstate = cherrypy.Tool('on_start_resource', invirtwebstate_init, priority=100)
145
146 class View(object):
147     _cp_config = {'tools.mako.directories': [os.path.join(os.path.dirname(__file__),'templates')]}