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

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