1 | ## $Id: catalog.py 15163 2018-09-23 05:05: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.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 | from waeup.kofa.students.workflow import (TRANSREQ, TRANSVAL) |
---|
32 | |
---|
33 | class StudentsCatalog(grok.Indexes): |
---|
34 | """A catalog for students. |
---|
35 | """ |
---|
36 | grok.site(IUniversity) |
---|
37 | grok.name('students_catalog') |
---|
38 | grok.context(IStudent) |
---|
39 | |
---|
40 | student_id = index.Field(attribute='student_id') |
---|
41 | fullname = index.Text(attribute='fullname') |
---|
42 | email = index.Field(attribute='email') |
---|
43 | reg_number = index.Field(attribute='reg_number') |
---|
44 | matric_number = index.Field(attribute='matric_number') |
---|
45 | state = index.Field(attribute='state') |
---|
46 | certcode = index.Field(attribute='certcode') |
---|
47 | depcode = index.Field(attribute='depcode') |
---|
48 | faccode = index.Field(attribute='faccode') |
---|
49 | current_session = index.Field(attribute='current_session') |
---|
50 | current_level = index.Field(attribute='current_level') |
---|
51 | current_mode = index.Field(attribute='current_mode') |
---|
52 | |
---|
53 | class StudentQueryResultItem(object): |
---|
54 | grok.implements(IQueryResultItem) |
---|
55 | |
---|
56 | title = u'Student Query Item' |
---|
57 | description = u'Some student found in a search' |
---|
58 | |
---|
59 | def __init__(self, context, view): |
---|
60 | self.context = context |
---|
61 | self.url = view.url(context) |
---|
62 | self.student_id = context.student_id |
---|
63 | self.display_fullname = context.display_fullname |
---|
64 | self.reg_number = context.reg_number |
---|
65 | self.matric_number = context.matric_number |
---|
66 | self.state = context.state |
---|
67 | self.translated_state = context.translated_state |
---|
68 | self.current_level = context['studycourse'].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 | self.comment = getattr(context, 'officer_comment', None) |
---|
77 | |
---|
78 | def search(query=None, searchtype=None, view=None): |
---|
79 | hitlist = [] |
---|
80 | if searchtype in ('fullname',): |
---|
81 | results = Query().searchResults( |
---|
82 | Text(('students_catalog', searchtype), query)) |
---|
83 | elif searchtype == 'suspended': |
---|
84 | # 'suspended' is not indexed |
---|
85 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
86 | all = cat.searchResults(student_id=(None, None)) |
---|
87 | for student in all: |
---|
88 | if student.suspended: |
---|
89 | hitlist.append(StudentQueryResultItem(student, view=view)) |
---|
90 | return hitlist |
---|
91 | elif searchtype in (TRANSREQ, TRANSVAL): |
---|
92 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
93 | results = cat.searchResults(state=(searchtype, searchtype)) |
---|
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 |
---|