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

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

Use UniqueField? index for code as suggested in ticket #31.

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