forked from tessel/t2-crash-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
executable file
·146 lines (114 loc) · 4.82 KB
/
common.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
import json
import logging
import os
import jinja2
from google.appengine.api import memcache
from model import from_milliseconds
from github_utils import issue_url
from util import crash_uri, snippetize
# decorator for all requests
def common_request(callable_function):
def wrapped_callable(*args, **kwargs):
args_list = list(args)
rrequest = RRequest(args_list[0])
args_list[0] = rrequest
try:
return callable_function(*args_list, **kwargs)
except Exception, e:
logging.exception('Exception : %s' % unicode(e))
rrequest.add_error('Exception : %s' % unicode(e))
rrequest.add_to_json('error', unicode(e))
rrequest.render('500.html')
return wrapped_callable
def cache_it(key=None, time=86400):
def decorator(callable_function):
def wrapped_callable(*args, **kwargs):
cached_value = memcache.get(key)
if cached_value:
return cached_value
else:
value = callable_function(*args, **kwargs)
if value is not None:
memcache.set(key, value, time=time)
return value
return wrapped_callable
return decorator
def readable_date(milliseconds):
date_time = from_milliseconds(milliseconds)
return date_time.strftime('%Y-%m-%d %H:%M:%S')
class RRequest(object):
environment = None
@classmethod
def jinja2_environment(cls):
# set the template search path to pages
if not RRequest.environment:
RRequest.environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'pages')))
# add readable date filter to make it available for templates
RRequest.environment.filters['readable_date'] = readable_date
RRequest.environment.filters['crash_uri'] = crash_uri
RRequest.environment.filters['snippetize'] = snippetize
RRequest.environment.filters['issue_url'] = issue_url
return RRequest.environment
def __init__(self, request_handler):
# handle to the current request context
self.request_handler = request_handler
# request context parameters
self.params = {}
# breadcrumbs
self.breadcrumbs = []
# error status for the request
self.error = False
# error messages
self.errors = []
# messages
self.messages = []
# json response
self.json = {}
def get_parameter(self, key, default_value=None, valid_iter=None):
value = self.request_handler.request.get(key, default_value)
if valid_iter:
if value not in valid_iter:
self.add_error('Invalid parameter for %s, \'%s\'.' % (key, value))
value = None
return value
# checks if the specified keys in the query string are not in either request.GET | POST | FILES
def empty_query_string(self, *args):
if args:
for key in args:
value = self.get_parameter(key, None, None)
if not value or len(value) <= 0:
return True
return False
def add_breadcrumb(self, name, href):
self.breadcrumbs.append({'name': name, 'href': href})
# adds new parameters to a request context
def add_parameter(self, key, value):
self.params[key] = value
# adds a new message in the list of messages
def add_message(self, message):
self.messages.append(message)
# adds an error to the list of errors
def add_error(self, error):
self.error = True
self.errors.append(error)
def add_to_json(self, key, value):
self.json[key] = value
def render(self, template_name):
f = self.get_parameter('f', 'html', ['html', 'json', 'pjson'])
p = self.get_parameter('pretty', 'false', ['true', 'false'])
callback = self.get_parameter('callback')
pretty = f == 'pjson' or p == 'true'
if f == 'json' or f == 'pjson':
'''Generate JSON response'''
self.request_handler.response.headers['Content-Type'] = 'text/plain'
self.request_handler.response.headers['Access-Control-Allow-Origin'] = '*'
_json = self.json if self.json else {'errors': self.errors, 'messages': self.messages}
jsonified_json = json.dumps(_json) if (pretty is False) else json.dumps(_json, indent=2)
if callback is not None:
jsonified_json = callback + '(' + jsonified_json + ');'
self.request_handler.response.out.write(jsonified_json)
else:
'''Generate HTML response '''
self.request_handler.response.out.write(
RRequest.jinja2_environment().get_template(template_name).render({'rrequest': self}))