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

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

Add an index view (page) for applicants containers.

File size: 9.2 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   
210
211#class AddApplicant(WAeUPAddFormPage):
212#    grok.context(IApplicantContainer)
213#    grok.name('add')
214#    form_fields = grok.AutoFields(IApplicant)
215#    form_fields['fst_sit_results'].custom_widget = list_results_widget
216#    form_fields['passport'].custom_widget = EncodingImageFileWidget
217#    label = 'Add Applicant'
218#    title = 'Add Applicant'
219#    pnav = 1
220#
221#    @grok.action('Add applicant')
222#    def addApplicant(self, **data):
223#        from waeup.sirp.jambtables.applicants import Applicant
224#        applicant = Applicant()
225#        self.applyData(applicant, **data)
226#        # XXX: temporarily disabled.
227#        #self.context[applicant.reg_no] = applicant
228#        try:
229#            self.context[applicant.access_code] = applicant
230#        except KeyError:
231#            self.flash('The given access code is already in use!')
232#            return
233#        self.redirect(self.url(self.context))
234
235class DisplayApplicant(WAeUPDisplayFormPage):
236    grok.context(IApplicant)
237    grok.name('index')
238    form_fields = grok.AutoFields(IApplicant)
239    form_fields['fst_sit_results'].custom_widget = list_results_display_widget
240    #form_fields['passport'].custom_widget = PassportDisplayWidget
241    form_fields['passport'].custom_widget = ThumbnailWidget
242    label = 'Applicant'
243    title = 'Applicant'
244    pnav = 2
245
246class EditApplicant(WAeUPEditFormPage):
247    grok.context(IApplicant)
248    grok.name('edit')
249    form_fields = grok.AutoFields(IApplicantPDEEditData)
250    #form_fields['passport'].custom_widget = FileWidget
251    #form_fields['passport'].custom_widget = PassportWidget
252    form_fields['passport'].custom_widget = EncodingImageFileWidget
253    grok.template('form_edit_pde')
254
255    def update(self):
256        super(EditApplicant, self).update()
257        print self.request.form
258        return
259   
260    @property
261    def label(self):
262        # XXX: Use current/upcoming session
263        return 'Apply for Post UDE Screening Test (2009/2010)'
264    title = 'Edit Application'
265    pnav = 1
266
267    @grok.action('Save')
268    def save(self, **data):
269        self.applyData(self.context, **data)
270        self.context._p_changed = True
271        return
272
273    @grok.action('Final Submit')
274    def finalsubmit(self, **data):
275        self.applyData(self.context, **data)
276        self.context._p_changed = True
277        # XXX: Lock the form for editing...
278        return
Note: See TracBrowser for help on using the repository browser.