1 | """Catalog and searching components for academics stuff. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from hurry.query import Eq |
---|
5 | from zope.app.catalog.interfaces import ICatalog |
---|
6 | from zope.app.intid.interfaces import IIntIds |
---|
7 | from zope.component import getUtility |
---|
8 | from zope.component.interfaces import ComponentLookupError |
---|
9 | from waeup.sirp.interfaces import IUniversity |
---|
10 | from waeup.sirp.catalog import QueryResultItem |
---|
11 | from waeup.sirp.university.interfaces import ICourse, ICertificateCourse |
---|
12 | |
---|
13 | class CourseIndexes(grok.Indexes): |
---|
14 | grok.site(IUniversity) |
---|
15 | grok.name('courses_catalog') |
---|
16 | grok.context(ICourse) |
---|
17 | |
---|
18 | code = grok.index.Field(attribute='code') |
---|
19 | title = grok.index.Text(attribute='title') |
---|
20 | |
---|
21 | class CourseCertificatesIndexes(grok.Indexes): |
---|
22 | grok.site(IUniversity) |
---|
23 | grok.name('certcourses_catalog') |
---|
24 | grok.context(ICertificateCourse) |
---|
25 | |
---|
26 | course_code = grok.index.Field(attribute='getCourseCode') |
---|
27 | |
---|
28 | @grok.subscribe(ICourse, grok.IObjectAddedEvent) |
---|
29 | def handleCourseAdd(obj, event): |
---|
30 | """Index an added course with the local catalog. |
---|
31 | |
---|
32 | Courses are not indexed automatically, as they are not a |
---|
33 | dictionary subitem of the accompanied site object |
---|
34 | (`IUniversity`). I.e. one cannot get them by asking for |
---|
35 | ``app['FACCODE']['DEPTCODE']['COURSECODE']`` but one has to ask for |
---|
36 | ``app.faculties['FACCODE']['DEPTCODE'].courses['COURSECODE']``. |
---|
37 | |
---|
38 | Once, a course is indexed we can leave the further handling to |
---|
39 | the default component architechture. At least removals will |
---|
40 | be handled correctly then (and the course unindexed). |
---|
41 | """ |
---|
42 | try: |
---|
43 | cat = getUtility(ICatalog, name='courses_catalog') |
---|
44 | except ComponentLookupError: |
---|
45 | # catalog not available. This might happen during tests. |
---|
46 | return |
---|
47 | intids = getUtility(IIntIds) |
---|
48 | index = cat['code'] |
---|
49 | index.index_doc(intids.getId(obj), obj) |
---|
50 | |
---|
51 | class CourseQueryResultItem(QueryResultItem): |
---|
52 | def __init__(self, context, view): |
---|
53 | self.context = context |
---|
54 | self.url = view.url(context) |
---|
55 | self.title = "COURSE: " + context.title |
---|
56 | self.description = 'code: %s' % context.code |
---|
57 | |
---|
58 | def search(query=None, view=None): |
---|
59 | if not query: |
---|
60 | return [] |
---|
61 | cat = getUtility(ICatalog, name='courses_catalog') |
---|
62 | results = list(cat.searchResults(code=(query, query))) |
---|
63 | |
---|
64 | hitlist = [] |
---|
65 | results = Query().searchResults( |
---|
66 | Eq(('courses_catalog', 'code'), query)) |
---|
67 | for result in results: |
---|
68 | hitlist.append(CourseQueryResultItem(result, view=view)) |
---|
69 | |
---|
70 | results = Query().searchResults( |
---|
71 | Text(('courses_catalog', 'title'), query)) |
---|
72 | |
---|
73 | for result in results: |
---|
74 | hitlist.append(CourseQueryResultItem(result, view=view)) |
---|
75 | |
---|
76 | return hitlist |
---|