[6621] | 1 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 2 | ## This program is free software; you can redistribute it and/or modify |
---|
| 3 | ## it under the terms of the GNU General Public License as published by |
---|
| 4 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | ## (at your option) any later version. |
---|
| 6 | ## |
---|
| 7 | ## This program is distributed in the hope that it will be useful, |
---|
| 8 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | ## GNU General Public License for more details. |
---|
| 11 | ## |
---|
| 12 | ## You should have received a copy of the GNU General Public License |
---|
| 13 | ## along with this program; if not, write to the Free Software |
---|
| 14 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 15 | ## |
---|
| 16 | """UI components for students and related components. |
---|
| 17 | """ |
---|
| 18 | import sys |
---|
| 19 | import grok |
---|
| 20 | |
---|
| 21 | from datetime import datetime |
---|
| 22 | from zope.formlib.widget import CustomWidgetFactory |
---|
| 23 | from zope.formlib.form import setUpEditWidgets |
---|
| 24 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
| 25 | from zope.traversing.browser import absoluteURL |
---|
[6622] | 26 | from zope.component import ( |
---|
| 27 | createObject,) |
---|
[6621] | 28 | |
---|
| 29 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
| 30 | from reportlab.pdfgen import canvas |
---|
| 31 | from reportlab.lib.units import cm |
---|
| 32 | from reportlab.lib.pagesizes import A4 |
---|
| 33 | from reportlab.lib.styles import getSampleStyleSheet |
---|
| 34 | from reportlab.platypus import (Frame, Paragraph, Image, |
---|
| 35 | Table, Spacer) |
---|
| 36 | from reportlab.platypus.tables import TableStyle |
---|
| 37 | |
---|
| 38 | from waeup.sirp.accesscodes import invalidate_accesscode, get_access_code |
---|
| 39 | from waeup.sirp.accesscodes.workflow import USED |
---|
| 40 | from waeup.sirp.browser import ( |
---|
| 41 | WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, WAeUPDisplayFormPage) |
---|
| 42 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
| 43 | from waeup.sirp.browser.layout import NullValidator |
---|
| 44 | from waeup.sirp.browser.pages import add_local_role, del_local_roles |
---|
| 45 | from waeup.sirp.browser.resources import datepicker, tabs, datatable |
---|
| 46 | from waeup.sirp.browser.viewlets import ( |
---|
[6626] | 47 | ManageActionButton, PrimaryNavTab, LeftSidebarLink, AddActionButton, |
---|
[6621] | 48 | ) |
---|
| 49 | from waeup.sirp.image.browser.widget import ( |
---|
| 50 | ThumbnailWidget, EncodingImageFileWidget, |
---|
| 51 | ) |
---|
| 52 | from waeup.sirp.image.image import createWAeUPImageFile |
---|
| 53 | from waeup.sirp.interfaces import IWAeUPObject, ILocalRolesAssignable |
---|
| 54 | from waeup.sirp.permissions import get_users_with_local_roles |
---|
| 55 | from waeup.sirp.university.interfaces import ICertificate |
---|
| 56 | from waeup.sirp.widgets.datewidget import ( |
---|
| 57 | FriendlyDateWidget, FriendlyDateDisplayWidget) |
---|
| 58 | from waeup.sirp.widgets.restwidget import ReSTDisplayWidget |
---|
| 59 | from waeup.sirp.widgets.objectwidget import ( |
---|
| 60 | WAeUPObjectWidget, WAeUPObjectDisplayWidget) |
---|
| 61 | from waeup.sirp.widgets.multilistwidget import ( |
---|
| 62 | MultiListWidget, MultiListDisplayWidget) |
---|
| 63 | from waeup.sirp.students.interfaces import ( |
---|
[6622] | 64 | IStudentsContainer, IStudent, |
---|
[6621] | 65 | ) |
---|
[6622] | 66 | from waeup.sirp.students.students import ( |
---|
| 67 | Student, |
---|
| 68 | ) |
---|
[6626] | 69 | from waeup.sirp.students.catalog import search |
---|
[6621] | 70 | |
---|
| 71 | class StudentsTab(PrimaryNavTab): |
---|
| 72 | """Students tab in primary navigation. |
---|
| 73 | """ |
---|
| 74 | |
---|
| 75 | grok.context(IWAeUPObject) |
---|
| 76 | grok.order(3) |
---|
| 77 | grok.require('waeup.viewStudents') |
---|
| 78 | grok.template('primarynavtab') |
---|
| 79 | |
---|
| 80 | pnav = 4 |
---|
| 81 | tab_title = u'Students' |
---|
| 82 | |
---|
| 83 | @property |
---|
| 84 | def link_target(self): |
---|
| 85 | return self.view.application_url('students') |
---|
| 86 | |
---|
[6626] | 87 | class StudentsContainerPage(WAeUPPage): |
---|
| 88 | """The standard view for student containers. |
---|
[6621] | 89 | """ |
---|
| 90 | grok.context(IStudentsContainer) |
---|
| 91 | grok.name('index') |
---|
| 92 | grok.require('waeup.viewStudents') |
---|
| 93 | grok.template('studentscontainerpage') |
---|
| 94 | |
---|
| 95 | @property |
---|
| 96 | def title(self): |
---|
| 97 | return "Students" |
---|
| 98 | |
---|
| 99 | @property |
---|
| 100 | def label(self): |
---|
[6622] | 101 | return self.title |
---|
| 102 | |
---|
[6626] | 103 | def update(self, *args, **kw): |
---|
| 104 | datatable.need() |
---|
| 105 | form = self.request.form |
---|
| 106 | self.hitlist = [] |
---|
| 107 | if 'searchterm' in form and form['searchterm']: |
---|
| 108 | self.searchterm = form['searchterm'] |
---|
| 109 | self.searchtype = form['searchtype'] |
---|
| 110 | elif 'old_searchterm' in form: |
---|
| 111 | self.searchterm = form['old_searchterm'] |
---|
| 112 | self.searchtype = form['old_searchtype'] |
---|
| 113 | else: |
---|
| 114 | if 'search' in form: |
---|
| 115 | self.flash('Empty search string.') |
---|
| 116 | return |
---|
| 117 | self.hitlist = search(query=self.searchterm, |
---|
| 118 | searchtype=self.searchtype, view=self) |
---|
| 119 | if not self.hitlist: |
---|
| 120 | self.flash('No student found.') |
---|
| 121 | return |
---|
| 122 | |
---|
| 123 | class StudentsContainerManageActionButton(ManageActionButton): |
---|
[6622] | 124 | grok.order(1) |
---|
| 125 | grok.context(IStudentsContainer) |
---|
| 126 | grok.view(StudentsContainerPage) |
---|
| 127 | grok.require('waeup.manageStudents') |
---|
| 128 | text = 'Manage students' |
---|
| 129 | |
---|
[6626] | 130 | |
---|
| 131 | class StudentsContainerManagePage(WAeUPPage): |
---|
| 132 | """The manage page for student containers. |
---|
[6622] | 133 | """ |
---|
| 134 | grok.context(IStudentsContainer) |
---|
| 135 | grok.name('manage') |
---|
| 136 | grok.require('waeup.manageStudents') |
---|
[6626] | 137 | grok.template('studentscontainermanagepage') |
---|
[6622] | 138 | |
---|
| 139 | @property |
---|
| 140 | def title(self): |
---|
| 141 | return "Students" |
---|
| 142 | |
---|
| 143 | @property |
---|
| 144 | def label(self): |
---|
| 145 | return self.title |
---|
| 146 | |
---|
[6626] | 147 | def update(self, *args, **kw): |
---|
| 148 | datatable.need() |
---|
| 149 | form = self.request.form |
---|
| 150 | self.hitlist = [] |
---|
| 151 | if 'searchterm' in form and form['searchterm']: |
---|
| 152 | self.searchterm = form['searchterm'] |
---|
| 153 | self.searchtype = form['searchtype'] |
---|
| 154 | elif 'old_searchterm' in form: |
---|
| 155 | self.searchterm = form['old_searchterm'] |
---|
| 156 | self.searchtype = form['old_searchtype'] |
---|
| 157 | else: |
---|
| 158 | if 'search' in form: |
---|
| 159 | self.flash('Empty search string.') |
---|
| 160 | return |
---|
| 161 | #import pdb; pdb.set_trace() |
---|
| 162 | if not 'entries' in form: |
---|
| 163 | self.hitlist = search(query=self.searchterm, |
---|
| 164 | searchtype=self.searchtype, view=self) |
---|
| 165 | if not self.hitlist: |
---|
| 166 | self.flash('No student found.') |
---|
| 167 | return |
---|
| 168 | entries = form['entries'] |
---|
| 169 | if isinstance(entries, basestring): |
---|
| 170 | entries = [entries] |
---|
| 171 | deleted = [] |
---|
| 172 | for entry in entries: |
---|
| 173 | if 'remove' in form: |
---|
| 174 | del self.context[entry] |
---|
| 175 | deleted.append(entry) |
---|
| 176 | self.hitlist = search(query=self.searchterm, |
---|
| 177 | searchtype=self.searchtype, view=self) |
---|
| 178 | if len(deleted): |
---|
| 179 | self.flash('Successfully removed: %s' % ', '.join(deleted)) |
---|
[6622] | 180 | return |
---|
| 181 | |
---|
[6626] | 182 | class StudentsContainerAddActionButton(AddActionButton): |
---|
| 183 | grok.order(1) |
---|
| 184 | grok.context(IStudentsContainer) |
---|
| 185 | grok.view(StudentsContainerManagePage) |
---|
| 186 | grok.require('waeup.manageStudents') |
---|
| 187 | text = 'Add student' |
---|
| 188 | target = 'addstudent' |
---|
| 189 | |
---|
[6622] | 190 | class StudentAddFormPage(WAeUPAddFormPage): |
---|
| 191 | """Add-form to add a student. |
---|
| 192 | """ |
---|
| 193 | grok.context(IStudentsContainer) |
---|
| 194 | grok.require('waeup.manageStudents') |
---|
| 195 | grok.name('addstudent') |
---|
| 196 | grok.template('studentaddpage') |
---|
| 197 | form_fields = grok.AutoFields(IStudent) |
---|
| 198 | title = 'Students' |
---|
| 199 | label = 'Add student' |
---|
| 200 | |
---|
| 201 | @grok.action('Create student record') |
---|
| 202 | def addStudent(self, **data): |
---|
| 203 | student_id = self.request.form.get('form.student_id') |
---|
| 204 | student = createObject(u'waeup.Student') |
---|
| 205 | self.applyData(student, **data) |
---|
| 206 | #import pdb; pdb.set_trace() |
---|
| 207 | try: |
---|
| 208 | self.context[student_id] = student |
---|
| 209 | except KeyError: |
---|
| 210 | self.flash('The student id chosen already exists.') |
---|
| 211 | return |
---|
[6626] | 212 | self.flash('Student record created.') |
---|
[6622] | 213 | self.redirect(self.url(self.context[student_id], 'index')) |
---|
| 214 | return |
---|
| 215 | |
---|
| 216 | class DisplayStudent(WAeUPDisplayFormPage): |
---|
| 217 | grok.context(IStudent) |
---|
| 218 | grok.name('index') |
---|
| 219 | grok.require('waeup.viewStudents') |
---|
| 220 | form_fields = grok.AutoFields(IStudent) |
---|
| 221 | |
---|
| 222 | @property |
---|
| 223 | def title(self): |
---|
| 224 | return 'Student: %s' % self.context.name |
---|
| 225 | |
---|
| 226 | @property |
---|
| 227 | def label(self): |
---|
| 228 | return self.context.name |
---|