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 | current_session = index.Field(attribute='current_session') |
---|
26 | |
---|
27 | class StudentQueryResultItem(object): |
---|
28 | grok.implements(IQueryResultItem) |
---|
29 | |
---|
30 | title = u'Student Query Item' |
---|
31 | description = u'Some student found in a search' |
---|
32 | |
---|
33 | def __init__(self, context, view): |
---|
34 | self.context = context |
---|
35 | self.url = view.url(context) |
---|
36 | self.student_id = context.student_id |
---|
37 | self.fullname = context.fullname |
---|
38 | self.reg_number = context.reg_number |
---|
39 | self.matric_number = context.matric_number |
---|
40 | self.state = context.state |
---|
41 | |
---|
42 | def search(query=None, searchtype=None, view=None): |
---|
43 | hitlist = [] |
---|
44 | if searchtype in ('fullname',): |
---|
45 | results = Query().searchResults( |
---|
46 | Text(('students_catalog', searchtype), query)) |
---|
47 | else: |
---|
48 | # Temporary solution to display all students added |
---|
49 | if query == '*': |
---|
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 |
---|
58 | |
---|
59 | class SimpleFieldSearch(object): |
---|
60 | """A programmatic (no UI required) search. |
---|
61 | |
---|
62 | Looks up a given field attribute of the students catalog for a |
---|
63 | single value. So normally you would call an instance of this |
---|
64 | search like this: |
---|
65 | |
---|
66 | >>> SimpleFieldSearch()(reg_number='somevalue') |
---|
67 | |
---|
68 | """ |
---|
69 | catalog_name = 'students_catalog' |
---|
70 | def __call__(self, **kw): |
---|
71 | """Search students catalog programmatically. |
---|
72 | """ |
---|
73 | if len(kw) != 1: |
---|
74 | raise ValueError('must give exactly one index name to search') |
---|
75 | cat = queryUtility(ICatalog, name=self.catalog_name) |
---|
76 | index_name, query_term = kw.items()[0] |
---|
77 | results = cat.searchResults(index_name=(query_term, query_term)) |
---|
78 | return results |
---|
79 | |
---|
80 | #: an instance of `SimpleFieldSearch` looking up students catalog. |
---|
81 | simple_search = SimpleFieldSearch() |
---|