-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.py
276 lines (219 loc) · 7.51 KB
/
views.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# -*- coding: utf-8 -*-
# Copyright 2005,2006,2007,2008 Spike^ekipS <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os, sys
from django import template
from django.db import models
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response as render_to_response_django, get_object_or_404
from django.utils.translation import ugettext as _
from django.views.generic.list_detail import object_list, object_detail
import models as models_search
import forms as forms_search
import core, pylucene, utils, constant, document
def check_auth (func) :
def wrapper (request, **kwargs) :
if not request.user.is_staff :
#return HttpResponse(content="<h1>UnAuthorized Request</h1>", status=401)
return HttpResponseRedirect("/admin/")
return func(request, **kwargs)
wrapper.func_name = func.func_name
wrapper.__doc__ = func.__doc__
return wrapper
def match_func_by_method (func) :
def wrapper (request, **kwargs) :
try :
if kwargs.has_key("_method") and kwargs.get("_method") in ("GET", "POST", "PUT", "DELETE", ) :
__method = kwargs.get("_method")
elif request.META["REQUEST_METHOD"] not in ("GET", "POST", "PUT", "DELETE", ) :
__method = "POST"
else :
__method = request.META["REQUEST_METHOD"]
__name = "%s_%s" % \
( \
__method.lower(), \
func.func_name \
)
if not func.func_globals.has_key(__name) :
return func(request, **kwargs)
else :
return func.func_globals.get(__name)(request, getattr(request, __method).copy(), **kwargs)
except Exception, e :
raise
wrapper.func_name = func.func_name
wrapper.__doc__ = func.__doc__
return wrapper
def render_to_response (request, *args, **kwargs) :
kwargs.update(
{"context_instance": template.RequestContext(request), }
)
return render_to_response_django(*args, **kwargs)
@check_auth
def execute (request, command=None, app_label=None, index_name=None, ) :
"""
Run the internal lucene jobs. See more details, see the methods of pylucene.IndexManager.
"""
if command == "search" :
return search(request, app_label=app_label, index_name=index_name, )
if not model_name :
sys.INDEX_MANAGER.execute(command)
else :
args = list()
if command == "clean" :
command = "unindex_by_term"
term = pylucene.Term.new(constant.FIELD_NAME_MODEL, model_name)
args.append(term)
sys.INDEX_MANAGER.execute(command, *args)
return HttpResponseRedirect(
os.path.normpath(os.path.join(request.META.get("PATH_INFO"), "../",)) + "/")
@check_auth
def index (request, *args, **kwargs) :
"""
Frontpage of Django search with Lucene.
"""
if kwargs.get("redirect", None) :
return HttpResponseRedirect(os.path.normpath(os.path.join(request.META.get("PATH_INFO"), kwargs.get("redirect"))) + "/")
index_model_list = list()
for k, v in sys.MODELS_REGISTERED.items() :
for i in v :
index_model_list.append(i)
return render_to_response(
request,
"search_admin_index.html",
{
"opts": models_search.Search._meta,
"index_model_list": index_model_list,
"reader": pylucene.Reader(),
"form": forms_search.Search(),
}
)
@check_auth
def model_view (request, app_label, index_name) :
"""
Object list of model.
"""
index_model = utils.get_index_model(app_label, index_name)
if not index_model :
raise Http404
try :
page = int(request.GET.get("page", 1))
except :
page = 1
if page < 1 : page = 1
return object_list(
request,
queryset=index_model.objects.all(),
paginate_by=20,
page=page,
allow_empty=True,
extra_context={
"opts": models_search.Search._meta,
"opts_model": index_model,
"form": forms_search.Search(),
},
template_name="search_admin_model.html",
)
@check_auth
def model_object_view (request, app_label, index_name, object_id) :
"""
Model object details
"""
index_model = utils.get_index_model(app_label, index_name, )
if not index_model :
raise Http404
_o = index_model.objects.all().get(pk=object_id, )
return object_detail(
request,
queryset=index_model.objects.all(),
object_id=object_id,
template_name="search_admin_model_object.html",
extra_context={
"opts": models_search.Search._meta,
"opts_model": index_model,
},
)
class ObjectList (list) :
def __init__ (self, query=None) :
self.query = query
def extend_by_hit (self, hits, query=None) :
self.query = query
self.extend(
[document.Document(i, query=query) for i in hits]
)
def set_raw_query (self, query) :
self.query = pylucene.parse_query(query)
return self.query
def get_raw_query (self) :
return self.query
def _clone (self) :
return self
def count (self) :
return len(self)
@check_auth
def search (request, app_label=None, index_name=None) :
"""
Search the index by raw lucene query.
"""
argument = request.POST.copy()
try :
page = int(argument.get("page", 0))
except :
page = 1
if page < 1 : page = 1
form = forms_search.Search(argument)
raw_query = argument.get("query")
error = None
queryset = ObjectList()
if form.is_valid() :
if app_label and index_name :
index_model = utils.get_index_model(app_label, index_name)
try :
queryset = index_model.objects.raw_query(raw_query)
except Exception, e :
error = "parsing_error"
else :
try :
query = pylucene.Query.parse(raw_query)
except Exception, e :
error = "parsing_error"
else :
searcher = pylucene.Searcher()
queryset.extend_by_hit(list(searcher.search(query)), query)
searcher.close()
return object_list(
request,
queryset=queryset,
paginate_by=20,
page=page,
allow_empty=True,
extra_context={
"in_search": True,
"form": form,
"queryset": queryset,
"opts": models_search.Search._meta,
"error": error,
},
template_name="search_admin_search.html",
)
"""
Description
-----------
ChangeLog
---------
Usage
-----
"""
__author__ = "Spike^ekipS <[email protected]>"