1 | ## $Id: catalog.py 10465 2013-08-07 11:18:43Z 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.kofa.catalog import FilteredCatalogQueryBase |
---|
27 | from waeup.kofa.interfaces import ( |
---|
28 | IUniversity, IQueryResultItem, academic_sessions_vocab) |
---|
29 | from waeup.kofa.students.interfaces import IStudent, ICourseTicket |
---|
30 | from waeup.kofa.university.vocabularies import course_levels |
---|
31 | |
---|
32 | class StudentsCatalog(grok.Indexes): |
---|
33 | """A catalog for students. |
---|
34 | """ |
---|
35 | grok.site(IUniversity) |
---|
36 | grok.name('students_catalog') |
---|
37 | grok.context(IStudent) |
---|
38 | |
---|
39 | student_id = index.Field(attribute='student_id') |
---|
40 | fullname = index.Text(attribute='fullname') |
---|
41 | email = index.Field(attribute='email') |
---|
42 | reg_number = index.Field(attribute='reg_number') |
---|
43 | matric_number = index.Field(attribute='matric_number') |
---|
44 | state = index.Field(attribute='state') |
---|
45 | certcode = index.Field(attribute='certcode') |
---|
46 | depcode = index.Field(attribute='depcode') |
---|
47 | faccode = index.Field(attribute='faccode') |
---|
48 | current_session = index.Field(attribute='current_session') |
---|
49 | current_level = index.Field(attribute='current_level') |
---|
50 | current_mode = index.Field(attribute='current_mode') |
---|
51 | |
---|
52 | class StudentQueryResultItem(object): |
---|
53 | grok.implements(IQueryResultItem) |
---|
54 | |
---|
55 | title = u'Student Query Item' |
---|
56 | description = u'Some student found in a search' |
---|
57 | |
---|
58 | def __init__(self, context, view): |
---|
59 | self.context = context |
---|
60 | self.url = view.url(context) |
---|
61 | self.student_id = context.student_id |
---|
62 | self.display_fullname = context.display_fullname |
---|
63 | self.reg_number = context.reg_number |
---|
64 | self.matric_number = context.matric_number |
---|
65 | self.state = context.state |
---|
66 | self.translated_state = context.translated_state |
---|
67 | self.current_level = context['studycourse'].current_level |
---|
68 | try: |
---|
69 | current_session = academic_sessions_vocab.getTerm( |
---|
70 | context['studycourse'].current_session).title |
---|
71 | except LookupError: |
---|
72 | current_session = None |
---|
73 | self.current_session = current_session |
---|
74 | self.certificate = context['studycourse'].certificate |
---|
75 | self.comment = getattr(context, 'officer_comment', None) |
---|
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 | elif searchtype == 'suspended': |
---|
83 | # 'suspended' is not indexed |
---|
84 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
85 | all = cat.searchResults(student_id=(None, None)) |
---|
86 | for student in all: |
---|
87 | if student.suspended: |
---|
88 | hitlist.append(StudentQueryResultItem(student, view=view)) |
---|
89 | return hitlist |
---|
90 | elif searchtype == 'transcript': |
---|
91 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
92 | results = cat.searchResults( |
---|
93 | state=('transcript requested', 'transcript requested')) |
---|
94 | else: |
---|
95 | # Temporary solution to display all students added |
---|
96 | if query == '*': |
---|
97 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
98 | results = cat.searchResults(student_id=(None, None)) |
---|
99 | else: |
---|
100 | results = Query().searchResults( |
---|
101 | Eq(('students_catalog', searchtype), query)) |
---|
102 | for result in results: |
---|
103 | hitlist.append(StudentQueryResultItem(result, view=view)) |
---|
104 | return hitlist |
---|
105 | |
---|
106 | class SimpleFieldSearch(object): |
---|
107 | """A programmatic (no UI required) search. |
---|
108 | |
---|
109 | Looks up a given field attribute of the students catalog for a |
---|
110 | single value. So normally you would call an instance of this |
---|
111 | search like this: |
---|
112 | |
---|
113 | >>> SimpleFieldSearch()(reg_number='somevalue') |
---|
114 | |
---|
115 | """ |
---|
116 | catalog_name = 'students_catalog' |
---|
117 | def __call__(self, **kw): |
---|
118 | """Search students catalog programmatically. |
---|
119 | """ |
---|
120 | if len(kw) != 1: |
---|
121 | raise ValueError('must give exactly one index name to search') |
---|
122 | cat = queryUtility(ICatalog, name=self.catalog_name) |
---|
123 | index_name, query_term = kw.items()[0] |
---|
124 | results = cat.searchResults(index_name=(query_term, query_term)) |
---|
125 | return results |
---|
126 | |
---|
127 | #: an instance of `SimpleFieldSearch` looking up students catalog. |
---|
128 | simple_search = SimpleFieldSearch() |
---|
129 | |
---|
130 | class CourseTicketIndexes(grok.Indexes): |
---|
131 | """A catalog for course tickets. |
---|
132 | """ |
---|
133 | grok.site(IUniversity) |
---|
134 | grok.name('coursetickets_catalog') |
---|
135 | grok.context(ICourseTicket) |
---|
136 | |
---|
137 | level = index.Field(attribute='level') |
---|
138 | session = index.Field(attribute='level_session') |
---|
139 | code = index.Field(attribute='code') |
---|
140 | |
---|
141 | class StudentsQuery(FilteredCatalogQueryBase): |
---|
142 | """Query students in a site. See waeup.kofa.catalog for more info. |
---|
143 | """ |
---|
144 | cat_name = 'students_catalog' |
---|
145 | defaults = dict(student_id=None) # make sure we get all studs by default |
---|
146 | |
---|
147 | class CourseTicketsQuery(FilteredCatalogQueryBase): |
---|
148 | """Query students in a site. See waeup.kofa.catalog for more info. |
---|
149 | """ |
---|
150 | cat_name = 'coursetickets_catalog' |
---|
151 | defaults = dict(code=None) # make sure we get all tickets by default |
---|