source: main/waeup.sirp/trunk/src/waeup/sirp/students/browser.py @ 6627

Last change on this file since 6627 was 6626, checked in by Henrik Bettermann, 13 years ago

Add students_catalog and search facilities.

  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
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"""
18import sys
19import grok
20
21from datetime import datetime
22from zope.formlib.widget import CustomWidgetFactory
23from zope.formlib.form import setUpEditWidgets
24from zope.securitypolicy.interfaces import IPrincipalRoleManager
25from zope.traversing.browser import absoluteURL
26from zope.component import (
27    createObject,)
28
29from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
30from reportlab.pdfgen import canvas
31from reportlab.lib.units import cm
32from reportlab.lib.pagesizes import A4
33from reportlab.lib.styles import getSampleStyleSheet
34from reportlab.platypus import (Frame, Paragraph, Image,
35    Table, Spacer)
36from reportlab.platypus.tables import TableStyle
37
38from waeup.sirp.accesscodes import invalidate_accesscode, get_access_code
39from waeup.sirp.accesscodes.workflow import USED
40from waeup.sirp.browser import (
41    WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage, WAeUPDisplayFormPage)
42from waeup.sirp.browser.breadcrumbs import Breadcrumb
43from waeup.sirp.browser.layout import NullValidator
44from waeup.sirp.browser.pages import add_local_role, del_local_roles
45from waeup.sirp.browser.resources import datepicker, tabs, datatable
46from waeup.sirp.browser.viewlets import (
47    ManageActionButton, PrimaryNavTab, LeftSidebarLink, AddActionButton,
48    )
49from waeup.sirp.image.browser.widget import (
50    ThumbnailWidget, EncodingImageFileWidget,
51    )
52from waeup.sirp.image.image import createWAeUPImageFile
53from waeup.sirp.interfaces import IWAeUPObject, ILocalRolesAssignable
54from waeup.sirp.permissions import get_users_with_local_roles
55from waeup.sirp.university.interfaces import ICertificate
56from waeup.sirp.widgets.datewidget import (
57    FriendlyDateWidget, FriendlyDateDisplayWidget)
58from waeup.sirp.widgets.restwidget import ReSTDisplayWidget
59from waeup.sirp.widgets.objectwidget import (
60    WAeUPObjectWidget, WAeUPObjectDisplayWidget)
61from waeup.sirp.widgets.multilistwidget import (
62    MultiListWidget, MultiListDisplayWidget)
63from waeup.sirp.students.interfaces import (
64    IStudentsContainer, IStudent,
65    )
66from waeup.sirp.students.students import (
67    Student,
68    )
69from waeup.sirp.students.catalog import search
70
71class 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
87class StudentsContainerPage(WAeUPPage):
88    """The standard view for student containers.
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):
101        return self.title
102
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
123class StudentsContainerManageActionButton(ManageActionButton):
124    grok.order(1)
125    grok.context(IStudentsContainer)
126    grok.view(StudentsContainerPage)
127    grok.require('waeup.manageStudents')
128    text = 'Manage students'
129
130
131class StudentsContainerManagePage(WAeUPPage):
132    """The manage page for student containers.
133    """
134    grok.context(IStudentsContainer)
135    grok.name('manage')
136    grok.require('waeup.manageStudents')
137    grok.template('studentscontainermanagepage')
138
139    @property
140    def title(self):
141        return "Students"
142
143    @property
144    def label(self):
145        return self.title
146
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))
180        return
181
182class 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
190class 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
212        self.flash('Student record created.')
213        self.redirect(self.url(self.context[student_id], 'index'))
214        return
215
216class 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
Note: See TracBrowser for help on using the repository browser.