## $Id: browser.py 16729 2021-12-02 06:30:02Z henrik $
##
## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""UI components for basic applicants and related components.
"""
import grok
from zope.component import getUtility, queryUtility
from zope.i18n import translate
from hurry.workflow.interfaces import IWorkflowState
from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget
from zope.formlib.textwidgets import BytesDisplayWidget
from waeup.kofa.interfaces import IExtFileStore, IKofaUtils
from waeup.kofa.browser.pages import doll_up
from waeup.kofa.applicants.interfaces import (
    IApplicant, IApplicantEdit,
    IApplicantsContainer,)
from waeup.kofa.applicants.browser import (ApplicantDisplayFormPage,
    ApplicantManageFormPage, ApplicantEditFormPage,
    ApplicantsContainerPage, ApplicationFeePaymentAddPage,
    ExportJobContainerOverview,
    ExportJobContainerJobStart,
    ExportJobContainerDownload,
    ApplicantBaseDisplayFormPage)
from waeup.kofa.browser.viewlets import ManageActionButton
from waeup.kofa.applicants.viewlets import (
    PaymentReceiptActionButton, PDFActionButton)
from waeup.kofa.applicants.pdf import PDFApplicationSlip
from waeup.kofa.applicants.workflow import ADMITTED, PAID, STARTED
from waeup.kofa.students.interfaces import IStudentsUtils
from kofacustom.nigeria.applicants.interfaces import (
    UG_OMIT_DISPLAY_FIELDS,
    UG_OMIT_PDF_FIELDS,
    UG_OMIT_MANAGE_FIELDS,
    UG_OMIT_EDIT_FIELDS
    )
from kofacustom.nigeria.applicants.browser import (
    NigeriaExportPDFPaymentSlipPage)
from kofacustom.dspg.applicants.interfaces import (
    ICustomUGApplicant, ICustomUGApplicantEdit,
    ICustomSpecialApplicant,
    ICustomApplicantOnlinePayment,
    ICustomApplicant,
    ND_OMIT_DISPLAY_FIELDS,
    ND_OMIT_PDF_FIELDS,
    ND_OMIT_MANAGE_FIELDS,
    ND_OMIT_EDIT_FIELDS
    )

from kofacustom.dspg.interfaces import MessageFactory as _

UG_OMIT_EDIT_FIELDS = [
    value for value in UG_OMIT_EDIT_FIELDS
        if not value in ('jamb_subjects', 'jamb_score', 'jamb_reg_number')]

PRE_OMIT_EDIT_FIELDS = UG_OMIT_EDIT_FIELDS + [
    'firstname',
    'middlename',
    'lastname',
    #'sex',
    'jamb_score'
    ]

##### Bursary exports

class BursaryExportPaymentsActionButton(ManageActionButton):
    """ 'Export payment data' button for faculties.
    """
    grok.context(IApplicantsContainer)
    grok.view(ApplicantsContainerPage)
    grok.require('waeup.exportBursaryData')
    icon = 'actionicon_down.png'
    text = _('Export payment data')
    grok.order(4)

    @property
    def target_url(self):
        return self.view.url(self.view.context) + '/exports/bursary.html'

class BursaryExportJobContainerOverview(ExportJobContainerOverview):
    """Page that lists active applicant data export jobs and provides links
    to discard or download CSV files.
    """
    grok.require('waeup.exportBursaryData')
    grok.name('bursary.html')

    def update(self, CREATE=None, DISCARD=None, job_id=None):
        if CREATE:
            self.redirect(self.url('@@start_bursary_export'))
            return
        if DISCARD and job_id:
            entry = self.context.entry_from_job_id(job_id)
            self.context.delete_export_entry(entry)
            ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
            self.context.logger.info(
                '%s - discarded: job_id=%s' % (ob_class, job_id))
            self.flash(_('Discarded export') + ' %s' % job_id)
        self.entries = doll_up(self, user=self.request.principal.id)
        return

class  BursaryExportJobContainerJobStart(ExportJobContainerJobStart):
    """View that starts export job.
    """
    grok.require('waeup.exportBursaryData')
    grok.name('start_bursary_export')

    def update(self):
        utils = queryUtility(IKofaUtils)
        if not utils.expensive_actions_allowed():
            self.flash(_(
                "Currently, exporters cannot be started due to high "
                "system load. Please try again later."), type='danger')
            self.entries = doll_up(self, user=None)
            return

        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
        container_code = self.context.__parent__.code
        # Start payments exporter
        exporter = 'applicantpayments'
        job_id = self.context.start_export_job(exporter,
                                      self.request.principal.id,
                                      container=container_code)
        self.context.logger.info(
            '%s - exported: %s (%s), job_id=%s'
            % (ob_class, exporter, container_code, job_id))

        self.flash(_('Exports started.'))
        self.redirect(self.url(self.context, 'bursary.html'))
        return

    def render(self):
        return

class BursaryExportJobContainerDownload(ExportJobContainerDownload):
    """Page that downloads a students export csv file.

    """
    grok.require('waeup.exportBursaryData')

class CustomApplicantsContainerPage(ApplicantsContainerPage):
    """The standard view for regular applicant containers.
    """

    @property
    def form_fields(self):
        form_fields = super(CustomApplicantsContainerPage, self).form_fields
        if self.request.principal.id == 'zope.anybody':
            return form_fields.omit('application_fee')
        return form_fields

class CustomApplicantDisplayFormPage(ApplicantDisplayFormPage):
    """A display view for applicant data.
    """

    @property
    def form_fields(self):
        if self.context.special:
            return grok.AutoFields(ICustomSpecialApplicant)
        if self.target in ('conv', 'hndfincl', 'ndfincl'):
            form_fields = grok.AutoFields(ICustomSpecialApplicant)
            form_fields = form_fields.omit(
                'special_application',
                'carryover_courses_1',
                'carryover_courses_2',
                'locked', 'suspended',
                'student_id')
            return form_fields
        form_fields = grok.AutoFields(ICustomUGApplicant)
        if self.context.is_nd:
            for field in ND_OMIT_DISPLAY_FIELDS:
                form_fields = form_fields.omit(field)
        else:
            form_fields = grok.AutoFields(ICustomUGApplicant)
            for field in UG_OMIT_DISPLAY_FIELDS:
                form_fields = form_fields.omit(field)
        form_fields['notice'].custom_widget = BytesDisplayWidget
        form_fields['jamb_subjects'].custom_widget = BytesDisplayWidget
        if not getattr(self.context, 'student_id'):
            form_fields = form_fields.omit('student_id')
        if not getattr(self.context, 'screening_score'):
            form_fields = form_fields.omit('screening_score')
        if not getattr(self.context, 'screening_venue'):
            form_fields = form_fields.omit('screening_venue')
        if not getattr(self.context, 'screening_date'):
            form_fields = form_fields.omit('screening_date')
        return form_fields

class CustomPDFApplicationSlip(PDFApplicationSlip):

    def _reduced_slip(self):
        return getattr(self.context, 'result_uploaded', False)

    @property
    def note(self):
        if self.context.special:
            return '''<br /><br />
Head of Department Signature:
'''
        note = getattr(self.context.__parent__, 'application_slip_notice', None)
        if note:
            return '<br /><br />' + note
        pronoun = 'S/he'
        if self.context.sex == 'm':
            pronoun = 'He'
        else:
            pronoun = 'She'
        return '''<br /><br />
The applicant has declared that:

a) %s is not a member of any secret cult and will not join any.
b) %s will not engage in any cult activities.
c) %s will not be involved in any acts of terrorism, rape, robbery, fighting, illegal gathering and
any activities that could disrupt peace on campus.
d) %s does not have and will not acquire any form of weapon, fire arms, gun knife or any weapon
that can cause damage to life and property.
e) %s will strive to be worthy in character and learning at all times.

The applicant has acknowledged that offences will automatically lead to the expulsion from the
Polytechnic and also be dealt with in accordance with the law of the land.

The applicant promises to abide by all the Rules and Regulations of the Polytechnic if offered
admission.
''' % (
    pronoun, pronoun, pronoun, pronoun, pronoun)

    @property
    def form_fields(self):
        if self.context.special:
            return grok.AutoFields(ICustomSpecialApplicant)
        if self.target in ('conv', 'hndfincl', 'ndfincl'):
            form_fields = grok.AutoFields(ICustomSpecialApplicant)
            form_fields = form_fields.omit(
                'special_application',
                'locked', 'suspended', 'applicant_id', 'student_id')
            if not self.context.carryover_courses_1:
                form_fields = form_fields.omit('carryover_courses_1')
            if not self.context.carryover_courses_2:
                form_fields = form_fields.omit('carryover_courses_2')
            return form_fields
        form_fields = grok.AutoFields(ICustomUGApplicant)
        if self.context.is_nd:
            for field in ND_OMIT_PDF_FIELDS:
                form_fields = form_fields.omit(field)
        else:
            form_fields = grok.AutoFields(ICustomUGApplicant)
            for field in UG_OMIT_PDF_FIELDS:
                form_fields = form_fields.omit(field)
        if not getattr(self.context, 'student_id'):
            form_fields = form_fields.omit('student_id')
        if not getattr(self.context, 'screening_score'):
            form_fields = form_fields.omit('screening_score')
        if not getattr(self.context, 'screening_venue'):
            form_fields = form_fields.omit('screening_venue')
        if not getattr(self.context, 'screening_date'):
            form_fields = form_fields.omit('screening_date')
        return form_fields

class CustomApplicantManageFormPage(ApplicantManageFormPage):
    """A full edit view for applicant data.
    """

    @property
    def form_fields(self):
        if self.context.special:
            form_fields = grok.AutoFields(ICustomSpecialApplicant)
            form_fields['applicant_id'].for_display = True
            return form_fields
        if self.target in ('conv', 'hndfincl', 'ndfincl'):
            form_fields = grok.AutoFields(ICustomSpecialApplicant)
            form_fields = form_fields.omit(
                'special_application',
                'carryover_courses_1',
                'carryover_courses_2',
                'locked', 'suspended',
                'student_id')
            form_fields['applicant_id'].for_display = True
            return form_fields
        form_fields = grok.AutoFields(ICustomUGApplicant)
        if self.context.is_nd:
            for field in ND_OMIT_MANAGE_FIELDS:
                form_fields = form_fields.omit(field)
        else:
            for field in UG_OMIT_MANAGE_FIELDS:
                form_fields = form_fields.omit(field)
        form_fields['student_id'].for_display = True
        form_fields['applicant_id'].for_display = True
        return form_fields

    def setUpWidgets(self, ignore_request=False):
        super(CustomApplicantManageFormPage,self).setUpWidgets(ignore_request)
        if self.context.special:
            self.widgets['carryover_courses_1'].height = 3
            self.widgets['carryover_courses_2'].height = 3
        return

class CustomApplicantEditFormPage(ApplicantEditFormPage):
    """An applicant-centered edit view for applicant data.
    """

    def unremovable(self, ticket):
        return True

    @property
    def form_fields(self):
        if self.context.special:
            form_fields = grok.AutoFields(ICustomSpecialApplicant).omit(
                'locked', 'suspended')
            form_fields['applicant_id'].for_display = True
            return form_fields
        if self.target in ('conv', 'hndfincl', 'ndfincl'):
            form_fields = grok.AutoFields(ICustomSpecialApplicant)
            form_fields = form_fields.omit(
                'special_application',
                'carryover_courses_1',
                'carryover_courses_2',
                'locked', 'suspended',
                'student_id')
            form_fields['applicant_id'].for_display = True
            return form_fields
        form_fields = grok.AutoFields(ICustomUGApplicantEdit)
        if self.context.is_nd:
            for field in ND_OMIT_EDIT_FIELDS:
                form_fields = form_fields.omit(field)
        elif self.target is not None and self.target.startswith('pre'):
            for field in PRE_OMIT_EDIT_FIELDS:
                form_fields = form_fields.omit(field)
        else:
            for field in UG_OMIT_EDIT_FIELDS:
                form_fields = form_fields.omit(field)
        form_fields['applicant_id'].for_display = True
        form_fields['reg_number'].for_display = True
        return form_fields

    def setUpWidgets(self, ignore_request=False):
        super(CustomApplicantEditFormPage,self).setUpWidgets(ignore_request)
        if self.context.special:
            self.widgets['carryover_courses_1'].height = 3
            self.widgets['carryover_courses_2'].height = 3
        return

    @property
    def display_actions(self):
        state = IWorkflowState(self.context).getState()
        # If the form is unlocked, applicants are allowed to save the form
        # and remove unused tickets.
        actions = [[_('Save')], []]
        # Only in state started they can also add tickets.
        if state == STARTED:
            actions = [[_('Save')],
                [_('Add online payment ticket'),]]
        # In state paid, they can submit the data and further add tickets
        # if the application is special.
        elif self.context.special and state == PAID:
            actions = [[_('Save'), _('Finally Submit')],
                [_('Add online payment ticket'),]]
        elif state == PAID:
            actions = [[_('Save'), _('Finally Submit')], []]
        return actions

class CustomApplicationFeePaymentAddPage(ApplicationFeePaymentAddPage):
    """ Page to add an online payment ticket
    """

    @property
    def custom_requirements(self):
        store = getUtility(IExtFileStore)
        if not store.getFileByContext(self.context, attr=u'passport.jpg') \
            and self.context.__parent__.with_picture:
            return _('Upload your passport photo before making payment.')
        return ''

class CustomApplicantBaseDisplayFormPage(ApplicantBaseDisplayFormPage):

    grok.context(ICustomApplicant)

    @property
    def form_fields(self):
        if self.context.__parent__.prefix in (
            'conv', 'special', 'hndfincl', 'ndfincl'):
            form_fields = grok.AutoFields(ICustomApplicant).select(
                'applicant_id', 'reg_number', 'email')
            form_fields['reg_number'].field.title = u'Identification Number'
            return form_fields
        form_fields = grok.AutoFields(ICustomApplicant).select(
            'applicant_id', 'reg_number', 'email', 'course1')
        return form_fields

class CustomExportPDFPaymentSlipPage(NigeriaExportPDFPaymentSlipPage):
    """Deliver a PDF slip of the context.
    """

    grok.context(ICustomApplicantOnlinePayment)

    @property
    def form_fields(self):
        form_fields = grok.AutoFields(ICustomApplicantOnlinePayment).omit(
            'ac', 'provider_amt', 'gateway_amt', 'thirdparty_amt', 'p_item',
            'p_split_data')
        form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
        form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
        if self.context.__parent__.__parent__.prefix in (
            'conv', 'special', 'hndfincl', 'ndfincl'):
            form_fields = form_fields.omit('p_session')
        return form_fields

    def render(self):
        if self.payment_slip_download_warning:
            self.flash(self.payment_slip_download_warning, type='danger')
            self.redirect(self.url(self.context))
            return
        applicantview = CustomApplicantBaseDisplayFormPage(self.context.__parent__,
            self.request)
        students_utils = getUtility(IStudentsUtils)
        return students_utils.renderPDF(self,'payment_slip.pdf',
            self.context.__parent__, applicantview, note=self.note)