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 | reg_number = index.Field(attribute='reg_number') |
---|
21 | matric_number = index.Field(attribute='matric_number') |
---|
22 | state = index.Field(attribute='state') |
---|
23 | |
---|
24 | class StudentQueryResultItem(object): |
---|
25 | grok.implements(IQueryResultItem) |
---|
26 | |
---|
27 | title = u'Student Query Item' |
---|
28 | description = u'Some students found in a search' |
---|
29 | |
---|
30 | def __init__(self, context, view): |
---|
31 | self.context = context |
---|
32 | self.url = view.url(context) |
---|
33 | self.student_id = context.student_id |
---|
34 | self.name = context.name |
---|
35 | self.reg_number = context.reg_number |
---|
36 | self.matric_number = context.matric_number |
---|
37 | self.state = context.state |
---|
38 | |
---|
39 | def search(query=None, searchtype=None, view=None): |
---|
40 | hitlist = [] |
---|
41 | #if not query: |
---|
42 | # view.flash('Empty search string.') |
---|
43 | # return |
---|
44 | if searchtype in ('name',): |
---|
45 | results = Query().searchResults( |
---|
46 | Text(('students_catalog', searchtype), query)) |
---|
47 | else: |
---|
48 | # Temporary solution to display all students added |
---|
49 | if query == '*': |
---|
50 | from zope.component import queryUtility |
---|
51 | from zope.catalog.interfaces import ICatalog |
---|
52 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
53 | results = cat.searchResults(student_id=(None, None)) |
---|
54 | else: |
---|
55 | results = Query().searchResults( |
---|
56 | Eq(('students_catalog', searchtype), query)) |
---|
57 | for result in results: |
---|
58 | hitlist.append(StudentQueryResultItem(result, view=view)) |
---|
59 | return hitlist |
---|