source: main/waeup.sirp/trunk/src/waeup/sirp/catalog.py @ 4971

Last change on this file since 4971 was 4920, checked in by uli, 15 years ago

Make unit tests run again with the new package layout.

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1import os
2import grok
3from grok import index
4from hurry.query.query import Query, Text
5from hurry.query import Eq
6from waeup.sirp.app import University
7from waeup.sirp.interfaces import ICourse, ICertificateCourse
8from waeup.sirp.student.interfaces import IStudent
9from zope.app.catalog.interfaces import ICatalog
10from zope.app.intid.interfaces import IIntIds
11from zope.component import getUtility
12from zope.component.interfaces import ComponentLookupError
13
14
15class StudentIndexes(grok.Indexes):
16     grok.site(University)
17     grok.name('students_catalog')
18     grok.context(IStudent)
19
20     name = index.Field(attribute='name')
21
22
23class CourseIndexes(grok.Indexes):
24     grok.site(University)
25     grok.name('courses_catalog')
26     grok.context(ICourse)
27
28     code = index.Field(attribute='code')
29     title = index.Text(attribute='title')
30
31class CourseCertificatesIndexes(grok.Indexes):
32     grok.site(University)
33     grok.name('certcourses_catalog')
34     grok.context(ICertificateCourse)
35
36     course_code = index.Field(attribute='getCourseCode')
37
38@grok.subscribe(ICourse, grok.IObjectAddedEvent)
39def handleCourseAdd(obj, event):
40     """Index an added course with the local catalog.
41
42     Courses are not indexed automatically, as they are not a
43     dictionary subitem of the accompanied site object
44     (`IUniversity`). I.e. one cannot get them by asking for
45     ``app['FACCODE']['DEPTCODE']['COURSECODE']`` but one has to ask for
46     ``app.faculties['FACCODE']['DEPTCODE'].courses['COURSECODE']``.
47
48     Once, a course is indexed we can leave the further handling to
49     the default component architechture. At least removals will
50     be handled correctly then (and the course unindexed).
51     """
52     try:
53          cat = getUtility(ICatalog, name='courses_catalog')
54     except ComponentLookupError:
55          # catalog not available. This might happen during tests.
56          return
57     intids = getUtility(IIntIds)
58     index = cat['code']
59     index.index_doc(intids.getId(obj), obj)
60
61from zope.interface import Interface
62from zope import schema
63
64class IQueryResultItem(Interface):
65     url = schema.TextLine(
66          title = u'URL that links to the found item')
67     title = schema.TextLine(
68          title = u'Title displayed in search results.')
69     description = schema.Text(
70          title = u'Longer description of the item found.')
71     
72     
73class QueryResultItem(object):
74     grok.implements(IQueryResultItem)
75     url = None
76     title = None
77     description = None
78     
79     def __init__(self, context, view):
80          self.context = context
81          self.url = view.url(context)
82          self.title = context.title
83          self.description = ''
84     
85class CourseQueryResultItem(QueryResultItem):
86     def __init__(self, context, view):
87          self.context = context
88          self.url = view.url(context)
89          self.title = "COURSE: " + context.title
90          self.description = 'code: %s' % context.code
91
92def search(query=None, view=None):
93     if not query:
94          return []
95     cat = getUtility(ICatalog, name='courses_catalog')
96     results = list(cat.searchResults(code=(query, query)))
97         
98     hitlist = []
99     results = Query().searchResults(
100          Eq(('courses_catalog', 'code'), query))
101     for result in results:
102          hitlist.append(CourseQueryResultItem(result, view=view))
103
104     results = Query().searchResults(
105          Text(('courses_catalog', 'title'), query))
106         
107     for result in results:
108          hitlist.append(CourseQueryResultItem(result, view=view))
109
110     return hitlist
111
112def search_context(query):
113     result = Query().searchResults(
114          Eq(('students_catalog', 'name'), query)
115          )
116     return result
Note: See TracBrowser for help on using the repository browser.