source: main/kofacustom.lpng/trunk/src/kofacustom/lpng/applicants/browser.py @ 17175

Last change on this file since 17175 was 17175, checked in by Henrik Bettermann, 2 years ago

Make puniteditpage an auto submit form.

  • Property svn:keywords set to Id
File size: 8.8 KB
RevLine 
[16717]1## $Id: browser.py 17175 2022-11-25 09:04:54Z henrik $
[15614]2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""UI components for basic applicants and related components.
19"""
20import grok
[16997]21from zope.formlib.textwidgets import BytesDisplayWidget
[17020]22from zope.component import getUtility
[17023]23from hurry.workflow.interfaces import IWorkflowState
[17020]24from waeup.kofa.widgets.datewidget import (
25    FriendlyDateDisplayWidget,
26    FriendlyDatetimeDisplayWidget)
[16997]27from waeup.kofa.applicants.pdf import PDFApplicationSlip
[17091]28from waeup.kofa.browser.layout import KofaEditFormPage, action
[15614]29from waeup.kofa.applicants.browser import (
[16997]30    ApplicantRegistrationPage, ApplicantsContainerPage,
31    ApplicantDisplayFormPage,
32    ApplicantManageFormPage,
[17019]33    ApplicantEditFormPage,
[17020]34    BalancePaymentAddFormPage,
35    ExportPDFPaymentSlipPage,
36    ApplicantBaseDisplayFormPage)
[17023]37from waeup.kofa.applicants.workflow import (
38    INITIALIZED, STARTED, PAID, SUBMITTED,
39    ADMITTED, NOT_ADMITTED, CREATED, PROCESSED)
[17020]40from waeup.kofa.students.interfaces import IStudentsUtils
[17023]41from waeup.kofa.applicants.interfaces import (
42    IApplicantOnlinePayment, IApplicantsUtils)
[17020]43from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
44from kofacustom.nigeria.applicants.browser import NigeriaExportPDFPaymentSlipPage
[16983]45from kofacustom.lpng.applicants.interfaces import (
[16997]46    ICustomApplicant,
[16718]47    ICustomApplicantOnlinePayment,
[17013]48    ICustomApplicantEdit,
[17091]49    IPollingUnit,
50    STATES, LGAS, WARDS, PUNITS
[16718]51    )
[16983]52from kofacustom.lpng.interfaces import MessageFactory as _
[15614]53
[16997]54       
55class CustomApplicantDisplayFormPage(ApplicantDisplayFormPage):
56    """A display view for applicant data.
57    """
58
59    @property
[17091]60    def render_punit(self):
61        if self.context.punit:
62            punit = self.context.punit.split('_')
63            return ' / '.join([STATES[punit[0]],
64                    LGAS['_'.join(punit[:2])],
65                    WARDS['_'.join(punit[:3])] ,
66                    PUNITS['_'.join(punit[:4])]
67                   ])
68        return
69
70    @property
[16997]71    def form_fields(self):
[17091]72        form_fields = grok.AutoFields(
73            ICustomApplicant).omit('locked', 'suspended', 'federalstate',
74                                   'lga', 'ward', 'punit')
[16997]75        form_fields['perm_address'].custom_widget = BytesDisplayWidget
[17091]76        #form_fields['notice'].custom_widget = BytesDisplayWidget
[16997]77        if not getattr(self.context, 'student_id'):
78            form_fields = form_fields.omit('student_id')
79        return form_fields
80
81class CustomPDFApplicationSlip(PDFApplicationSlip):
82
83    @property
84    def form_fields(self):
85        form_fields = grok.AutoFields(ICustomApplicant)
86        if not getattr(self.context, 'student_id'):
87            form_fields = form_fields.omit('student_id')
88        return form_fields
89
90class CustomApplicantManageFormPage(ApplicantManageFormPage):
91    """A full edit view for applicant data.
92    """
93
94    @property
[17023]95    def display_actions(self):
96        actions = [[_('Save'), _('Finally Submit')],
97                   [
98                    #_('Add online payment ticket'),
99                    _('Add balance payment ticket'),
100                    _('Remove selected tickets')
101                   ]]
102        applicants_utils = getUtility(IApplicantsUtils)
103        if self.context.state not in applicants_utils.BALANCE_PAYMENT_STATES:
104            actions[1].pop(1)
105        return actions
106
107    @property
[16997]108    def form_fields(self):
[17091]109        form_fields = grok.AutoFields(ICustomApplicant).omit(
110            'federalstate', 'lga', 'ward', 'punit')
[16997]111        if not getattr(self.context, 'student_id'):
112            form_fields = form_fields.omit('student_id')
113        form_fields['applicant_id'].for_display = True
114        return form_fields
115
116class CustomApplicantEditFormPage(ApplicantEditFormPage):
[16718]117    """An applicant-centered edit view for applicant data.
118    """
119
120    def unremovable(self, ticket):
121        return True
122
[16997]123    @property
[17023]124    def display_actions(self):
125        state = IWorkflowState(self.context).getState()
126        actions = [[_('Save')],
127                   [
128                    #_('Remove selected tickets')
129                   ]]
130        if state == STARTED:
131            actions = [[_('Save')],
132                [
133                 #_('Add online payment ticket'),
134                 #_('Remove selected tickets')
135                ]]
136        elif self.context.special and state == PAID:
137            actions = [[_('Save'), _('Finally Submit')],
138                [
139                 #_('Add online payment ticket'),
140                 #_('Remove selected tickets')
141                ]]
142        elif state == PAID:
143            actions = [[_('Save'), _('Finally Submit')],
144                [
145                 #_('Remove selected tickets')
146                ]]
147        applicants_utils = getUtility(IApplicantsUtils)
148        if self.context.state in applicants_utils.BALANCE_PAYMENT_STATES:
149            actions[1].append(_('Add balance payment ticket'))
150        return actions
151
152    @property
[16997]153    def form_fields(self):
[17013]154        form_fields = grok.AutoFields(ICustomApplicantEdit)
[17091]155        form_fields = form_fields.omit('locked', 'suspended', 'federalstate',
156                                       'lga', 'ward', 'punit')
[16997]157        if not getattr(self.context, 'student_id'):
158            form_fields = form_fields.omit('student_id')
159        form_fields['applicant_id'].for_display = True
160        form_fields['reg_number'].for_display = True
161        return form_fields
[17019]162       
163class CustomBalancePaymentAddFormPage(BalancePaymentAddFormPage):
164    """ Page to add an online payment which can balance s previous session
165    payment.
166    """
167    grok.require('waeup.payApplicant')
[17020]168   
169class CustomApplicantBaseDisplayFormPage(ApplicantBaseDisplayFormPage):
[16997]170
[17020]171    @property
172    def form_fields(self):
173        form_fields = grok.AutoFields(ICustomApplicant).select(
174            'applicant_id', 'reg_number', 'email')
175        return form_fields
176
177class CustomExportPDFPaymentSlipPage(NigeriaExportPDFPaymentSlipPage):
178    """Deliver a PDF slip of the context.
179    """
180    # use IApplicantOnlinePayment alternativly
181    form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit(
182        'p_item').omit('p_option').omit('p_combi')
183    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
184    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
185
186    def render(self):
187        if self.payment_slip_download_warning:
188            self.flash(self.payment_slip_download_warning, type='danger')
189            self.redirect(self.url(self.context))
190            return
191        applicantview = CustomApplicantBaseDisplayFormPage(self.context.__parent__,
192            self.request)
193        students_utils = getUtility(IStudentsUtils)
194        return students_utils.renderPDF(self,'payment_slip.pdf',
195            self.context.__parent__, applicantview, note=self.note,
[17091]196            omit_fields=())
197
198class PunitFormPage(KofaEditFormPage):
199    """
200    """
201    grok.context(ICustomApplicant)
202    grok.require('waeup.handleApplication')
203    grok.name('punitformpage')
204    form_fields = grok.AutoFields(IPollingUnit)
205    grok.template('puniteditpage')
206    label = _('Locate polling unit')
207    pnav = 3
208
[17175]209    @action(_('Save'), style='invisible')
[17091]210    def save(self, **data):
211        changed_fields = self.applyData(self.context, **data)
212        # Turn list of lists into single list
213        if changed_fields:
214            changed_fields = reduce(lambda x, y: x+y, changed_fields.values())
215        if 'federalstate' in changed_fields:
216            # Clear other form fields
217            self.context.lga = self.context.ward = self.context.punit = None
218            self.flash(_('Federal State has been saved.'))
219            return         
220        if 'lga' in changed_fields:
221            # Clear other form fields
222            self.context.ward = self.context.punit = None
223            self.flash(_('LGA has been saved.'))
224            return         
225        if 'ward' in changed_fields:
226            # Clear other form fields
227            self.context.punit = None
228            self.flash(_('Ward has been saved.'))
229            return
230        if 'punit' in changed_fields and self.context.punit:
231            self.flash(_('Polling Unit has been successfully saved.'))
232            self.redirect(self.url(self.context))
233        return
Note: See TracBrowser for help on using the repository browser.