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

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

Add docstring to tell what's going on when getting a list of
applicants container providers.

File size: 7.3 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 AddActionButton
34from waeup.sirp.applicants import get_applicant_data, ResultEntry
35from waeup.sirp.applicants.interfaces import (
36    IApplicant, IApplicantPrincipal, IApplicantPDEEditData,
37    IApplicantsRoot, IApplicantsContainer, IApplicantContainerProvider,
38    )
39from waeup.sirp.widgets.passportwidget import (
40    PassportWidget, PassportDisplayWidget
41    )
42#from zope.formlib.objectwidget import ObjectWidget
43from zope.formlib.sequencewidget import ListSequenceWidget, SequenceDisplayWidget
44from zope.formlib.widget import CustomWidgetFactory
45from waeup.sirp.widgets.objectwidget import (
46    WAeUPObjectWidget, WAeUPObjectDisplayWidget)
47from waeup.sirp.widgets.multilistwidget import (
48    MultiListWidget, MultiListDisplayWidget)
49from waeup.sirp.image.browser.widget import (
50    ThumbnailWidget, EncodingImageFileWidget,
51    )
52
53results_widget = CustomWidgetFactory(
54    WAeUPObjectWidget, ResultEntry)
55
56results_display_widget = CustomWidgetFactory(
57    WAeUPObjectDisplayWidget, ResultEntry)
58
59#list_results_widget = CustomWidgetFactory(
60#    ListSequenceWidget, subwidget=results_widget)
61
62list_results_widget = CustomWidgetFactory(
63    MultiListWidget, subwidget=results_widget)
64
65list_results_display_widget = CustomWidgetFactory(
66    MultiListDisplayWidget, subwidget=results_display_widget)
67
68class ApplicationsPage(WAeUPPage):
69    grok.context(IApplicantsRoot)
70    grok.name('index')
71    title = 'Applicants'
72    pnav = 1
73   
74    def getApplications(self):
75        """Get a list of all stored applicant containers.
76        """
77        for key, val in self.context.items():
78            url = self.url(val)
79            yield(dict(url=url, name=key))
80
81class AddApplicantsContainerActionButton(AddActionButton):
82    grok.context(IApplicantsRoot)
83    grok.view(ApplicationsPage)
84    text = 'Add applicants container'
85
86class AddApplicantsContainer(WAeUPPage):
87    grok.context(IApplicantsRoot)
88    grok.name('add')
89    grok.template('addcontainer')
90    title = 'Add applicants container'
91    pnav = 1
92
93    def update(self, providername=None, name=None, ADD=None, CANCEL=None):
94        if CANCEL is not None:
95            self.redirect(self.url(self.context))
96            return
97        if ADD is None:
98            return
99        if not name:
100            self.flash('Error: you must give a name')
101            return
102        if name in self.context.keys():
103            self.flash('A container of the given name already exists')
104            return
105        # Add new applicants container...
106        provider = getUtility(IApplicantContainerProvider,
107                              name=providername)
108        container = provider.factory()
109        self.context[name] = container
110        self.flash('Added "%s".' % name)
111        self.redirect(self.url(self.context))
112        return
113       
114    def getContainerProviders(self):
115        """Get a list of applicants container providers.
116
117        Applicants container providers are named utilities that help
118        to create applicants containers of different types
119        (JAMB-based, non-JAMB-based, etc.).
120
121        The list returned contains dicts::
122
123          {'name': <utility_name>,
124           'provider': <provider instance>}
125
126        where `utility_name` is the name under which the respective
127        provider utility is registered and `provider` is the real
128        provider instance.
129
130        The `utility_name` can be used to lookup the utility anew (for
131        instance after submitting a form) and the `provider` instance
132        can be used to create new instances of the respective
133        applicants container type.
134        """
135        providers = getAllUtilitiesRegisteredFor(IApplicantContainerProvider)
136        result = [
137            {'name': getattr(x, 'grokcore.component.directive.name'),
138             'provider': x}
139             for x in providers
140            ]
141        return result
142       
143#class AddApplicant(WAeUPAddFormPage):
144#    grok.context(IApplicantContainer)
145#    grok.name('add')
146#    form_fields = grok.AutoFields(IApplicant)
147#    form_fields['fst_sit_results'].custom_widget = list_results_widget
148#    form_fields['passport'].custom_widget = EncodingImageFileWidget
149#    label = 'Add Applicant'
150#    title = 'Add Applicant'
151#    pnav = 1
152#
153#    @grok.action('Add applicant')
154#    def addApplicant(self, **data):
155#        from waeup.sirp.jambtables.applicants import Applicant
156#        applicant = Applicant()
157#        self.applyData(applicant, **data)
158#        # XXX: temporarily disabled.
159#        #self.context[applicant.reg_no] = applicant
160#        try:
161#            self.context[applicant.access_code] = applicant
162#        except KeyError:
163#            self.flash('The given access code is already in use!')
164#            return
165#        self.redirect(self.url(self.context))
166
167class DisplayApplicant(WAeUPDisplayFormPage):
168    grok.context(IApplicant)
169    grok.name('index')
170    form_fields = grok.AutoFields(IApplicant)
171    form_fields['fst_sit_results'].custom_widget = list_results_display_widget
172    #form_fields['passport'].custom_widget = PassportDisplayWidget
173    form_fields['passport'].custom_widget = ThumbnailWidget
174    label = 'Applicant'
175    title = 'Applicant'
176    pnav = 1
177
178class EditApplicant(WAeUPEditFormPage):
179    grok.context(IApplicant)
180    grok.name('edit')
181    form_fields = grok.AutoFields(IApplicantPDEEditData)
182    #form_fields['passport'].custom_widget = FileWidget
183    #form_fields['passport'].custom_widget = PassportWidget
184    form_fields['passport'].custom_widget = EncodingImageFileWidget
185    grok.template('form_edit_pde')
186
187    def update(self):
188        super(EditApplicant, self).update()
189        print self.request.form
190        return
191   
192    @property
193    def label(self):
194        # XXX: Use current/upcoming session
195        return 'Apply for Post UDE Screening Test (2009/2010)'
196    title = 'Edit Application'
197    pnav = 1
198
199    @grok.action('Save')
200    def save(self, **data):
201        self.applyData(self.context, **data)
202        self.context._p_changed = True
203        return
204
205    @grok.action('Final Submit')
206    def finalsubmit(self, **data):
207        self.applyData(self.context, **data)
208        self.context._p_changed = True
209        # XXX: Lock the form for editing...
210        return
Note: See TracBrowser for help on using the repository browser.