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

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

More customizations.

  • Property svn:keywords set to Id
File size: 11.4 KB
Line 
1## $Id: browser.py 17225 2022-12-15 11:07:25Z henrik $
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
21from zope.formlib.textwidgets import BytesDisplayWidget
22from zope.component import getUtility
23from hurry.workflow.interfaces import IWorkflowState
24from waeup.kofa.widgets.datewidget import (
25    FriendlyDateDisplayWidget,
26    FriendlyDatetimeDisplayWidget)
27from waeup.kofa.applicants.pdf import PDFApplicationSlip
28from waeup.kofa.browser.layout import KofaEditFormPage, KofaPage, action
29from waeup.kofa.applicants.browser import (
30    ApplicantRegistrationPage, ApplicantsContainerPage,
31    ApplicantDisplayFormPage,
32    ApplicantManageFormPage,
33    ApplicantEditFormPage,
34    BalancePaymentAddFormPage,
35    ExportPDFPaymentSlipPage,
36    ApplicantBaseDisplayFormPage,
37    ExportJobContainerJobStart)
38from waeup.kofa.applicants.workflow import (
39    INITIALIZED, STARTED, PAID, SUBMITTED,
40    ADMITTED, NOT_ADMITTED, CREATED, PROCESSED)
41from waeup.kofa.students.interfaces import IStudentsUtils
42from waeup.kofa.applicants.interfaces import (
43    IApplicantOnlinePayment, IApplicantsUtils)
44from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
45from kofacustom.nigeria.applicants.browser import (
46    NigeriaExportPDFPaymentSlipPage, NigeriaOnlinePaymentDisplayFormPage)
47from kofacustom.lpng.applicants.interfaces import (
48    ICustomApplicant,
49    ICustomApplicantOnlinePayment,
50    ICustomApplicantEdit,
51    IPollingUnit,
52    STATES, LGAS, WARDS, PUNITS
53    )
54from kofacustom.lpng.interfaces import MessageFactory as _
55
56
57class CustomExportJobContainerJobStart(ExportJobContainerJobStart):
58    """View that starts three export jobs, one for applicants, a second
59    one for applicant payments and a third for referee reports.
60    """
61
62    EXPORTER_LIST = ('applicants',
63                   'applicantpayments',
64                   )
65       
66class CustomApplicantDisplayFormPage(ApplicantDisplayFormPage):
67    """A display view for applicant data.
68    """
69
70    @property
71    def render_punit(self):
72        if self.context.punit:
73            punit = self.context.punit.split('_')
74            return ' -> '.join([STATES[punit[0]],
75                    LGAS['_'.join(punit[:2])],
76                    WARDS['_'.join(punit[:3])] ,
77                    PUNITS['_'.join(punit[:4])]
78                   ])
79        return
80
81    @property
82    def form_fields(self):
83        form_fields = grok.AutoFields(
84            ICustomApplicant).omit('locked', 'suspended', 'federalstate',
85                                   'lga', 'ward', 'punit')
86        form_fields['perm_address'].custom_widget = BytesDisplayWidget
87        #form_fields['notice'].custom_widget = BytesDisplayWidget
88        if not getattr(self.context, 'student_id'):
89            form_fields = form_fields.omit('student_id')
90        return form_fields
91
92class CustomPDFApplicationSlip(PDFApplicationSlip):
93
94    @property
95    def form_fields(self):
96        form_fields = grok.AutoFields(ICustomApplicant)
97        if not getattr(self.context, 'student_id'):
98            form_fields = form_fields.omit('student_id')
99        return form_fields
100
101class CustomApplicantManageFormPage(ApplicantManageFormPage):
102    """A full edit view for applicant data.
103    """
104
105    @property
106    def display_actions(self):
107        actions = [[_('Save'), _('Finally Submit')],
108                   [
109                    #_('Add online payment ticket'),
110                    _('Add balance payment ticket'),
111                    _('Remove selected tickets')
112                   ]]
113        applicants_utils = getUtility(IApplicantsUtils)
114        if self.context.state not in applicants_utils.BALANCE_PAYMENT_STATES:
115            actions[1].pop(1)
116        return actions
117
118    @property
119    def form_fields(self):
120        form_fields = grok.AutoFields(ICustomApplicant).omit(
121            'federalstate', 'lga', 'ward', 'punit')
122        if not getattr(self.context, 'student_id'):
123            form_fields = form_fields.omit('student_id')
124        form_fields['applicant_id'].for_display = True
125        return form_fields
126
127class CustomApplicantEditFormPage(ApplicantEditFormPage):
128    """An applicant-centered edit view for applicant data.
129    """
130
131    def unremovable(self, ticket):
132        return True
133
134    @property
135    def display_actions(self):
136        state = IWorkflowState(self.context).getState()
137        actions = [[_('Save')],
138                   [
139                    #_('Remove selected tickets')
140                   ]]
141        if state == STARTED:
142            actions = [[_('Save'),
143                        _('Save and Make Donation')],
144                [
145                 #_('Add online payment ticket'),
146                 #_('Remove selected tickets')
147                ]]
148        elif self.context.special and state == PAID:
149            actions = [[_('Save'), _('Finally Submit')],
150                [
151                 #_('Add online payment ticket'),
152                 #_('Remove selected tickets')
153                ]]
154        elif state == PAID:
155            actions = [[_('Save'), _('Finally Submit')],
156                [
157                 #_('Remove selected tickets')
158                ]]
159        applicants_utils = getUtility(IApplicantsUtils)
160        if self.context.state in applicants_utils.BALANCE_PAYMENT_STATES:
161            actions[1].append(_('Make Donation'))
162            #actions[1].append(_('Make Donation via USSD'))
163        return actions
164
165    @property
166    def form_fields(self):
167        form_fields = grok.AutoFields(ICustomApplicantEdit)
168        form_fields = form_fields.omit('locked', 'suspended', 'federalstate',
169                                       'lga', 'ward', 'punit')
170        if not getattr(self.context, 'student_id'):
171            form_fields = form_fields.omit('student_id')
172        form_fields['applicant_id'].for_display = True
173        form_fields['reg_number'].for_display = True
174        return form_fields
175
176    @action(_('Save'), style='primary')
177    def save(self, **data):
178        if self.upload_success is False:  # False is not None!
179            # Error during image upload. Ignore other values.
180            return
181        if not self.request.form.get('confirm_passport', False):
182            self.flash(_('Please tick the checkbox above the save buttons.'))
183            return
184        self.applyData(self.context, **data)
185        self.flash(_('Form has been saved.'))
186        return
187
188    @action(_('Add online payment ticket'), style='primary')
189    def addPaymentTicket(self, **data):
190        self.redirect(self.url(self.context, 'addafp'))
191        return
192
193    @action(_('Make Donation'), style='primary')
194    def addBalancePaymentTicket(self, **data):
195        self.redirect(self.url(self.context, 'addbp'))
196        return
197
198    @action(_('Save and Make Donation'), style='primary')
199    def saveAndDonate(self, **data):
200        if self.upload_success is False:  # False is not None!
201            # Error during image upload. Ignore other values.
202            return
203        if not self.request.form.get('confirm_passport', False):
204            self.flash(_('Please tick the checkbox above the save buttons.'))
205            return
206        self.applyData(self.context, **data)
207        self.redirect(self.url(self.context, 'addbp'))
208        return
209       
210class CustomBalancePaymentAddFormPage(BalancePaymentAddFormPage):
211    """ Page to add an online payment which can balance s previous session
212    payment.
213    """
214    grok.require('waeup.payApplicant')
215   
216class CustomApplicantBaseDisplayFormPage(ApplicantBaseDisplayFormPage):
217
218    @property
219    def form_fields(self):
220        form_fields = grok.AutoFields(ICustomApplicant).select(
221            'applicant_id', 'reg_number', 'email')
222        return form_fields
223
224class CustomOnlinePaymentDisplayFormPage(NigeriaOnlinePaymentDisplayFormPage):
225    """ Page to view an online payment ticket
226    """
227    form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit('ac',
228        'provider_amt', 'gateway_amt', 'thirdparty_amt',
229        'p_item', 'display_item', 'p_session')
230    form_fields[
231        'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
232    form_fields[
233        'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
234
235class CustomExportPDFPaymentSlipPage(NigeriaExportPDFPaymentSlipPage):
236    """Deliver a PDF slip of the context.
237    """
238    # use IApplicantOnlinePayment alternativly
239    form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit(
240        'p_item', 'p_option', 'p_combi', 'display_item', 'p_session')
241    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
242    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
243
244    def render(self):
245        if self.payment_slip_download_warning:
246            self.flash(self.payment_slip_download_warning, type='danger')
247            self.redirect(self.url(self.context))
248            return
249        applicantview = CustomApplicantBaseDisplayFormPage(self.context.__parent__,
250            self.request)
251        students_utils = getUtility(IStudentsUtils)
252        return students_utils.renderPDF(self,'payment_slip.pdf',
253            self.context.__parent__, applicantview, note=self.note,
254            omit_fields=())
255
256class PunitFormPage(KofaEditFormPage):
257    """
258    """
259    grok.context(ICustomApplicant)
260    grok.require('waeup.handleApplication')
261    grok.name('punitformpage')
262    form_fields = grok.AutoFields(IPollingUnit)
263    grok.template('puniteditpage')
264    label = _('Locate polling unit')
265    pnav = 3
266
267    @action(_('Save'), style='invisible')
268    def save(self, **data):
269        changed_fields = self.applyData(self.context, **data)
270        # Turn list of lists into single list
271        if changed_fields:
272            changed_fields = reduce(lambda x, y: x+y, changed_fields.values())
273        if 'federalstate' in changed_fields:
274            # Clear other form fields
275            self.context.lga = self.context.ward = self.context.punit = None
276            self.flash(_('Federal State has been saved.'))
277            return         
278        if 'lga' in changed_fields:
279            # Clear other form fields
280            self.context.ward = self.context.punit = None
281            self.flash(_('LGA has been saved.'))
282            return         
283        if 'ward' in changed_fields:
284            # Clear other form fields
285            self.context.punit = None
286            self.flash(_('Ward has been saved.'))
287            return
288        if 'punit' in changed_fields and self.context.punit:
289            self.flash(_('Polling Unit has been successfully saved.'))
290            self.redirect(self.url(self.context))
291        return
292
293class USSDInfoPage(KofaPage):
294    """
295    """
296    grok.context(ICustomApplicantOnlinePayment)
297    grok.require('waeup.handleApplication')
298    grok.name('ussdinfo')
299    label = _('Pay via USSD')
300    grok.template('ussdinfo')
301    pnav = 3
Note: See TracBrowser for help on using the repository browser.