source: main/waeup.sirp/branches/ulif-experimental-folders/src/waeup/sirp/university/catalog.py @ 11107

Last change on this file since 11107 was 5053, checked in by uli, 15 years ago

BBB imports for older groktoolkit.

File size: 2.8 KB
Line 
1"""Catalog and searching components for academics stuff.
2"""
3import grok
4from hurry.query import Eq
5try:
6     from zope.catalog.interfaces import ICatalog
7except ImportError:
8     # BBB
9     from zope.app.catalog.interfaces import ICatalog
10from zope.component import getUtility
11from zope.component.interfaces import ComponentLookupError
12try:
13     from zope.intid import IIntIds
14except ImportError:
15     # BBB
16     from zope.app.intid.interfaces import IIntIds
17from waeup.sirp.interfaces import IUniversity
18from waeup.sirp.catalog import QueryResultItem
19from waeup.sirp.university.interfaces import ICourse, ICertificateCourse
20
21class CourseIndexes(grok.Indexes):
22     grok.site(IUniversity)
23     grok.name('courses_catalog')
24     grok.context(ICourse)
25
26     code = grok.index.Field(attribute='code')
27     title = grok.index.Text(attribute='title')
28
29class CourseCertificatesIndexes(grok.Indexes):
30     grok.site(IUniversity)
31     grok.name('certcourses_catalog')
32     grok.context(ICertificateCourse)
33
34     course_code = grok.index.Field(attribute='getCourseCode')
35
36@grok.subscribe(ICourse, grok.IObjectAddedEvent)
37def handleCourseAdd(obj, event):
38     """Index an added course with the local catalog.
39
40     Courses are not indexed automatically, as they are not a
41     dictionary subitem of the accompanied site object
42     (`IUniversity`). I.e. one cannot get them by asking for
43     ``app['FACCODE']['DEPTCODE']['COURSECODE']`` but one has to ask for
44     ``app.faculties['FACCODE']['DEPTCODE'].courses['COURSECODE']``.
45
46     Once, a course is indexed we can leave the further handling to
47     the default component architechture. At least removals will
48     be handled correctly then (and the course unindexed).
49     """
50     try:
51          cat = getUtility(ICatalog, name='courses_catalog')
52     except ComponentLookupError:
53          # catalog not available. This might happen during tests.
54          return
55     intids = getUtility(IIntIds)
56     index = cat['code']
57     index.index_doc(intids.getId(obj), obj)
58
59class CourseQueryResultItem(QueryResultItem):
60     def __init__(self, context, view):
61          self.context = context
62          self.url = view.url(context)
63          self.title = "COURSE: " + context.title
64          self.description = 'code: %s' % context.code
65
66def search(query=None, view=None):
67     if not query:
68          return []
69     cat = getUtility(ICatalog, name='courses_catalog')
70     results = list(cat.searchResults(code=(query, query)))
71         
72     hitlist = []
73     results = Query().searchResults(
74          Eq(('courses_catalog', 'code'), query))
75     for result in results:
76          hitlist.append(CourseQueryResultItem(result, view=view))
77
78     results = Query().searchResults(
79          Text(('courses_catalog', 'title'), query))
80         
81     for result in results:
82          hitlist.append(CourseQueryResultItem(result, view=view))
83
84     return hitlist
Note: See TracBrowser for help on using the repository browser.