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

Last change on this file since 6207 was 6207, checked in by Henrik Bettermann, 13 years ago

Fix indentation. We use only 4 whitespaces not 5. Remove #BBB.

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