source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/browser.py @ 5833

Last change on this file since 5833 was 5832, checked in by uli, 14 years ago
  • Add manage button on applicants container default page.
  • Add manage page for applicants containers.
File size: 9.5 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 basic applicants and related components.
23"""
24import grok
25
26from zope.component import getUtility, getAllUtilitiesRegisteredFor
27from zope.formlib.widgets import FileWidget
28from waeup.sirp.browser import (
29    WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage,
30    WAeUPDisplayFormPage, NullValidator)
31from waeup.sirp.browser.pages import LoginPage
32from waeup.sirp.interfaces import IWAeUPObject
33from waeup.sirp.browser.viewlets import (
34    AddActionButton, ManageActionButton, PrimaryNavTab,
35    )
36from waeup.sirp.applicants import get_applicant_data, ResultEntry
37from waeup.sirp.applicants.interfaces import (
38    IApplicant, IApplicantPrincipal, IApplicantPDEEditData,
39    IApplicantsRoot, IApplicantsContainer, IApplicantContainerProvider,
40    )
41from waeup.sirp.widgets.passportwidget import (
42    PassportWidget, PassportDisplayWidget
43    )
44#from zope.formlib.objectwidget import ObjectWidget
45from zope.formlib.sequencewidget import ListSequenceWidget, SequenceDisplayWidget
46from zope.formlib.widget import CustomWidgetFactory
47from waeup.sirp.widgets.objectwidget import (
48    WAeUPObjectWidget, WAeUPObjectDisplayWidget)
49from waeup.sirp.widgets.multilistwidget import (
50    MultiListWidget, MultiListDisplayWidget)
51from waeup.sirp.image.browser.widget import (
52    ThumbnailWidget, EncodingImageFileWidget,
53    )
54
55results_widget = CustomWidgetFactory(
56    WAeUPObjectWidget, ResultEntry)
57
58results_display_widget = CustomWidgetFactory(
59    WAeUPObjectDisplayWidget, ResultEntry)
60
61#list_results_widget = CustomWidgetFactory(
62#    ListSequenceWidget, subwidget=results_widget)
63
64list_results_widget = CustomWidgetFactory(
65    MultiListWidget, subwidget=results_widget)
66
67list_results_display_widget = CustomWidgetFactory(
68    MultiListDisplayWidget, subwidget=results_display_widget)
69
70class ApplicationsPage(WAeUPPage):
71    grok.context(IApplicantsRoot)
72    grok.name('index')
73    title = 'Applicants'
74    pnav = 2
75   
76    def getApplications(self):
77        """Get a list of all stored applicant containers.
78        """
79        for key, val in self.context.items():
80            url = self.url(val)
81            yield(dict(url=url, name=key))
82
83class ManageApplicantsRootActionButton(ManageActionButton):
84    grok.context(IApplicantsRoot)
85    grok.view(ApplicationsPage)
86    text = 'Manage applicant containers'
87
88class ApplicantsRootEditPage(WAeUPPage):
89    grok.context(IApplicantsRoot)
90    grok.name('manage')
91    grok.template('applicantsrooteditpage')
92    title = 'Edit applicants containers'
93    pnav = 2
94
95    def update(self, entries=None, DELETE=None, CANCEL=None):
96        if CANCEL is not None:
97            self.redirect(self.url(self.context))
98            return
99        if DELETE is None:
100            return
101        if entries is None:
102            return
103        if not isinstance(entries, list):
104            entries = [entries]
105        for name in entries:
106            del self.context[name]
107            self.flash('Deleted "%s"' % name)
108        return
109
110    def getApplications(self):
111        """Get a list of all stored applicant containers.
112        """
113        for key, val in self.context.items():
114            url = self.url(val)
115            yield(dict(url=url, name=key))
116
117class AddApplicantsContainerActionButton(AddActionButton):
118    grok.context(IApplicantsRoot)
119    grok.view(ApplicantsRootEditPage)
120    text = 'Add applicants container'
121
122class AddApplicantsContainer(WAeUPPage):
123    grok.context(IApplicantsRoot)
124    grok.name('add')
125    grok.template('addcontainer')
126    title = 'Add applicants container'
127    pnav = 2
128
129    def update(self, providername=None, name=None, title=None,
130               description=None, ADD=None, CANCEL=None):
131        if CANCEL is not None:
132            self.redirect(self.url(self.context))
133            return
134        if ADD is None:
135            return
136        if not name:
137            self.flash('Error: you must give a name')
138            return
139        if name in self.context.keys():
140            self.flash('A container of the given name already exists')
141            return
142        # Add new applicants container...
143        provider = getUtility(IApplicantContainerProvider,
144                              name=providername)
145        container = provider.factory()
146        if title:
147            container.title = title
148        if description:
149            container.description = description
150        self.context[name] = container
151        self.flash('Added "%s".' % name)
152        self.redirect(self.url(self.context))
153        return
154       
155    def getContainerProviders(self):
156        """Get a list of applicants container providers.
157
158        Applicants container providers are named utilities that help
159        to create applicants containers of different types
160        (JAMB-based, non-JAMB-based, etc.).
161
162        The list returned contains dicts::
163
164          {'name': <utility_name>,
165           'provider': <provider instance>}
166
167        where `utility_name` is the name under which the respective
168        provider utility is registered and `provider` is the real
169        provider instance.
170
171        The `utility_name` can be used to lookup the utility anew (for
172        instance after submitting a form) and the `provider` instance
173        can be used to create new instances of the respective
174        applicants container type.
175        """
176        providers = getAllUtilitiesRegisteredFor(IApplicantContainerProvider)
177        result = [
178            {'name': getattr(x, 'grokcore.component.directive.name'),
179             'provider': x}
180             for x in providers
181            ]
182        return result
183
184
185class ApplicantsTab(PrimaryNavTab):
186    """Faculties-tab in primary navigation.
187    """
188    grok.context(IWAeUPObject)
189    grok.order(3)
190    grok.require('waeup.View')
191    grok.template('primarynavtab')
192
193    pnav = 2
194    tab_title = u'Applicants'
195
196    @property
197    def link_target(self):
198        return self.view.application_url('applicants')
199
200class ApplicantsContainerPage(WAeUPPage):
201    """The standard view for regular applicant containers.
202    """
203    grok.context(IApplicantsContainer)
204    grok.name('index')
205
206    title = 'Applicants Container'
207    label = 'Applicants Container'
208    pnav = 2
209
210class ManageApplicantsContainerActionButton(ManageActionButton):
211    grok.context(IApplicantsContainer)
212    grok.view(ApplicantsContainerPage)
213    text = 'Manage'
214
215class ApplicantsContainerEditPage(WAeUPPage):
216    grok.context(IApplicantsContainer)
217    grok.name('manage')
218
219    title = 'Manage Applicants Container'
220    label = 'Manage Applicants Container'
221    pnav = 2
222
223
224#class AddApplicant(WAeUPAddFormPage):
225#    grok.context(IApplicantContainer)
226#    grok.name('add')
227#    form_fields = grok.AutoFields(IApplicant)
228#    form_fields['fst_sit_results'].custom_widget = list_results_widget
229#    form_fields['passport'].custom_widget = EncodingImageFileWidget
230#    label = 'Add Applicant'
231#    title = 'Add Applicant'
232#    pnav = 1
233#
234#    @grok.action('Add applicant')
235#    def addApplicant(self, **data):
236#        from waeup.sirp.jambtables.applicants import Applicant
237#        applicant = Applicant()
238#        self.applyData(applicant, **data)
239#        # XXX: temporarily disabled.
240#        #self.context[applicant.reg_no] = applicant
241#        try:
242#            self.context[applicant.access_code] = applicant
243#        except KeyError:
244#            self.flash('The given access code is already in use!')
245#            return
246#        self.redirect(self.url(self.context))
247
248class DisplayApplicant(WAeUPDisplayFormPage):
249    grok.context(IApplicant)
250    grok.name('index')
251    form_fields = grok.AutoFields(IApplicant)
252    form_fields['fst_sit_results'].custom_widget = list_results_display_widget
253    #form_fields['passport'].custom_widget = PassportDisplayWidget
254    form_fields['passport'].custom_widget = ThumbnailWidget
255    label = 'Applicant'
256    title = 'Applicant'
257    pnav = 2
258
259class EditApplicant(WAeUPEditFormPage):
260    grok.context(IApplicant)
261    grok.name('edit')
262    form_fields = grok.AutoFields(IApplicantPDEEditData)
263    #form_fields['passport'].custom_widget = FileWidget
264    #form_fields['passport'].custom_widget = PassportWidget
265    form_fields['passport'].custom_widget = EncodingImageFileWidget
266    grok.template('form_edit_pde')
267
268    def update(self):
269        super(EditApplicant, self).update()
270        print self.request.form
271        return
272   
273    @property
274    def label(self):
275        # XXX: Use current/upcoming session
276        return 'Apply for Post UDE Screening Test (2009/2010)'
277    title = 'Edit Application'
278    pnav = 1
279
280    @grok.action('Save')
281    def save(self, **data):
282        self.applyData(self.context, **data)
283        self.context._p_changed = True
284        return
285
286    @grok.action('Final Submit')
287    def finalsubmit(self, **data):
288        self.applyData(self.context, **data)
289        self.context._p_changed = True
290        # XXX: Lock the form for editing...
291        return
Note: See TracBrowser for help on using the repository browser.