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

Last change on this file since 6779 was 6778, checked in by Henrik Bettermann, 14 years ago

Remove index package. We don't need UniqueFields? since they do not really help us.

File size: 4.5 KB
Line 
1"""Catalog and searching components for academics stuff.
2"""
3import grok
4from hurry.query import Eq, Text
5from hurry.query.query import Query
6from zope.catalog.interfaces import ICatalog
7from zope.component import getUtility
8from zope.component.interfaces import ComponentLookupError
9from zope.index.text.parsetree import ParseError
10from zope.intid import IIntIds
11#from waeup.sirp.catalog import QueryResultItem
12from waeup.sirp.interfaces import IUniversity, IQueryResultItem
13from waeup.sirp.university.interfaces import (
14    ICourse, ICertificateCourse, IDepartment,
15    ICertificate,
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 = grok.index.Field(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 = grok.index.Field(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 CoursesQueryResultItem(object):
95    grok.implements(IQueryResultItem)
96
97    def __init__(self, context, view):
98        self.context = context
99        self.url = view.url(context)
100        self.title = context.title
101        self.code = context.code
102        self.type = 'Course'
103
104class CertificateCoursesQueryResultItem(object):
105    grok.implements(IQueryResultItem)
106
107    def __init__(self, context, view):
108        self.context = context
109        self.url = view.url(context)
110        self.title = context.course.title
111        self.code = context.getCourseCode
112        self.type = 'Course Referrer'
113
114def search(query=None, view=None):
115    if not query:
116        view.flash('Empty search string.')
117        return
118
119    hitlist = []
120    try:
121        results = Query().searchResults(
122            Eq(('courses_catalog', 'code'), query))
123        for result in results:
124            hitlist.append(CoursesQueryResultItem(result, view=view))
125        results = Query().searchResults(
126            Text(('courses_catalog', 'title'), query))
127        for result in results:
128            hitlist.append(CoursesQueryResultItem(result, view=view))
129
130        results = Query().searchResults(
131            Eq(('certcourses_catalog', 'course_code'), query))
132        for result in results:
133            hitlist.append(CertificateCoursesQueryResultItem(result, view=view))
134    except ParseError:
135        view.flash('Search string not allowed.')
136        return
137    return hitlist
Note: See TracBrowser for help on using the repository browser.