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

Last change on this file since 17302 was 17302, checked in by Henrik Bettermann, 20 months ago

Disable USSD payments.

  • Property svn:keywords set to Id
File size: 12.3 KB
Line 
1## $Id: browser.py 17302 2023-01-17 07:29:23Z 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, NullValidator
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        return actions
163
164    @property
165    def form_fields(self):
166        form_fields = grok.AutoFields(ICustomApplicantEdit)
167        form_fields = form_fields.omit('locked', 'suspended', 'federalstate',
168                                       'lga', 'ward', 'punit')
169        if not getattr(self.context, 'student_id'):
170            form_fields = form_fields.omit('student_id')
171        form_fields['applicant_id'].for_display = True
172        form_fields['reg_number'].for_display = True
173        return form_fields
174
175    @action(_('Save'), style='primary')
176    def save(self, **data):
177        if self.upload_success is False:  # False is not None!
178            # Error during image upload. Ignore other values.
179            return
180        if not self.request.form.get('confirm_passport', False):
181            self.flash(_('Please tick the checkbox above the save buttons.'))
182            return
183        self.applyData(self.context, **data)
184        self.flash(_('Form has been saved.'))
185        return
186
187    @action(_('Add online payment ticket'), style='primary')
188    def addPaymentTicket(self, **data):
189        self.redirect(self.url(self.context, 'addafp'))
190        return
191
192    @action(_('Make Donation'), style='primary')
193    def addBalancePaymentTicket(self, **data):
194        self.redirect(self.url(self.context, 'addbp'))
195        return
196
197    @action(_('Save and Make Donation'), style='primary')
198    def saveAndDonate(self, **data):
199        if self.upload_success is False:  # False is not None!
200            # Error during image upload. Ignore other values.
201            return
202        if not self.request.form.get('confirm_passport', False):
203            self.flash(_('Please tick the checkbox above the save buttons.'))
204            return
205        self.applyData(self.context, **data)
206        self.redirect(self.url(self.context, 'addbp'))
207        return
208       
209class CustomBalancePaymentAddFormPage(BalancePaymentAddFormPage):
210    """ Page to add an online payment which can balance s previous session
211    payment.
212    """
213    grok.require('waeup.payApplicant')
214   
215    @action(_('Create ticket'), style='primary')
216    def createTicket(self, **data):
217        p_category = data['p_category']
218        form = self.request.form
219        p_option = form.get('form.p_option', None)
220        balance_amount = data.get('balance_amount', None)
221        applicants_utils = getUtility(IApplicantsUtils)
222        error, payment = applicants_utils.setBalanceDetails(
223            self.context, p_category, balance_amount)
224        if error is not None:
225            self.flash(error, type="danger")
226            return
227        if p_option:
228            payment.p_option = p_option
229            payment.p_currency = p_option
230        self.context[payment.p_id] = payment
231        self.flash(_('Payment ticket created.'))
232        self.context.writeLogMessage(self,'added: %s' % payment.p_id)
233        self.redirect(self.url(payment))
234        return
235
236    @action(_('Cancel'), validator=NullValidator)
237    def cancel(self, **data):
238        self.redirect(self.url(self.context))
239
240class CustomApplicantBaseDisplayFormPage(ApplicantBaseDisplayFormPage):
241
242    @property
243    def form_fields(self):
244        form_fields = grok.AutoFields(ICustomApplicant).select(
245            'applicant_id', 'reg_number', 'email')
246        return form_fields
247
248class CustomOnlinePaymentDisplayFormPage(NigeriaOnlinePaymentDisplayFormPage):
249    """ Page to view an online payment ticket
250    """
251    form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit('ac',
252        'provider_amt', 'gateway_amt', 'thirdparty_amt',
253        'p_item', 'display_item', 'p_session', 'p_split_data')
254    form_fields[
255        'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
256    form_fields[
257        'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
258
259class CustomExportPDFPaymentSlipPage(NigeriaExportPDFPaymentSlipPage):
260    """Deliver a PDF slip of the context.
261    """
262    # use IApplicantOnlinePayment alternativly
263    form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit(
264        'p_item', 'p_option', 'p_combi', 'display_item', 'p_session',
265        'thirdparty_amt', 'p_split_data')
266    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
267    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
268
269    def render(self):
270        if self.payment_slip_download_warning:
271            self.flash(self.payment_slip_download_warning, type='danger')
272            self.redirect(self.url(self.context))
273            return
274        applicantview = CustomApplicantBaseDisplayFormPage(self.context.__parent__,
275            self.request)
276        students_utils = getUtility(IStudentsUtils)
277        return students_utils.renderPDF(self,'payment_slip.pdf',
278            self.context.__parent__, applicantview, note=self.note,
279            omit_fields=())
280
281class PunitFormPage(KofaEditFormPage):
282    """
283    """
284    grok.context(ICustomApplicant)
285    grok.require('waeup.handleApplication')
286    grok.name('punitformpage')
287    form_fields = grok.AutoFields(IPollingUnit)
288    grok.template('puniteditpage')
289    label = _('Locate polling unit')
290    pnav = 3
291
292    @action(_('Save'), style='invisible')
293    def save(self, **data):
294        changed_fields = self.applyData(self.context, **data)
295        # Turn list of lists into single list
296        if changed_fields:
297            changed_fields = reduce(lambda x, y: x+y, changed_fields.values())
298        if 'federalstate' in changed_fields:
299            # Clear other form fields
300            self.context.lga = self.context.ward = self.context.punit = None
301            self.flash(_('Federal State has been saved.'))
302            return         
303        if 'lga' in changed_fields:
304            # Clear other form fields
305            self.context.ward = self.context.punit = None
306            self.flash(_('LGA has been saved.'))
307            return         
308        if 'ward' in changed_fields:
309            # Clear other form fields
310            self.context.punit = None
311            self.flash(_('Ward has been saved.'))
312            return
313        if 'punit' in changed_fields and self.context.punit:
314            self.flash(_('Polling Unit has been successfully saved.'))
315            self.redirect(self.url(self.context))
316        return
317
318class USSDInfoPage(KofaPage):
319    """
320    """
321    grok.context(ICustomApplicantOnlinePayment)
322    grok.require('waeup.handleApplication')
323    grok.name('ussdinfo')
324    label = _('Pay via USSD')
325    grok.template('ussdinfo')
326    pnav = 3
Note: See TracBrowser for help on using the repository browser.