[16717] | 1 | ## $Id: browser.py 17216 2022-12-13 11:40:19Z henrik $ |
---|
[15614] | 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 | """ |
---|
| 20 | import grok |
---|
[16997] | 21 | from zope.formlib.textwidgets import BytesDisplayWidget |
---|
[17020] | 22 | from zope.component import getUtility |
---|
[17023] | 23 | from hurry.workflow.interfaces import IWorkflowState |
---|
[17020] | 24 | from waeup.kofa.widgets.datewidget import ( |
---|
| 25 | FriendlyDateDisplayWidget, |
---|
| 26 | FriendlyDatetimeDisplayWidget) |
---|
[16997] | 27 | from waeup.kofa.applicants.pdf import PDFApplicationSlip |
---|
[17190] | 28 | from waeup.kofa.browser.layout import KofaEditFormPage, KofaPage, action |
---|
[15614] | 29 | from waeup.kofa.applicants.browser import ( |
---|
[16997] | 30 | ApplicantRegistrationPage, ApplicantsContainerPage, |
---|
| 31 | ApplicantDisplayFormPage, |
---|
| 32 | ApplicantManageFormPage, |
---|
[17019] | 33 | ApplicantEditFormPage, |
---|
[17020] | 34 | BalancePaymentAddFormPage, |
---|
| 35 | ExportPDFPaymentSlipPage, |
---|
[17201] | 36 | ApplicantBaseDisplayFormPage, |
---|
| 37 | ExportJobContainerJobStart) |
---|
[17023] | 38 | from waeup.kofa.applicants.workflow import ( |
---|
| 39 | INITIALIZED, STARTED, PAID, SUBMITTED, |
---|
| 40 | ADMITTED, NOT_ADMITTED, CREATED, PROCESSED) |
---|
[17020] | 41 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
[17023] | 42 | from waeup.kofa.applicants.interfaces import ( |
---|
| 43 | IApplicantOnlinePayment, IApplicantsUtils) |
---|
[17020] | 44 | from kofacustom.nigeria.applicants.interfaces import INigeriaApplicantOnlinePayment |
---|
[17216] | 45 | from kofacustom.nigeria.applicants.browser import ( |
---|
| 46 | NigeriaExportPDFPaymentSlipPage, NigeriaOnlinePaymentDisplayFormPage) |
---|
[16983] | 47 | from kofacustom.lpng.applicants.interfaces import ( |
---|
[16997] | 48 | ICustomApplicant, |
---|
[16718] | 49 | ICustomApplicantOnlinePayment, |
---|
[17013] | 50 | ICustomApplicantEdit, |
---|
[17091] | 51 | IPollingUnit, |
---|
| 52 | STATES, LGAS, WARDS, PUNITS |
---|
[16718] | 53 | ) |
---|
[16983] | 54 | from kofacustom.lpng.interfaces import MessageFactory as _ |
---|
[15614] | 55 | |
---|
[17201] | 56 | |
---|
| 57 | class 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 | ) |
---|
[16997] | 65 | |
---|
| 66 | class CustomApplicantDisplayFormPage(ApplicantDisplayFormPage): |
---|
| 67 | """A display view for applicant data. |
---|
| 68 | """ |
---|
| 69 | |
---|
| 70 | @property |
---|
[17091] | 71 | def render_punit(self): |
---|
| 72 | if self.context.punit: |
---|
| 73 | punit = self.context.punit.split('_') |
---|
[17216] | 74 | return ' -> '.join([STATES[punit[0]], |
---|
[17091] | 75 | LGAS['_'.join(punit[:2])], |
---|
| 76 | WARDS['_'.join(punit[:3])] , |
---|
| 77 | PUNITS['_'.join(punit[:4])] |
---|
| 78 | ]) |
---|
| 79 | return |
---|
| 80 | |
---|
| 81 | @property |
---|
[16997] | 82 | def form_fields(self): |
---|
[17091] | 83 | form_fields = grok.AutoFields( |
---|
| 84 | ICustomApplicant).omit('locked', 'suspended', 'federalstate', |
---|
| 85 | 'lga', 'ward', 'punit') |
---|
[16997] | 86 | form_fields['perm_address'].custom_widget = BytesDisplayWidget |
---|
[17091] | 87 | #form_fields['notice'].custom_widget = BytesDisplayWidget |
---|
[16997] | 88 | if not getattr(self.context, 'student_id'): |
---|
| 89 | form_fields = form_fields.omit('student_id') |
---|
| 90 | return form_fields |
---|
| 91 | |
---|
| 92 | class 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 | |
---|
| 101 | class CustomApplicantManageFormPage(ApplicantManageFormPage): |
---|
| 102 | """A full edit view for applicant data. |
---|
| 103 | """ |
---|
| 104 | |
---|
| 105 | @property |
---|
[17023] | 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 |
---|
[16997] | 119 | def form_fields(self): |
---|
[17091] | 120 | form_fields = grok.AutoFields(ICustomApplicant).omit( |
---|
| 121 | 'federalstate', 'lga', 'ward', 'punit') |
---|
[16997] | 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 | |
---|
| 127 | class CustomApplicantEditFormPage(ApplicantEditFormPage): |
---|
[16718] | 128 | """An applicant-centered edit view for applicant data. |
---|
| 129 | """ |
---|
| 130 | |
---|
| 131 | def unremovable(self, ticket): |
---|
| 132 | return True |
---|
| 133 | |
---|
[16997] | 134 | @property |
---|
[17023] | 135 | def display_actions(self): |
---|
| 136 | state = IWorkflowState(self.context).getState() |
---|
| 137 | actions = [[_('Save')], |
---|
| 138 | [ |
---|
| 139 | #_('Remove selected tickets') |
---|
| 140 | ]] |
---|
| 141 | if state == STARTED: |
---|
[17216] | 142 | actions = [[_('Save'), |
---|
| 143 | _('Save and Make Donation via USSD')], |
---|
[17023] | 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: |
---|
[17190] | 161 | actions[1].append(_('Make Donation')) |
---|
| 162 | #actions[1].append(_('Make Donation via USSD')) |
---|
[17023] | 163 | return actions |
---|
| 164 | |
---|
| 165 | @property |
---|
[16997] | 166 | def form_fields(self): |
---|
[17013] | 167 | form_fields = grok.AutoFields(ICustomApplicantEdit) |
---|
[17091] | 168 | form_fields = form_fields.omit('locked', 'suspended', 'federalstate', |
---|
| 169 | 'lga', 'ward', 'punit') |
---|
[16997] | 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 |
---|
[17190] | 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 | self.applyData(self.context, **data) |
---|
| 182 | self.flash(_('Form has been saved.')) |
---|
| 183 | return |
---|
| 184 | |
---|
| 185 | @action(_('Add online payment ticket'), style='primary') |
---|
| 186 | def addPaymentTicket(self, **data): |
---|
| 187 | self.redirect(self.url(self.context, '@@addafp')) |
---|
| 188 | return |
---|
| 189 | |
---|
| 190 | @action(_('Make Donation'), style='primary') |
---|
| 191 | def addBalancePaymentTicket(self, **data): |
---|
| 192 | self.redirect(self.url(self.context, '@@addbp')) |
---|
| 193 | return |
---|
| 194 | |
---|
[17191] | 195 | @action(_('Save and Make Donation via USSD'), style='primary') |
---|
[17190] | 196 | def makeUSSDonation(self, **data): |
---|
[17191] | 197 | if self.upload_success is False: # False is not None! |
---|
| 198 | # Error during image upload. Ignore other values. |
---|
| 199 | return |
---|
| 200 | self.applyData(self.context, **data) |
---|
[17195] | 201 | self.redirect(self.url(self.context, 'ussdinfo')) |
---|
[17190] | 202 | return |
---|
[17019] | 203 | |
---|
| 204 | class CustomBalancePaymentAddFormPage(BalancePaymentAddFormPage): |
---|
| 205 | """ Page to add an online payment which can balance s previous session |
---|
| 206 | payment. |
---|
| 207 | """ |
---|
| 208 | grok.require('waeup.payApplicant') |
---|
[17020] | 209 | |
---|
| 210 | class CustomApplicantBaseDisplayFormPage(ApplicantBaseDisplayFormPage): |
---|
[16997] | 211 | |
---|
[17020] | 212 | @property |
---|
| 213 | def form_fields(self): |
---|
| 214 | form_fields = grok.AutoFields(ICustomApplicant).select( |
---|
| 215 | 'applicant_id', 'reg_number', 'email') |
---|
| 216 | return form_fields |
---|
| 217 | |
---|
[17216] | 218 | class CustomOnlinePaymentDisplayFormPage(NigeriaOnlinePaymentDisplayFormPage): |
---|
| 219 | """ Page to view an online payment ticket |
---|
| 220 | """ |
---|
| 221 | form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit('ac', |
---|
| 222 | 'provider_amt', 'gateway_amt', 'thirdparty_amt', |
---|
| 223 | 'p_item', 'display_item', 'p_session') |
---|
| 224 | form_fields[ |
---|
| 225 | 'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 226 | form_fields[ |
---|
| 227 | 'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 228 | |
---|
[17020] | 229 | class CustomExportPDFPaymentSlipPage(NigeriaExportPDFPaymentSlipPage): |
---|
| 230 | """Deliver a PDF slip of the context. |
---|
| 231 | """ |
---|
| 232 | # use IApplicantOnlinePayment alternativly |
---|
| 233 | form_fields = grok.AutoFields(INigeriaApplicantOnlinePayment).omit( |
---|
[17216] | 234 | 'p_item', 'p_option', 'p_combi', 'display_item', 'p_session') |
---|
[17020] | 235 | form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 236 | form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le') |
---|
| 237 | |
---|
| 238 | def render(self): |
---|
| 239 | if self.payment_slip_download_warning: |
---|
| 240 | self.flash(self.payment_slip_download_warning, type='danger') |
---|
| 241 | self.redirect(self.url(self.context)) |
---|
| 242 | return |
---|
| 243 | applicantview = CustomApplicantBaseDisplayFormPage(self.context.__parent__, |
---|
| 244 | self.request) |
---|
| 245 | students_utils = getUtility(IStudentsUtils) |
---|
| 246 | return students_utils.renderPDF(self,'payment_slip.pdf', |
---|
| 247 | self.context.__parent__, applicantview, note=self.note, |
---|
[17091] | 248 | omit_fields=()) |
---|
| 249 | |
---|
| 250 | class PunitFormPage(KofaEditFormPage): |
---|
| 251 | """ |
---|
| 252 | """ |
---|
| 253 | grok.context(ICustomApplicant) |
---|
| 254 | grok.require('waeup.handleApplication') |
---|
| 255 | grok.name('punitformpage') |
---|
| 256 | form_fields = grok.AutoFields(IPollingUnit) |
---|
| 257 | grok.template('puniteditpage') |
---|
| 258 | label = _('Locate polling unit') |
---|
| 259 | pnav = 3 |
---|
| 260 | |
---|
[17175] | 261 | @action(_('Save'), style='invisible') |
---|
[17091] | 262 | def save(self, **data): |
---|
| 263 | changed_fields = self.applyData(self.context, **data) |
---|
| 264 | # Turn list of lists into single list |
---|
| 265 | if changed_fields: |
---|
| 266 | changed_fields = reduce(lambda x, y: x+y, changed_fields.values()) |
---|
| 267 | if 'federalstate' in changed_fields: |
---|
| 268 | # Clear other form fields |
---|
| 269 | self.context.lga = self.context.ward = self.context.punit = None |
---|
| 270 | self.flash(_('Federal State has been saved.')) |
---|
| 271 | return |
---|
| 272 | if 'lga' in changed_fields: |
---|
| 273 | # Clear other form fields |
---|
| 274 | self.context.ward = self.context.punit = None |
---|
| 275 | self.flash(_('LGA has been saved.')) |
---|
| 276 | return |
---|
| 277 | if 'ward' in changed_fields: |
---|
| 278 | # Clear other form fields |
---|
| 279 | self.context.punit = None |
---|
| 280 | self.flash(_('Ward has been saved.')) |
---|
| 281 | return |
---|
| 282 | if 'punit' in changed_fields and self.context.punit: |
---|
| 283 | self.flash(_('Polling Unit has been successfully saved.')) |
---|
| 284 | self.redirect(self.url(self.context)) |
---|
[17190] | 285 | return |
---|
| 286 | |
---|
| 287 | class USSDInfoPage(KofaPage): |
---|
| 288 | """ |
---|
| 289 | """ |
---|
[17216] | 290 | grok.context(ICustomApplicantOnlinePayment) |
---|
[17190] | 291 | grok.require('waeup.handleApplication') |
---|
| 292 | grok.name('ussdinfo') |
---|
[17216] | 293 | label = _('Pay via USSD') |
---|
[17190] | 294 | grok.template('ussdinfo') |
---|
| 295 | pnav = 3 |
---|