From 49757e825b725f4f3957c2bde1c12aa59166d98a Mon Sep 17 00:00:00 2001 From: artunit Date: Mon, 1 Dec 2008 19:01:20 +0000 Subject: [PATCH] git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@68 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/syrup/models.py | 26 ++++++++++++++++++++++++++ conifer/syrup/views.py | 24 +++++------------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index 4988a21..6361139 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -2,6 +2,24 @@ from django.db import models as m from django.contrib.auth.models import User from django.contrib.auth.models import AnonymousUser from datetime import datetime +import re + +def highlight(text, phrase, + highlighter='\\1'): + ''' This may be a lame way to do this, but will want to highlight matches somehow + + >>> highlight('The River is Wide', 'wide') + 'The River is Wide' + + ''' + if not phrase or not text: + return text + highlight_re = re.compile('(%s)' % re.escape(phrase), re.I) + if hasattr(text, '__html__'): + return literal(highlight_re.sub(highlighter, text)) + else: + return highlight_re.sub(highlighter, text) + #---------------------------------------------------------------------- # USERS @@ -159,6 +177,14 @@ class Member(m.Model): default = 'STUDT', max_length = 5) + def instr_name_hl(self, terms): + for term in terms: + hl_instr = highlight(self.user.last_name,term) + if not hl_instr == self.user.last_name: + return hl_instr + + return self.user.last_name + def instr_name(self): return self.user.last_name diff --git a/conifer/syrup/views.py b/conifer/syrup/views.py index 16c8ba8..83ec58e 100644 --- a/conifer/syrup/views.py +++ b/conifer/syrup/views.py @@ -4,9 +4,9 @@ from django.shortcuts import get_object_or_404 from django.contrib.auth.decorators import login_required from django.contrib.auth import authenticate, login, logout import conifer.genshi_support as g +import re from conifer.syrup import models from django.contrib.auth.models import User -import re from django.db.models import Q @@ -142,22 +142,6 @@ def get_query(query_string, search_fields): #------------------------------------------------------------ -def highlight(text, phrase, - highlighter='\\1'): - ''' This may be a lame way to do this, but will want to highlight matches somehow - - >>> highlight('The River is Wide', 'wide') - 'The River is Wide' - - ''' - if not phrase or not text: - return text - highlight_re = re.compile('(%s)' % re.escape(phrase), re.I) - if hasattr(text, '__html__'): - return literal(highlight_re.sub(highlighter, text)) - else: - return highlight_re.sub(highlighter, text) - def search(request): ''' Need to work on this @@ -178,9 +162,11 @@ def search(request): for term in norm_query: print term print len(models.Member.objects.filter(instr_query).filter(role='INSTR')) - print highlight('The River Is Wide', 'is wide') + instr_len = len(models.Member.objects.filter(instr_query).filter(role='INSTR')) + #print highlight('The River Is Wide', 'is wide') return g.render('search_results.xhtml', paginator=paginator, page_num=page_num, - count=count, query_string=query_string, instructor_list=instructor_list) + count=count, query_string=query_string, instructor_list=instructor_list, + norm_query=norm_query, instr_len=instr_len) -- 2.11.0