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

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

Finetune exporter pages.

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