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

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

Add more views (pages) and a primary nav tab, which we can remove
later.

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