1 | import os |
---|
2 | import grok |
---|
3 | from grok import index |
---|
4 | from hurry.query.query import Query, Text |
---|
5 | from hurry.query import Eq |
---|
6 | from waeup.sirp.app import University |
---|
7 | from waeup.sirp.interfaces import ICourse, ICertificateCourse |
---|
8 | from waeup.sirp.student.interfaces import IStudent |
---|
9 | from zope.app.catalog.interfaces import ICatalog |
---|
10 | from zope.app.intid.interfaces import IIntIds |
---|
11 | from zope.component import getUtility |
---|
12 | from zope.component.interfaces import ComponentLookupError |
---|
13 | |
---|
14 | |
---|
15 | class StudentIndexes(grok.Indexes): |
---|
16 | grok.site(University) |
---|
17 | grok.name('students_catalog') |
---|
18 | grok.context(IStudent) |
---|
19 | |
---|
20 | name = index.Field(attribute='name') |
---|
21 | |
---|
22 | |
---|
23 | class CourseIndexes(grok.Indexes): |
---|
24 | grok.site(University) |
---|
25 | grok.name('courses_catalog') |
---|
26 | grok.context(ICourse) |
---|
27 | |
---|
28 | code = index.Field(attribute='code') |
---|
29 | title = index.Text(attribute='title') |
---|
30 | |
---|
31 | class CourseCertificatesIndexes(grok.Indexes): |
---|
32 | grok.site(University) |
---|
33 | grok.name('certcourses_catalog') |
---|
34 | grok.context(ICertificateCourse) |
---|
35 | |
---|
36 | course_code = index.Field(attribute='getCourseCode') |
---|
37 | |
---|
38 | @grok.subscribe(ICourse, grok.IObjectAddedEvent) |
---|
39 | def handleCourseAdd(obj, event): |
---|
40 | """Index an added course with the local catalog. |
---|
41 | |
---|
42 | Courses are not indexed automatically, as they are not a |
---|
43 | dictionary subitem of the accompanied site object |
---|
44 | (`IUniversity`). I.e. one cannot get them by asking for |
---|
45 | ``app['FACCODE']['DEPTCODE']['COURSECODE']`` but one has to ask for |
---|
46 | ``app.faculties['FACCODE']['DEPTCODE'].courses['COURSECODE']``. |
---|
47 | |
---|
48 | Once, a course is indexed we can leave the further handling to |
---|
49 | the default component architechture. At least removals will |
---|
50 | be handled correctly then (and the course unindexed). |
---|
51 | """ |
---|
52 | try: |
---|
53 | cat = getUtility(ICatalog, name='courses_catalog') |
---|
54 | except ComponentLookupError: |
---|
55 | # catalog not available. This might happen during tests. |
---|
56 | return |
---|
57 | intids = getUtility(IIntIds) |
---|
58 | index = cat['code'] |
---|
59 | index.index_doc(intids.getId(obj), obj) |
---|
60 | |
---|
61 | from zope.interface import Interface |
---|
62 | from zope import schema |
---|
63 | |
---|
64 | class IQueryResultItem(Interface): |
---|
65 | url = schema.TextLine( |
---|
66 | title = u'URL that links to the found item') |
---|
67 | title = schema.TextLine( |
---|
68 | title = u'Title displayed in search results.') |
---|
69 | description = schema.Text( |
---|
70 | title = u'Longer description of the item found.') |
---|
71 | |
---|
72 | |
---|
73 | class QueryResultItem(object): |
---|
74 | grok.implements(IQueryResultItem) |
---|
75 | url = None |
---|
76 | title = None |
---|
77 | description = None |
---|
78 | |
---|
79 | def __init__(self, context, view): |
---|
80 | self.context = context |
---|
81 | self.url = view.url(context) |
---|
82 | self.title = context.title |
---|
83 | self.description = '' |
---|
84 | |
---|
85 | class CourseQueryResultItem(QueryResultItem): |
---|
86 | def __init__(self, context, view): |
---|
87 | self.context = context |
---|
88 | self.url = view.url(context) |
---|
89 | self.title = "COURSE: " + context.title |
---|
90 | self.description = 'code: %s' % context.code |
---|
91 | |
---|
92 | def search(query=None, view=None): |
---|
93 | if not query: |
---|
94 | return [] |
---|
95 | cat = getUtility(ICatalog, name='courses_catalog') |
---|
96 | results = list(cat.searchResults(code=(query, query))) |
---|
97 | |
---|
98 | hitlist = [] |
---|
99 | results = Query().searchResults( |
---|
100 | Eq(('courses_catalog', 'code'), query)) |
---|
101 | for result in results: |
---|
102 | hitlist.append(CourseQueryResultItem(result, view=view)) |
---|
103 | |
---|
104 | results = Query().searchResults( |
---|
105 | Text(('courses_catalog', 'title'), query)) |
---|
106 | |
---|
107 | for result in results: |
---|
108 | hitlist.append(CourseQueryResultItem(result, view=view)) |
---|
109 | |
---|
110 | return hitlist |
---|
111 | |
---|
112 | def search_context(query): |
---|
113 | result = Query().searchResults( |
---|
114 | Eq(('students_catalog', 'name'), query) |
---|
115 | ) |
---|
116 | return result |
---|