[7191] | 1 | ## $Id: catalog.py 15417 2019-05-21 09:16:47Z 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 | ## |
---|
[6626] | 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 |
---|
[6786] | 24 | from zope.catalog.interfaces import ICatalog |
---|
| 25 | from zope.component import queryUtility |
---|
[9786] | 26 | from waeup.kofa.catalog import FilteredCatalogQueryBase |
---|
[7811] | 27 | from waeup.kofa.interfaces import ( |
---|
[7215] | 28 | IUniversity, IQueryResultItem, academic_sessions_vocab) |
---|
[8700] | 29 | from waeup.kofa.students.interfaces import IStudent, ICourseTicket |
---|
[7811] | 30 | from waeup.kofa.university.vocabularies import course_levels |
---|
[15417] | 31 | from waeup.kofa.students.workflow import TRANSREQ, TRANSVAL, GRADUATED |
---|
[6626] | 32 | |
---|
[8404] | 33 | class StudentsCatalog(grok.Indexes): |
---|
[6626] | 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') |
---|
[6818] | 41 | fullname = index.Text(attribute='fullname') |
---|
[7369] | 42 | email = index.Field(attribute='email') |
---|
[6778] | 43 | reg_number = index.Field(attribute='reg_number') |
---|
| 44 | matric_number = index.Field(attribute='matric_number') |
---|
[6696] | 45 | state = index.Field(attribute='state') |
---|
[7203] | 46 | certcode = index.Field(attribute='certcode') |
---|
| 47 | depcode = index.Field(attribute='depcode') |
---|
| 48 | faccode = index.Field(attribute='faccode') |
---|
[7062] | 49 | current_session = index.Field(attribute='current_session') |
---|
[9736] | 50 | current_level = index.Field(attribute='current_level') |
---|
[7641] | 51 | current_mode = index.Field(attribute='current_mode') |
---|
[6626] | 52 | |
---|
| 53 | class StudentQueryResultItem(object): |
---|
| 54 | grok.implements(IQueryResultItem) |
---|
| 55 | |
---|
| 56 | title = u'Student Query Item' |
---|
[6786] | 57 | description = u'Some student found in a search' |
---|
[6626] | 58 | |
---|
| 59 | def __init__(self, context, view): |
---|
| 60 | self.context = context |
---|
| 61 | self.url = view.url(context) |
---|
| 62 | self.student_id = context.student_id |
---|
[7364] | 63 | self.display_fullname = context.display_fullname |
---|
[6696] | 64 | self.reg_number = context.reg_number |
---|
[6750] | 65 | self.matric_number = context.matric_number |
---|
[6696] | 66 | self.state = context.state |
---|
[7692] | 67 | self.translated_state = context.translated_state |
---|
[9220] | 68 | self.current_level = context['studycourse'].current_level |
---|
[7215] | 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 |
---|
[9486] | 76 | self.comment = getattr(context, 'officer_comment', None) |
---|
[6626] | 77 | |
---|
| 78 | def search(query=None, searchtype=None, view=None): |
---|
| 79 | hitlist = [] |
---|
[6818] | 80 | if searchtype in ('fullname',): |
---|
[6626] | 81 | results = Query().searchResults( |
---|
| 82 | Text(('students_catalog', searchtype), query)) |
---|
[9795] | 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 |
---|
[15417] | 91 | elif searchtype in (TRANSREQ, TRANSVAL, GRADUATED): |
---|
[10465] | 92 | cat = queryUtility(ICatalog, name='students_catalog') |
---|
[15163] | 93 | results = cat.searchResults(state=(searchtype, searchtype)) |
---|
[6626] | 94 | else: |
---|
[6630] | 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)) |
---|
[6626] | 102 | for result in results: |
---|
| 103 | hitlist.append(StudentQueryResultItem(result, view=view)) |
---|
| 104 | return hitlist |
---|
[6786] | 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() |
---|
[7633] | 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 | |
---|
[9925] | 137 | level = index.Field(attribute='level') |
---|
| 138 | session = index.Field(attribute='level_session') |
---|
[7633] | 139 | code = index.Field(attribute='code') |
---|
[9786] | 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 |
---|
[9843] | 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 |
---|