[16717] | 1 | ## $Id: browser.py 17201 2022-12-01 15:57:05Z 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 |
---|
| 45 | from kofacustom.nigeria.applicants.browser import NigeriaExportPDFPaymentSlipPage |
---|
[16983] | 46 | from kofacustom.lpng.applicants.interfaces import ( |
---|
[16997] | 47 | ICustomApplicant, |
---|
[16718] | 48 | ICustomApplicantOnlinePayment, |
---|
[17013] | 49 | ICustomApplicantEdit, |
---|
[17091] | 50 | IPollingUnit, |
---|
| 51 | STATES, LGAS, WARDS, PUNITS |
---|
[16718] | 52 | ) |
---|
[16983] | 53 | from kofacustom.lpng.interfaces import MessageFactory as _ |
---|
[15614] | 54 | |
---|
[17201] | 55 | |
---|
| 56 | class 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 | ) |
---|
[16997] | 64 | |
---|
| 65 | class CustomApplicantDisplayFormPage(ApplicantDisplayFormPage): |
---|
| 66 | """A display view for applicant data. |
---|
| 67 | """ |
---|
| 68 | |
---|
| 69 | @property |
---|
[17091] | 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 |
---|
[16997] | 81 | def form_fields(self): |
---|
[17091] | 82 | form_fields = grok.AutoFields( |
---|
| 83 | ICustomApplicant).omit('locked', 'suspended', 'federalstate', |
---|
| 84 | 'lga', 'ward', 'punit') |
---|
[16997] | 85 | form_fields['perm_address'].custom_widget = BytesDisplayWidget |
---|
[17091] | 86 | #form_fields['notice'].custom_widget = BytesDisplayWidget |
---|
[16997] | 87 | if not getattr(self.context, 'student_id'): |
---|
| 88 | form_fields = form_fields.omit('student_id') |
---|
| 89 | return form_fields |
---|
| 90 | |
---|
| 91 | class 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 | |
---|
| 100 | class CustomApplicantManageFormPage(ApplicantManageFormPage): |
---|
| 101 | """A full edit view for applicant data. |
---|
| 102 | """ |
---|
| 103 | |
---|
| 104 | @property |
---|
[17023] | 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 |
---|
[16997] | 118 | def form_fields(self): |
---|
[17091] | 119 | form_fields = grok.AutoFields(ICustomApplicant).omit( |
---|
| 120 | 'federalstate', 'lga', 'ward', 'punit') |
---|
[16997] | 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 | |
---|
| 126 | class CustomApplicantEditFormPage(ApplicantEditFormPage): |
---|
[16718] | 127 | """An applicant-centered edit view for applicant data. |
---|
| 128 | """ |
---|
| 129 | |
---|
| 130 | def unremovable(self, ticket): |
---|
| 131 | return True |
---|
| 132 | |
---|
[16997] | 133 | @property |
---|
[17023] | 134 | def display_actions(self): |
---|
| 135 | state = IWorkflowState(self.context).getState() |
---|
| 136 | actions = [[_('Save')], |
---|
| 137 | [ |
---|
| 138 | #_('Remove selected tickets') |
---|
| 139 | ]] |
---|
| 140 | if state == STARTED: |
---|
[17191] | 141 | actions = [[_('Save'), _('Save and Make Donation via USSD')], |
---|
[17023] | 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: |
---|
[17190] | 159 | actions[1].append(_('Make Donation')) |
---|
| 160 | #actions[1].append(_('Make Donation via USSD')) |
---|
[17023] | 161 | return actions |
---|
| 162 | |
---|
| 163 | @property |
---|
[16997] | 164 | def form_fields(self): |
---|
[17013] | 165 | form_fields = grok.AutoFields(ICustomApplicantEdit) |
---|
[17091] | 166 | form_fields = form_fields.omit('locked', 'suspended', 'federalstate', |
---|
| 167 | 'lga', 'ward', 'punit') |
---|
[16997] | 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 |
---|
[17190] | 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 | |
---|
[17191] | 193 | @action(_('Save and Make Donation via USSD'), style='primary') |
---|
[17190] | 194 | def makeUSSDonation(self, **data): |
---|
[17191] | 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) |
---|
[17195] | 199 | self.redirect(self.url(self.context, 'ussdinfo')) |
---|
[17190] | 200 | return |
---|
[17019] | 201 | |
---|
| 202 | class CustomBalancePaymentAddFormPage(BalancePaymentAddFormPage): |
---|
| 203 | """ Page to add an online payment which can balance s previous session |
---|
| 204 | payment. |
---|
| 205 | """ |
---|
| 206 | grok.require('waeup.payApplicant') |
---|
[17020] | 207 | |
---|
| 208 | class CustomApplicantBaseDisplayFormPage(ApplicantBaseDisplayFormPage): |
---|
[16997] | 209 | |
---|
[17020] | 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 | |
---|
| 216 | class 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, |
---|
[17091] | 235 | omit_fields=()) |
---|
| 236 | |
---|
| 237 | class 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 | |
---|
[17175] | 248 | @action(_('Save'), style='invisible') |
---|
[17091] | 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)) |
---|
[17190] | 272 | return |
---|
| 273 | |
---|
| 274 | class 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 |
---|