source: main/waeup.sirp/trunk/src/waeup/sirp/jambtables/browser.py @ 5465

Last change on this file since 5465 was 5453, checked in by uli, 14 years ago

Update login page to play well with the new authentication.

File size: 7.1 KB
Line 
1##
2## browser.py
3## Login : <uli@pu.smp.net>
4## Started on  Sun Jun 27 11:03:10 2010 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2010 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""UI components for JAMB tables.
23"""
24import grok
25
26from waeup.sirp.browser import (
27    WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage,
28    WAeUPDisplayFormPage, NullValidator)
29from waeup.sirp.browser.pages import LoginPage
30from waeup.sirp.interfaces import IWAeUPObject
31from waeup.sirp.jambtables import JAMBDataTable
32from waeup.sirp.jambtables.util import get_applicant_data
33from waeup.sirp.jambtables.interfaces import (
34    IApplicant, IApplicantContainer, IApplicantPrincipal)
35                                             
36
37#from zope.formlib.objectwidget import ObjectWidget
38from zope.formlib.sequencewidget import ListSequenceWidget, SequenceDisplayWidget
39from zope.formlib.widget import CustomWidgetFactory
40from waeup.sirp.jambtables.applicants import ResultEntry
41from waeup.sirp.widgets.objectwidget import (
42    WAeUPObjectWidget, WAeUPObjectDisplayWidget)
43from waeup.sirp.widgets.multilistwidget import (
44    MultiListWidget, MultiListDisplayWidget)
45
46results_widget = CustomWidgetFactory(
47    WAeUPObjectWidget, ResultEntry)
48
49results_display_widget = CustomWidgetFactory(
50    WAeUPObjectDisplayWidget, ResultEntry)
51
52#list_results_widget = CustomWidgetFactory(
53#    ListSequenceWidget, subwidget=results_widget)
54
55list_results_widget = CustomWidgetFactory(
56    MultiListWidget, subwidget=results_widget)
57
58list_results_display_widget = CustomWidgetFactory(
59    MultiListDisplayWidget, subwidget=results_display_widget)
60
61class ApplicationsPage(WAeUPPage):
62    grok.context(IApplicantContainer)
63    grok.name('index')
64    title = 'Applications'
65    pnav = 1
66   
67    def getApplications(self):
68        """Get a list of all stored applications.
69        """
70        for key, val in self.context.items():
71            url = self.url(val)
72            yield(dict(url=url, name=key))
73
74class AddApplicant(WAeUPAddFormPage):
75    grok.context(IApplicantContainer)
76    grok.name('add')
77    form_fields = grok.AutoFields(IApplicant)
78    form_fields['fst_sit_results'].custom_widget = list_results_widget
79    label = 'Add Applicant'
80    title = 'Add Applicant'
81    pnav = 1
82
83    @grok.action('Add applicant')
84    def addApplicant(self, **data):
85        from waeup.sirp.jambtables.applicants import Applicant
86        applicant = Applicant()
87        self.applyData(applicant, **data)
88        # XXX: temporarily disabled.
89        #self.context[applicant.reg_no] = applicant
90        try:
91            self.context[applicant.access_code] = applicant
92        except KeyError:
93            self.flash('The given access code is already in use!')
94            return
95        self.redirect(self.url(self.context))
96
97class DisplayApplicant(WAeUPDisplayFormPage):
98    grok.context(IApplicant)
99    grok.name('index')
100    form_fields = grok.AutoFields(IApplicant)
101    form_fields['fst_sit_results'].custom_widget = list_results_display_widget
102    label = 'Applicant'
103    title = 'Applicant'
104    pnav = 1
105
106class EditApplicant(WAeUPEditFormPage):
107    grok.context(IApplicant)
108    grok.name('edit')
109    form_fields = grok.AutoFields(IApplicant)
110    form_fields['fst_sit_results'].custom_widget = list_results_widget
111    label = 'Edit Application'
112    title = 'Edit Application'
113    pnav = 1
114
115    @grok.action('Save')
116    def save(self, **data):
117        self.applyData(self.context, **data)
118        self.context._p_changed = True
119        return
120
121    @grok.action('Save and return')
122    def saveAndReturn(self, **data):
123        self.applyData(self.context, **data)
124        self.redirect(self.url(self.context))
125        return
126
127    @grok.action('Cancel', validator=NullValidator)
128    def cancel(self, **data):
129        self.redirect(self.url(self.context))
130        return
131
132class Login_PDE(LoginPage):
133    grok.context(IWAeUPObject)
134    grok.name('login_pde')
135
136    title = 'PDE Login'
137    pnav = 1
138
139    def update(self):
140        """Validate credentials and redirect or show error.
141
142        The real validation is done by an pluggable authentication
143        utility (PAU). Here we only check, whether correct credentials
144        were entered by looking up the principal status of the
145        request: If the user authenticated successfully, we get an
146        applicant principal. Otherwise we get the unauthenticated
147        principal.
148        """
149        formfields = ['form.ac_number', 'form.jamb_reg_no',
150                      'form.ac_series', 'form.prefix',]
151
152        self.reg_no = self.request.form.get('form.jamb_reg_no', '')
153        self.ac_series = self.request.form.get('form.ac_series', '')
154        self.ac_number = self.request.form.get('form.ac_number', '')
155
156        for required_field in formfields:
157            if required_field not in self.request.form.keys():
158                return
159        self.reg_no = self.request.form['form.jamb_reg_no']
160        self.ac_series = self.request.form['form.ac_series']
161        self.ac_number = self.request.form['form.ac_number']
162        principal = self.request.principal
163        if not IApplicantPrincipal.providedBy(principal):
164            self.flash('You entered invalid credentials')
165            return
166        if hasattr(principal, 'reg_no'):
167            if not principal.reg_no is None:
168               
169                site = grok.getSite()
170                applications = site['applications']
171                application = applications[principal.reg_no]
172                self.redirect(self.url(application, '@@edit'))
173        return
174
175    def getCurrentSession(self):
176        """Get the current session.
177
178        XXX: This should be computed or retrieved from elsewhere.
179
180        `session` here means an academic session, not a browser
181        session.
182        """
183        return u'2010/2011'
184
185    def getDeadline(self):
186        """Get application submission deadline.
187
188        XXX: This should be computed or retrieved from elsewhere.
189        """
190        return u"""Application submission deadline is at Midnight on Friday,
191                   01. October 2010. No application will be treated
192                   after the deadline."""
193
194    def getApplicantData(self, reg_no, ac):
195        """Validate credentials and return applicant data.
196
197        Returns tuple ``(<APPLICANT_ENTRY>, <ACCESSCODE>) on
198        successful validation and ``None`` else.
199
200        We expect a JAMB registration number and an access code in
201        format ``PUDE-XXX-XXXXXXXXXX``.
202
203        See
204        :func:`waeup.sirp.jambtables.util.get_applicant_data`
205        for details.
206        """
207        return get_applicant_data(reg_no, ac)
Note: See TracBrowser for help on using the repository browser.