From 068c043e032d17a52d53d004998b47d0386b97fc Mon Sep 17 00:00:00 2001 From: artunit Date: Mon, 1 Dec 2008 17:00:54 +0000 Subject: [PATCH] git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@67 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/syrup/models.py | 3 +++ conifer/syrup/views.py | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index 7b601fa..4988a21 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -159,6 +159,9 @@ class Member(m.Model): default = 'STUDT', max_length = 5) + def instr_name(self): + return self.user.last_name + def __unicode__(self): return '%s--%s--%s' % (self.user, self.role, self.course) diff --git a/conifer/syrup/views.py b/conifer/syrup/views.py index bb71e98..16c8ba8 100644 --- a/conifer/syrup/views.py +++ b/conifer/syrup/views.py @@ -9,6 +9,7 @@ from django.contrib.auth.models import User import re from django.db.models import Q + #------------------------------------------------------------ def auth_handler(request, path): @@ -139,6 +140,24 @@ def get_query(query_string, search_fields): query = query & or_query return query +#------------------------------------------------------------ + +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 @@ -149,11 +168,17 @@ def search(request): count = int(request.GET.get('count', 5)) if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'] + norm_query = normalize_query(query_string) entry_query = get_query(query_string, ['title', 'author', 'course__title', 'course__department__name']) instr_query = get_query(query_string, ['user__last_name']) paginator = Paginator( models.Item.objects.filter(entry_query).order_by('-date_created'), count) - instructor_list = models.Member.objects.filter(instr_query).filter(role='INSTR').order_by('-user__last_name') + instructor_list = models.Member.objects.filter(instr_query).filter(role='INSTR').order_by('-user__last_name')[0:5] + print(norm_query) + 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') return g.render('search_results.xhtml', paginator=paginator, page_num=page_num, -- 2.11.0