修订版 | 5e9a7469520b4c80ea88835d9b215b150980f098 (tree) |
---|---|
时间 | 2018-01-11 10:56:00 |
作者 | yamat0jp <yamat0jp@yaho...> |
Commiter | yamat0jp |
‘and or’ search
@@ -318,7 +318,8 @@ class UserHandler(web.RequestHandler): | ||
318 | 318 | class SearchHandler(web.RequestHandler): |
319 | 319 | def post(self,dbname): |
320 | 320 | arg = self.get_argument('word1') |
321 | - self.word = arg | |
321 | + self.word = arg[:] | |
322 | + self.andor = self.get_argument('type') | |
322 | 323 | self.radiobox = self.get_argument('filter') |
323 | 324 | rec = sorted(self.search(dbname),key=lambda x: x['number']) |
324 | 325 | self.render('modules/search.htm',records=rec,word1=arg,db=dbname) |
@@ -331,6 +332,7 @@ class SearchHandler(web.RequestHandler): | ||
331 | 332 | |
332 | 333 | def search(self,dbname): |
333 | 334 | table = self.application.db[dbname] |
335 | + andor = self.andor == 'OR' | |
334 | 336 | element = self.word.split() |
335 | 337 | if len(element) == 0: |
336 | 338 | element = [''] |
@@ -339,18 +341,25 @@ class SearchHandler(web.RequestHandler): | ||
339 | 341 | elm = [] |
340 | 342 | for i in range(3): |
341 | 343 | elm.append(re.escape(element[i])) |
342 | - if self.radiobox == 'comment': | |
343 | - for x in table.find({'$or':[{'raw':re.compile(elm[0],re.IGNORECASE)}, | |
344 | - {'raw':re.compile(elm[1],re.IGNORECASE)}, | |
345 | - {'raw':re.compile(elm[2],re.IGNORECASE)} | |
346 | - ]}): | |
344 | + if self.radiobox == 'comment': | |
345 | + query = [{'raw':re.compile(elm[0],re.IGNORECASE)}, | |
346 | + {'raw':re.compile(elm[1],re.IGNORECASE)}, | |
347 | + {'raw':re.compile(elm[2],re.IGNORECASE)} | |
348 | + ] | |
349 | + if andor: | |
350 | + result = table.find({'$or':query}) | |
351 | + color = 'yellow' | |
352 | + else: | |
353 | + result = table.find({'$and':query}) | |
354 | + color = 'aqua' | |
355 | + for x in result: | |
347 | 356 | com = '' |
348 | 357 | for text in x['raw'].splitlines(True): |
349 | 358 | if re.match(' ',text): |
350 | 359 | text = text.replace(' ',' ',1) |
351 | 360 | for i in range(3): |
352 | 361 | if element[i].lower() in text.lower(): |
353 | - com = com +'<p style=background-color:yellow>'+text+'<br></p>' | |
362 | + com = com +'<p style=background-color:'+color+'>'+text+'<br></p>' | |
354 | 363 | break |
355 | 364 | else: |
356 | 365 | com = com+'<p>'+text+'<br></p>' |