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

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

Add certificates catalog.

File size: 3.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    ICertificate,
14    )
15
16class CourseIndexes(grok.Indexes):
17    """This catalog is needed for building sources.
18    """
19    grok.site(IUniversity)
20    grok.name('courses_catalog')
21    grok.context(ICourse)
22
23    code = grok.index.Field(attribute='code')
24    title = grok.index.Text(attribute='title')
25
26class CertificatesIndexes(grok.Indexes):
27    """This catalog is needed for building sources.
28    """
29    grok.site(IUniversity)
30    grok.name('certifcates_catalog')
31    grok.context(ICertificate)
32
33    code = grok.index.Field(attribute='code')
34    title = grok.index.Field(attribute='application_category')
35
36class CertificateCoursesIndexes(grok.Indexes):
37    """This catalog is needed for automatic removal of certificate courses
38    and later for selection course tickets in the students section.
39    """
40    grok.site(IUniversity)
41    grok.name('certcourses_catalog')
42    grok.context(ICertificateCourse)
43
44    course_code = grok.index.Field(attribute='getCourseCode')
45
46@grok.subscribe(ICourse, grok.IObjectAddedEvent)
47def handleCourseAdd(obj, event):
48    """Index an added course with the local catalog.
49
50    Courses are not indexed automatically, as they are not a
51    dictionary subitem of the accompanied site object
52    (`IUniversity`). I.e. one cannot get them by asking for
53    ``app['FACCODE']['DEPTCODE']['COURSECODE']`` but one has to ask for
54    ``app.faculties['FACCODE']['DEPTCODE'].courses['COURSECODE']``.
55
56    Once, a course is indexed we can leave the further handling to
57    the default component architechture. At least removals will
58    be handled correctly then (and the course unindexed).
59    """
60    try:
61        cat = getUtility(ICatalog, name='courses_catalog')
62    except ComponentLookupError:
63        # catalog not available. This might happen during tests.
64        return
65    intids = getUtility(IIntIds)
66    index = cat['code']
67    index.index_doc(intids.getId(obj), obj)
68
69@grok.subscribe(ICourse, grok.IObjectAddedEvent)
70def handleCertificateAdd(obj, event):
71    """Index an added certificates with the local catalog.
72
73    See handleCourseAdd.
74    """
75    try:
76      cat = getUtility(ICatalog, name='certificates_catalog')
77    except ComponentLookupError:
78      # catalog not available. This might happen during tests.
79      return
80    intids = getUtility(IIntIds)
81    index = cat['code']
82    index.index_doc(intids.getId(obj), obj)
83
84@grok.subscribe(IDepartment, grok.IObjectRemovedEvent)
85def handleDepartmentRemoval(obj, event):
86    """Clear courses and certificates when a department is killed.
87    """
88    obj.courses.clear()
89    obj.certificates.clear()
90    return
91
92class CourseQueryResultItem(QueryResultItem):
93    def __init__(self, context, view):
94        self.context = context
95        self.url = view.url(context)
96        self.title = "COURSE: " + context.title
97        self.description = 'code: %s' % context.code
98
99def search(query=None, view=None):
100    if not query:
101        return []
102    cat = getUtility(ICatalog, name='courses_catalog')
103    results = list(cat.searchResults(code=(query, query)))
104
105    hitlist = []
106    results = Query().searchResults(
107        Eq(('courses_catalog', 'code'), query))
108    for result in results:
109        hitlist.append(CourseQueryResultItem(result, view=view))
110
111    results = Query().searchResults(
112        Text(('courses_catalog', 'title'), query))
113
114    for result in results:
115        hitlist.append(CourseQueryResultItem(result, view=view))
116
117    return hitlist
Note: See TracBrowser for help on using the repository browser.