1 | """Catalog and searching components for academics stuff. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from hurry.query import Eq |
---|
5 | from zope.catalog.interfaces import ICatalog |
---|
6 | from zope.component import getUtility |
---|
7 | from zope.component.interfaces import ComponentLookupError |
---|
8 | from zope.intid import IIntIds |
---|
9 | from waeup.sirp.catalog import QueryResultItem |
---|
10 | from waeup.sirp.index import UniqueField |
---|
11 | from waeup.sirp.interfaces import IUniversity |
---|
12 | from waeup.sirp.university.interfaces import ( |
---|
13 | ICourse, ICertificateCourse, IDepartment, |
---|
14 | ICertificate, |
---|
15 | ) |
---|
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 = UniqueField(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 = UniqueField(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 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 | |
---|
101 | def 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 |
---|