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.catalog.interfaces import ICatalog |
---|
8 | from zope.component import queryUtility |
---|
9 | from waeup.sirp.interfaces import IUniversity, IQueryResultItem |
---|
10 | from waeup.sirp.students.interfaces import IStudent |
---|
11 | |
---|
12 | class StudentIndexes(grok.Indexes): |
---|
13 | """A catalog for students. |
---|
14 | """ |
---|
15 | grok.site(IUniversity) |
---|
16 | grok.name('students_catalog') |
---|
17 | grok.context(IStudent) |
---|
18 | |
---|
19 | student_id = index.Field(attribute='student_id') |
---|
20 | fullname = index.Text(attribute='fullname') |
---|
21 | reg_number = index.Field(attribute='reg_number') |
---|
22 | matric_number = index.Field(attribute='matric_number') |
---|
23 | state = index.Field(attribute='state') |
---|
24 | certificate = index.Field(attribute='certificate') |
---|
25 | |
---|
26 | class StudentQueryResultItem(object): |
---|
27 | grok.implements(IQueryResultItem) |
---|
28 | |
---|
29 | title = u'Student Query Item' |
---|
30 | description = u'Some student found in a search' |
---|
31 | |
---|
32 | def __init__(self, context, view): |
---|
33 | self.context = context |
---|
34 | self.url = view.url(context) |
---|
35 | self.student_id = context.student_id |
---|
36 | self.fullname = context.fullname |
---|
37 | self.reg_number = context.reg_number |
---|
38 | self.matric_number = context.matric_number |
---|
39 | self.state = context.state |
---|
40 | |
---|
41 | def search(query=None, searchtype=None, view=None): |
---|
42 | hitlist = [] |
---|
43 | if searchtype in ('fullname',): |
---|
44 | results = Query().searchResults( |
---|
45 | Text(('students_catalog', searchtype), query)) |
---|
46 | else: |
---|
47 | # Temporary solution to display all students added |
---|
48 | if query == '*': |
---|
49 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
50 | results = cat.searchResults(student_id=(None, None)) |
---|
51 | else: |
---|
52 | results = Query().searchResults( |
---|
53 | Eq(('students_catalog', searchtype), query)) |
---|
54 | for result in results: |
---|
55 | hitlist.append(StudentQueryResultItem(result, view=view)) |
---|
56 | return hitlist |
---|
57 | |
---|
58 | class SimpleFieldSearch(object): |
---|
59 | """A programmatic (no UI required) search. |
---|
60 | |
---|
61 | Looks up a given field attribute of the students catalog for a |
---|
62 | single value. So normally you would call an instance of this |
---|
63 | search like this: |
---|
64 | |
---|
65 | >>> SimpleFieldSearch()(reg_number='somevalue') |
---|
66 | |
---|
67 | """ |
---|
68 | catalog_name = 'students_catalog' |
---|
69 | def __call__(self, **kw): |
---|
70 | """Search students catalog programmatically. |
---|
71 | """ |
---|
72 | if len(kw) != 1: |
---|
73 | raise ValueError('must give exactly one index name to search') |
---|
74 | cat = queryUtility(ICatalog, name=self.catalog_name) |
---|
75 | index_name, query_term = kw.items()[0] |
---|
76 | results = cat.searchResults(index_name=(query_term, query_term)) |
---|
77 | return results |
---|
78 | |
---|
79 | #: an instance of `SimpleFieldSearch` looking up students catalog. |
---|
80 | simple_search = SimpleFieldSearch() |
---|