1 | """Cataloging and searching components for students. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from grok import index |
---|
5 | from hurry.query import Eq, Text |
---|
6 | from hurry.query.query import Query |
---|
7 | from zope.index.text.parsetree import ParseError |
---|
8 | from waeup.sirp.interfaces import IUniversity, IQueryResultItem |
---|
9 | from waeup.sirp.students.interfaces import IStudent |
---|
10 | |
---|
11 | class StudentIndexes(grok.Indexes): |
---|
12 | """A catalog for students. |
---|
13 | """ |
---|
14 | grok.site(IUniversity) |
---|
15 | grok.name('students_catalog') |
---|
16 | grok.context(IStudent) |
---|
17 | |
---|
18 | student_id = index.Field(attribute='student_id') |
---|
19 | name = index.Text(attribute='name') |
---|
20 | #history = index.Text(attribute='history') |
---|
21 | #state = index.Field(attribute='state') |
---|
22 | |
---|
23 | class StudentQueryResultItem(object): |
---|
24 | grok.implements(IQueryResultItem) |
---|
25 | |
---|
26 | title = u'Student Query Item' |
---|
27 | description = u'Some students found in a search' |
---|
28 | |
---|
29 | def __init__(self, context, view): |
---|
30 | self.context = context |
---|
31 | self.url = view.url(context) |
---|
32 | self.student_id = context.student_id |
---|
33 | self.name = context.name |
---|
34 | #self.history = context.history |
---|
35 | #self.state = context.state |
---|
36 | |
---|
37 | def search(query=None, searchtype=None, view=None): |
---|
38 | hitlist = [] |
---|
39 | #if not query: |
---|
40 | # view.flash('Empty search string.') |
---|
41 | # return |
---|
42 | if searchtype in ('history','name'): |
---|
43 | results = Query().searchResults( |
---|
44 | Text(('students_catalog', searchtype), query)) |
---|
45 | else: |
---|
46 | # Temporary solution to display all students added |
---|
47 | if query == '*': |
---|
48 | from zope.component import queryUtility |
---|
49 | from zope.catalog.interfaces import ICatalog |
---|
50 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
51 | results = cat.searchResults(student_id=(None, None)) |
---|
52 | else: |
---|
53 | results = Query().searchResults( |
---|
54 | Eq(('students_catalog', searchtype), query)) |
---|
55 | for result in results: |
---|
56 | hitlist.append(StudentQueryResultItem(result, view=view)) |
---|
57 | return hitlist |
---|