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

Last change on this file since 5992 was 5992, checked in by uli, 13 years ago

Clear certificate and course containers of departments explicitly when
removing a department.

File size: 3.1 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 (
20     ICourse, ICertificateCourse, IDepartment,
21     )
22
23class CourseIndexes(grok.Indexes):
24     grok.site(IUniversity)
25     grok.name('courses_catalog')
26     grok.context(ICourse)
27
28     code = grok.index.Field(attribute='code')
29     title = grok.index.Text(attribute='title')
30
31class CourseCertificatesIndexes(grok.Indexes):
32     grok.site(IUniversity)
33     grok.name('certcourses_catalog')
34     grok.context(ICertificateCourse)
35
36     course_code = grok.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
61@grok.subscribe(IDepartment, grok.IObjectRemovedEvent)
62def handleDepartmentRemoval(obj, event):
63     """Clear courses and certificates when a department is killed.
64     """
65     obj.courses.clear()
66     obj.certificates.clear()
67     return
68
69class CourseQueryResultItem(QueryResultItem):
70     def __init__(self, context, view):
71          self.context = context
72          self.url = view.url(context)
73          self.title = "COURSE: " + context.title
74          self.description = 'code: %s' % context.code
75
76def search(query=None, view=None):
77     if not query:
78          return []
79     cat = getUtility(ICatalog, name='courses_catalog')
80     results = list(cat.searchResults(code=(query, query)))
81         
82     hitlist = []
83     results = Query().searchResults(
84          Eq(('courses_catalog', 'code'), query))
85     for result in results:
86          hitlist.append(CourseQueryResultItem(result, view=view))
87
88     results = Query().searchResults(
89          Text(('courses_catalog', 'title'), query))
90         
91     for result in results:
92          hitlist.append(CourseQueryResultItem(result, view=view))
93
94     return hitlist
Note: See TracBrowser for help on using the repository browser.