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
RevLine 
[5273]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##
[5824]22"""UI components for basic applicants and related components.
[5273]23"""
24import grok
25
[5822]26from zope.component import getUtility, getAllUtilitiesRegisteredFor
[5686]27from zope.formlib.widgets import FileWidget
[5273]28from waeup.sirp.browser import (
29    WAeUPPage, WAeUPEditFormPage, WAeUPAddFormPage,
30    WAeUPDisplayFormPage, NullValidator)
[5442]31from waeup.sirp.browser.pages import LoginPage
[5320]32from waeup.sirp.interfaces import IWAeUPObject
[5828]33from waeup.sirp.browser.viewlets import (
34    AddActionButton, ManageActionButton, PrimaryNavTab,
35    )
[5806]36from waeup.sirp.applicants import get_applicant_data, ResultEntry
[5758]37from waeup.sirp.applicants.interfaces import (
[5822]38    IApplicant, IApplicantPrincipal, IApplicantPDEEditData,
39    IApplicantsRoot, IApplicantsContainer, IApplicantContainerProvider,
40    )
[5686]41from waeup.sirp.widgets.passportwidget import (
42    PassportWidget, PassportDisplayWidget
43    )
[5273]44#from zope.formlib.objectwidget import ObjectWidget
45from zope.formlib.sequencewidget import ListSequenceWidget, SequenceDisplayWidget
46from zope.formlib.widget import CustomWidgetFactory
[5303]47from waeup.sirp.widgets.objectwidget import (
[5301]48    WAeUPObjectWidget, WAeUPObjectDisplayWidget)
[5303]49from waeup.sirp.widgets.multilistwidget import (
[5273]50    MultiListWidget, MultiListDisplayWidget)
[5686]51from waeup.sirp.image.browser.widget import (
52    ThumbnailWidget, EncodingImageFileWidget,
53    )
[5320]54
[5273]55results_widget = CustomWidgetFactory(
[5301]56    WAeUPObjectWidget, ResultEntry)
[5273]57
58results_display_widget = CustomWidgetFactory(
[5301]59    WAeUPObjectDisplayWidget, ResultEntry)
[5273]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
[5822]70class ApplicationsPage(WAeUPPage):
71    grok.context(IApplicantsRoot)
72    grok.name('index')
73    title = 'Applicants'
[5828]74    pnav = 2
[5822]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))
[5273]82
[5828]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
[5822]117class AddApplicantsContainerActionButton(AddActionButton):
118    grok.context(IApplicantsRoot)
[5828]119    grok.view(ApplicantsRootEditPage)
[5822]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'
[5828]127    pnav = 2
[5822]128
[5828]129    def update(self, providername=None, name=None, title=None,
130               description=None, ADD=None, CANCEL=None):
[5822]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()
[5828]146        if title:
147            container.title = title
148        if description:
149            container.description = description
[5822]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):
[5825]156        """Get a list of applicants container providers.
[5822]157
[5825]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        """
[5822]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
[5828]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
[5758]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))
[5273]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
[5686]230    #form_fields['passport'].custom_widget = PassportDisplayWidget
231    form_fields['passport'].custom_widget = ThumbnailWidget
[5273]232    label = 'Applicant'
233    title = 'Applicant'
[5828]234    pnav = 2
[5273]235
236class EditApplicant(WAeUPEditFormPage):
237    grok.context(IApplicant)
238    grok.name('edit')
[5484]239    form_fields = grok.AutoFields(IApplicantPDEEditData)
[5686]240    #form_fields['passport'].custom_widget = FileWidget
241    #form_fields['passport'].custom_widget = PassportWidget
242    form_fields['passport'].custom_widget = EncodingImageFileWidget
[5488]243    grok.template('form_edit_pde')
[5484]244
[5686]245    def update(self):
246        super(EditApplicant, self).update()
247        print self.request.form
248        return
249   
[5484]250    @property
251    def label(self):
252        # XXX: Use current/upcoming session
253        return 'Apply for Post UDE Screening Test (2009/2010)'
[5273]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
[5484]263    @grok.action('Final Submit')
264    def finalsubmit(self, **data):
[5273]265        self.applyData(self.context, **data)
[5484]266        self.context._p_changed = True
267        # XXX: Lock the form for editing...
[5273]268        return
Note: See TracBrowser for help on using the repository browser.