1 | ## $Id: catalog.py 7369 2011-12-18 08:24:04Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """Cataloging and searching components for students. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from grok import index |
---|
22 | from hurry.query import Eq, Text |
---|
23 | from hurry.query.query import Query |
---|
24 | from zope.catalog.interfaces import ICatalog |
---|
25 | from zope.component import queryUtility |
---|
26 | from waeup.sirp.interfaces import ( |
---|
27 | IUniversity, IQueryResultItem, academic_sessions_vocab) |
---|
28 | from waeup.sirp.students.interfaces import IStudent |
---|
29 | from waeup.sirp.university.vocabularies import course_levels |
---|
30 | |
---|
31 | class StudentIndexes(grok.Indexes): |
---|
32 | """A catalog for students. |
---|
33 | """ |
---|
34 | grok.site(IUniversity) |
---|
35 | grok.name('students_catalog') |
---|
36 | grok.context(IStudent) |
---|
37 | |
---|
38 | student_id = index.Field(attribute='student_id') |
---|
39 | fullname = index.Text(attribute='fullname') |
---|
40 | email = index.Field(attribute='email') |
---|
41 | reg_number = index.Field(attribute='reg_number') |
---|
42 | matric_number = index.Field(attribute='matric_number') |
---|
43 | state = index.Field(attribute='state') |
---|
44 | certcode = index.Field(attribute='certcode') |
---|
45 | depcode = index.Field(attribute='depcode') |
---|
46 | faccode = index.Field(attribute='faccode') |
---|
47 | current_session = index.Field(attribute='current_session') |
---|
48 | |
---|
49 | class StudentQueryResultItem(object): |
---|
50 | grok.implements(IQueryResultItem) |
---|
51 | |
---|
52 | title = u'Student Query Item' |
---|
53 | description = u'Some student found in a search' |
---|
54 | |
---|
55 | def __init__(self, context, view): |
---|
56 | self.context = context |
---|
57 | self.url = view.url(context) |
---|
58 | self.student_id = context.student_id |
---|
59 | self.display_fullname = context.display_fullname |
---|
60 | self.reg_number = context.reg_number |
---|
61 | self.matric_number = context.matric_number |
---|
62 | self.state = context.state |
---|
63 | try: |
---|
64 | current_level = course_levels.getTerm( |
---|
65 | context['studycourse'].current_level).title |
---|
66 | except LookupError: |
---|
67 | current_level = None |
---|
68 | self.current_level = current_level |
---|
69 | try: |
---|
70 | current_session = academic_sessions_vocab.getTerm( |
---|
71 | context['studycourse'].current_session).title |
---|
72 | except LookupError: |
---|
73 | current_session = None |
---|
74 | self.current_session = current_session |
---|
75 | self.certificate = context['studycourse'].certificate |
---|
76 | |
---|
77 | def search(query=None, searchtype=None, view=None): |
---|
78 | hitlist = [] |
---|
79 | if searchtype in ('fullname',): |
---|
80 | results = Query().searchResults( |
---|
81 | Text(('students_catalog', searchtype), query)) |
---|
82 | else: |
---|
83 | # Temporary solution to display all students added |
---|
84 | if query == '*': |
---|
85 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
86 | results = cat.searchResults(student_id=(None, None)) |
---|
87 | else: |
---|
88 | results = Query().searchResults( |
---|
89 | Eq(('students_catalog', searchtype), query)) |
---|
90 | for result in results: |
---|
91 | hitlist.append(StudentQueryResultItem(result, view=view)) |
---|
92 | return hitlist |
---|
93 | |
---|
94 | class SimpleFieldSearch(object): |
---|
95 | """A programmatic (no UI required) search. |
---|
96 | |
---|
97 | Looks up a given field attribute of the students catalog for a |
---|
98 | single value. So normally you would call an instance of this |
---|
99 | search like this: |
---|
100 | |
---|
101 | >>> SimpleFieldSearch()(reg_number='somevalue') |
---|
102 | |
---|
103 | """ |
---|
104 | catalog_name = 'students_catalog' |
---|
105 | def __call__(self, **kw): |
---|
106 | """Search students catalog programmatically. |
---|
107 | """ |
---|
108 | if len(kw) != 1: |
---|
109 | raise ValueError('must give exactly one index name to search') |
---|
110 | cat = queryUtility(ICatalog, name=self.catalog_name) |
---|
111 | index_name, query_term = kw.items()[0] |
---|
112 | results = cat.searchResults(index_name=(query_term, query_term)) |
---|
113 | return results |
---|
114 | |
---|
115 | #: an instance of `SimpleFieldSearch` looking up students catalog. |
---|
116 | simple_search = SimpleFieldSearch() |
---|