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

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

Fix link.

  • Property svn:keywords set to Id
File size: 10.1 KB
Line 
1## $Id: browser.py 17195 2022-12-01 10:49:59Z 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'), _('Save and Make Donation via USSD')],
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(_('Save and Make Donation via USSD'), style='primary')
184    def makeUSSDonation(self, **data):
185        if self.upload_success is False:  # False is not None!
186            # Error during image upload. Ignore other values.
187            return
188        self.applyData(self.context, **data)
189        self.redirect(self.url(self.context, 'ussdinfo'))
190        return
191       
192class CustomBalancePaymentAddFormPage(BalancePaymentAddFormPage):
193    """ Page to add an online payment which can balance s previous session
194    payment.
195    """
196    grok.require('waeup.payApplicant')
197   
198class CustomApplicantBaseDisplayFormPage(ApplicantBaseDisplayFormPage):
199
200    @property
201    def form_fields(self):
202        form_fields = grok.AutoFields(ICustomApplicant).select(
203            'applicant_id', 'reg_number', 'email')
204        return form_fields
205
206class CustomExportPDFPaymentSlipPage(NigeriaExportPDFPaymentSlipPage):
207    """Deliver a PDF slip of the context.
208    """
209    # use IApplicantOnlinePayment alternativly
210    form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit(
211        'p_item').omit('p_option').omit('p_combi')
212    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
213    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
214
215    def render(self):
216        if self.payment_slip_download_warning:
217            self.flash(self.payment_slip_download_warning, type='danger')
218            self.redirect(self.url(self.context))
219            return
220        applicantview = CustomApplicantBaseDisplayFormPage(self.context.__parent__,
221            self.request)
222        students_utils = getUtility(IStudentsUtils)
223        return students_utils.renderPDF(self,'payment_slip.pdf',
224            self.context.__parent__, applicantview, note=self.note,
225            omit_fields=())
226
227class PunitFormPage(KofaEditFormPage):
228    """
229    """
230    grok.context(ICustomApplicant)
231    grok.require('waeup.handleApplication')
232    grok.name('punitformpage')
233    form_fields = grok.AutoFields(IPollingUnit)
234    grok.template('puniteditpage')
235    label = _('Locate polling unit')
236    pnav = 3
237
238    @action(_('Save'), style='invisible')
239    def save(self, **data):
240        changed_fields = self.applyData(self.context, **data)
241        # Turn list of lists into single list
242        if changed_fields:
243            changed_fields = reduce(lambda x, y: x+y, changed_fields.values())
244        if 'federalstate' in changed_fields:
245            # Clear other form fields
246            self.context.lga = self.context.ward = self.context.punit = None
247            self.flash(_('Federal State has been saved.'))
248            return         
249        if 'lga' in changed_fields:
250            # Clear other form fields
251            self.context.ward = self.context.punit = None
252            self.flash(_('LGA has been saved.'))
253            return         
254        if 'ward' in changed_fields:
255            # Clear other form fields
256            self.context.punit = None
257            self.flash(_('Ward has been saved.'))
258            return
259        if 'punit' in changed_fields and self.context.punit:
260            self.flash(_('Polling Unit has been successfully saved.'))
261            self.redirect(self.url(self.context))
262        return
263
264class USSDInfoPage(KofaPage):
265    """
266    """
267    grok.context(ICustomApplicant)
268    grok.require('waeup.handleApplication')
269    grok.name('ussdinfo')
270    label = _('Donate via USSD')
271    grok.template('ussdinfo')
272    pnav = 3
Note: See TracBrowser for help on using the repository browser.