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