1 | """Catalog and searching components for academics stuff. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from hurry.query import Eq, Text |
---|
5 | from hurry.query.query import Query |
---|
6 | from zope.catalog.interfaces import ICatalog |
---|
7 | from zope.component import getUtility |
---|
8 | from zope.component.interfaces import ComponentLookupError |
---|
9 | from zope.index.text.parsetree import ParseError |
---|
10 | from zope.intid import IIntIds |
---|
11 | #from waeup.sirp.catalog import QueryResultItem |
---|
12 | from waeup.sirp.interfaces import IUniversity, IQueryResultItem |
---|
13 | from waeup.sirp.university.interfaces import ( |
---|
14 | ICourse, ICertificateCourse, IDepartment, |
---|
15 | ICertificate, |
---|
16 | ) |
---|
17 | |
---|
18 | class 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 | |
---|
28 | class 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 | |
---|
38 | class 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) |
---|
49 | def 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) |
---|
72 | def 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) |
---|
87 | def 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 | |
---|
94 | class 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 | |
---|
104 | class 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 | |
---|
114 | def 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 |
---|