-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.py
163 lines (131 loc) · 5.86 KB
/
controller.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import json, urllib2
from core import Configuration, IndexFactory, UrlManager, TemplateFactory
from negotiator import ContentNegotiator
# create a global configuration
global_configuration = Configuration()
class EdjoGenericController(object):
def get_accept_parameters(self, accept, accept_language):
# do the content negotiation
cn = ContentNegotiator(default_accept_parameters=self.config.accepts_default,
acceptable=self.config.accepts, weights=self.config.conneg_weights)
ap = cn.negotiate(accept=accept, accept_language=accept_language)
return ap
def get_mimetype(self, suffix, accept_parameters):
if suffix is not None:
return self.config.get_mimetype(suffix)
else:
return self.config.get_mimetype(accept_parameters)
def _get_suffix(self, path):
bits = path.split("/")
last_path_part = bits[-1]
file_parts = last_path_part.split(".")
if len(file_parts) > 1:
return file_parts[-1]
return None
def _construct_args(self, a, q):
args = None
if a is not None:
a = urllib2.unquote(a)
args = json.loads(a)
initial_request = False
if args is None:
args = self.config.get_default_args()
initial_request = True
if q is not None and q != "":
initial_request = False
args["search"] = q
return initial_request, args
class EdjoGenericRecordController(EdjoGenericController):
def GET(self, id=None):
"""
method to handle GET request; override this in specific controllers
GET request will contain the following features
- id: the id of the item to be displayed
"""
self.config = global_configuration
def process(self, a, q, id):
# turn the incoming arguments to the args parameter
initial_request, args = self._construct_args(a, q)
properties = self._ui_properties(args)
index_factory = IndexFactory(self.config)
s = index_factory.get_index_dao()
properties['record'] = s.record(id)
return properties
def _ui_properties(self, args):
properties = {}
properties['config'] = self.config
# set the UrlManager for the UI to use
properties['url_manager'] = UrlManager(self.config, args)
return properties
def render_record(self, properties, suffix, accept_parameters):
# call the render engine
template_factory = TemplateFactory(self.config)
t = template_factory.get_template_engine()
return t.render_record(properties, suffix, accept_parameters)
class EdjoGenericIndexController(EdjoGenericController):
def GET(self, path=None):
"""
method to handle GET request; override this in specific controllers
GET request will contain the following features
- path: a path attached to the end of the base url containing implicit facet info
- a: a query argument containing the search constraints in JSON
- q: a free text search query argument
"""
self.config = global_configuration
def process(self, a, q, path):
# turn the incoming arguments to the args parameter
initial_request, args = self._construct_args(a, q)
# fold in the free text search options if relevant
implicit_facets = self._add_implicit_facets(path, args)
if len(implicit_facets) > 0:
initial_request = False
properties = self._ui_properties(args, implicit_facets)
# create a search engine connection and get the results back
index_factory = IndexFactory(self.config)
s = index_factory.get_index_dao()
if initial_request:
properties['results'] = s.initial(args)
else:
properties['results'] = s.search(args)
if args.has_key("search"):
properties['q'] = args['search']
else:
properties['q'] = ""
return properties
def _ui_properties(self, args, implicit_facets):
properties = {}
properties['config'] = self.config
# set the implicit facets for the UI to use
properties['implicit_facets'] = implicit_facets
# set the UrlManager for the UI to use
properties['url_manager'] = UrlManager(self.config, args, implicit_facets)
return properties
def _add_implicit_facets(self, path, args):
implicit_facets = {}
if path is not None:
# split the path by the middle "/"
path = path.strip()
if path.endswith("/"):
path = path[:-1]
bits = path.split('/')
if len(bits) % 2 == 0:
# self.config.base_url = self.config.base_url + path
if not args.has_key('q'):
args['q'] = {}
for i in range(0, len(bits), 2):
field = bits[i]
value = bits[i+1]
if args['q'].has_key(field):
args['q'][field].append(value)
else:
args['q'][field] = [value]
if implicit_facets.has_key(field):
implicit_facets[field].append(value)
else:
implicit_facets[field] = [value]
return implicit_facets
def render_index(self, properties, suffix, accept_parameters):
# call the render engine
template_factory = TemplateFactory(self.config)
t = template_factory.get_template_engine()
return t.render_index(properties, suffix, accept_parameters)