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.interfaces import IUniversity |
---|
10 | from waeup.sirp.catalog import QueryResultItem |
---|
11 | from waeup.sirp.university.interfaces import ( |
---|
12 | ICourse, ICertificateCourse, IDepartment, |
---|
13 | ICertificate, |
---|
14 | ) |
---|
15 | from waeup.sirp.index import UniqueField |
---|
16 | |
---|
17 | class 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 | |
---|
27 | class 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 | |
---|
37 | class 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) |
---|
48 | def 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) |
---|
71 | def 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) |
---|
86 | def 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 | |
---|
93 | class 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 | |
---|
100 | def 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 |
---|