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

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

Reorder imports.

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