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

Last change on this file since 17190 was 17190, checked in by Henrik Bettermann, 22 months ago

Add USSD payment infos.

  • Property svn:keywords set to Id
File size: 9.9 KB
Line 
1## $Id: browser.py 17190 2022-12-01 08:51:54Z 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)
37from waeup.kofa.applicants.workflow import (
38    INITIALIZED, STARTED, PAID, SUBMITTED,
39    ADMITTED, NOT_ADMITTED, CREATED, PROCESSED)
40from waeup.kofa.students.interfaces import IStudentsUtils
41from waeup.kofa.applicants.interfaces import (
42    IApplicantOnlinePayment, IApplicantsUtils)
43from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment
44from kofacustom.nigeria.applicants.browser import NigeriaExportPDFPaymentSlipPage
45from kofacustom.lpng.applicants.interfaces import (
46    ICustomApplicant,
47    ICustomApplicantOnlinePayment,
48    ICustomApplicantEdit,
49    IPollingUnit,
50    STATES, LGAS, WARDS, PUNITS
51    )
52from kofacustom.lpng.interfaces import MessageFactory as _
53
54       
55class CustomApplicantDisplayFormPage(ApplicantDisplayFormPage):
56    """A display view for applicant data.
57    """
58
59    @property
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
71    def form_fields(self):
72        form_fields = grok.AutoFields(
73            ICustomApplicant).omit('locked', 'suspended', 'federalstate',
74                                   'lga', 'ward', 'punit')
75        form_fields['perm_address'].custom_widget = BytesDisplayWidget
76        #form_fields['notice'].custom_widget = BytesDisplayWidget
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
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
108    def form_fields(self):
109        form_fields = grok.AutoFields(ICustomApplicant).omit(
110            'federalstate', 'lga', 'ward', 'punit')
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):
117    """An applicant-centered edit view for applicant data.
118    """
119
120    def unremovable(self, ticket):
121        return True
122
123    @property
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(_('Make Donation'))
150            #actions[1].append(_('Make Donation via USSD'))
151        return actions
152
153    @property
154    def form_fields(self):
155        form_fields = grok.AutoFields(ICustomApplicantEdit)
156        form_fields = form_fields.omit('locked', 'suspended', 'federalstate',
157                                       'lga', 'ward', 'punit')
158        if not getattr(self.context, 'student_id'):
159            form_fields = form_fields.omit('student_id')
160        form_fields['applicant_id'].for_display = True
161        form_fields['reg_number'].for_display = True
162        return form_fields
163
164    @action(_('Save'), style='primary')
165    def save(self, **data):
166        if self.upload_success is False:  # False is not None!
167            # Error during image upload. Ignore other values.
168            return
169        self.applyData(self.context, **data)
170        self.flash(_('Form has been saved.'))
171        return
172
173    @action(_('Add online payment ticket'), style='primary')
174    def addPaymentTicket(self, **data):
175        self.redirect(self.url(self.context, '@@addafp'))
176        return
177
178    @action(_('Make Donation'), style='primary')
179    def addBalancePaymentTicket(self, **data):
180        self.redirect(self.url(self.context, '@@addbp'))
181        return
182
183    @action(_('Make Donation via USSD Money Transfer'), style='primary')
184    def makeUSSDonation(self, **data):
185        self.redirect(self.url(self.context, '@USSSD'))
186        return
187       
188class CustomBalancePaymentAddFormPage(BalancePaymentAddFormPage):
189    """ Page to add an online payment which can balance s previous session
190    payment.
191    """
192    grok.require('waeup.payApplicant')
193   
194class CustomApplicantBaseDisplayFormPage(ApplicantBaseDisplayFormPage):
195
196    @property
197    def form_fields(self):
198        form_fields = grok.AutoFields(ICustomApplicant).select(
199            'applicant_id', 'reg_number', 'email')
200        return form_fields
201
202class CustomExportPDFPaymentSlipPage(NigeriaExportPDFPaymentSlipPage):
203    """Deliver a PDF slip of the context.
204    """
205    # use IApplicantOnlinePayment alternativly
206    form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit(
207        'p_item').omit('p_option').omit('p_combi')
208    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
209    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
210
211    def render(self):
212        if self.payment_slip_download_warning:
213            self.flash(self.payment_slip_download_warning, type='danger')
214            self.redirect(self.url(self.context))
215            return
216        applicantview = CustomApplicantBaseDisplayFormPage(self.context.__parent__,
217            self.request)
218        students_utils = getUtility(IStudentsUtils)
219        return students_utils.renderPDF(self,'payment_slip.pdf',
220            self.context.__parent__, applicantview, note=self.note,
221            omit_fields=())
222
223class PunitFormPage(KofaEditFormPage):
224    """
225    """
226    grok.context(ICustomApplicant)
227    grok.require('waeup.handleApplication')
228    grok.name('punitformpage')
229    form_fields = grok.AutoFields(IPollingUnit)
230    grok.template('puniteditpage')
231    label = _('Locate polling unit')
232    pnav = 3
233
234    @action(_('Save'), style='invisible')
235    def save(self, **data):
236        changed_fields = self.applyData(self.context, **data)
237        # Turn list of lists into single list
238        if changed_fields:
239            changed_fields = reduce(lambda x, y: x+y, changed_fields.values())
240        if 'federalstate' in changed_fields:
241            # Clear other form fields
242            self.context.lga = self.context.ward = self.context.punit = None
243            self.flash(_('Federal State has been saved.'))
244            return         
245        if 'lga' in changed_fields:
246            # Clear other form fields
247            self.context.ward = self.context.punit = None
248            self.flash(_('LGA has been saved.'))
249            return         
250        if 'ward' in changed_fields:
251            # Clear other form fields
252            self.context.punit = None
253            self.flash(_('Ward has been saved.'))
254            return
255        if 'punit' in changed_fields and self.context.punit:
256            self.flash(_('Polling Unit has been successfully saved.'))
257            self.redirect(self.url(self.context))
258        return
259
260class USSDInfoPage(KofaPage):
261    """
262    """
263    grok.context(ICustomApplicant)
264    grok.require('waeup.handleApplication')
265    grok.name('ussdinfo')
266    label = _('Donate via USSD')
267    grok.template('ussdinfo')
268    pnav = 3
Note: See TracBrowser for help on using the repository browser.