## $Id: browser.py 12396 2015-01-04 20:20:21Z henrik $
##
## Copyright (C) 2012 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
##
import grok
from zope.i18n import translate
from zope.formlib.textwidgets import BytesDisplayWidget
from zope.component import getUtility
from waeup.kofa.interfaces import IKofaUtils
from waeup.kofa.browser.layout import UtilityView
from waeup.kofa.students.interfaces import IStudentsUtils
from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget
from waeup.kofa.students.browser import (
    StartClearancePage, BedTicketAddPage, ExportPDFAdmissionSlipPage,
    StudentBasePDFFormPage)
from waeup.kwarapoly.students.interfaces import (
    ICustomStudent, ICustomStudentBase, ICustomStudentStudyLevel,
    ICustomUGStudentClearance, ICustomPGStudentClearance)
from waeup.kwarapoly.interfaces import MessageFactory as _
from waeup.kofa.students.workflow import (
    ADMITTED, PAID, REQUESTED, RETURNING, CLEARED, REGISTERED,
    VALIDATED, GRADUATED, TRANSCRIPT, CREATED, CLEARANCE)
from kofacustom.nigeria.students.browser import (
    NigeriaOnlinePaymentDisplayFormPage,
    NigeriaOnlinePaymentAddFormPage,
    NigeriaExportPDFPaymentSlipPage,
    NigeriaStudentClearanceDisplayFormPage,
    NigeriaExportPDFClearanceSlipPage,
    NigeriaStudentClearanceEditFormPage,
    NigeriaExportPDFCourseRegistrationSlipPage,
    NigeriaStudentPersonalDisplayFormPage,
    NigeriaStudentClearanceManageFormPage,
    NigeriaStudentPersonalEditFormPage,
    NigeriaStudentPersonalManageFormPage,
    NigeriaStudentBaseEditFormPage
    )

from waeup.kwarapoly.students.interfaces import (
    ICustomStudentOnlinePayment,
    ICustomStudentPersonal,
    ICustomStudentPersonalEdit
    )


class CustomStudentBaseEditFormPage(NigeriaStudentBaseEditFormPage):
    """ View to edit student base data
    """
    form_fields = grok.AutoFields(ICustomStudentBase).select(
        'email', 'phone', 'sex')


class CustomStudentPersonalDisplayFormPage(
    NigeriaStudentPersonalDisplayFormPage):
    """ Page to display student personal data
    """
    form_fields = grok.AutoFields(ICustomStudentPersonal)
    form_fields['perm_address'].custom_widget = BytesDisplayWidget
    form_fields['next_kin_address'].custom_widget = BytesDisplayWidget
    form_fields['corr_address'].custom_widget = BytesDisplayWidget
    form_fields['sponsor_address'].custom_widget = BytesDisplayWidget
    form_fields[
        'personal_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le')


class CustomStudentPersonalEditFormPage(NigeriaStudentPersonalEditFormPage):
    """ Page to edit personal data
    """
    form_fields = grok.AutoFields(ICustomStudentPersonalEdit).omit(
        'personal_updated')


class CustomStudentPersonalManageFormPage(
    NigeriaStudentPersonalManageFormPage):
    """ Page to edit personal data
    """
    form_fields = grok.AutoFields(ICustomStudentPersonal)
    form_fields['personal_updated'].for_display = True
    form_fields[
        'personal_updated'].custom_widget = FriendlyDatetimeDisplayWidget('le')


class CustomOnlinePaymentDisplayFormPage(NigeriaOnlinePaymentDisplayFormPage):
    """ Page to view an online payment ticket
    """
    grok.context(ICustomStudentOnlinePayment)
    form_fields = grok.AutoFields(ICustomStudentOnlinePayment).omit(
        'provider_amt', 'gateway_amt', 'thirdparty_amt', 'p_item')
    form_fields[
        'creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
    form_fields[
        'payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')


class CustomOnlinePaymentAddFormPage(NigeriaOnlinePaymentAddFormPage):
    """ Page to add an online payment ticket
    """
    form_fields = grok.AutoFields(ICustomStudentOnlinePayment).select(
        'p_category')


class CustomExportPDFPaymentSlipPage(NigeriaExportPDFPaymentSlipPage):
    """Deliver a PDF slip of the context.
    """
    grok.context(ICustomStudentOnlinePayment)
    form_fields = grok.AutoFields(ICustomStudentOnlinePayment).omit(
        'provider_amt', 'gateway_amt', 'thirdparty_amt', 'p_item')
    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget(
        'le')
    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget(
        'le')


class CustomStartClearancePage(StartClearancePage):

    with_ac = False


class CustomStudentClearanceDisplayFormPage(
    NigeriaStudentClearanceDisplayFormPage):
    """ Page to display student clearance data
    """

    @property
    def form_fields(self):
        if self.context.is_postgrad:
            form_fields = grok.AutoFields(
                ICustomPGStudentClearance).omit('clearance_locked')
        else:
            form_fields = grok.AutoFields(
                ICustomUGStudentClearance).omit('clearance_locked')
        if not getattr(self.context, 'officer_comment'):
            form_fields = form_fields.omit('officer_comment')
        else:
            form_fields['officer_comment'].custom_widget = BytesDisplayWidget
        return form_fields


class CustomExportPDFClearanceSlipPage(NigeriaExportPDFClearanceSlipPage):
    """Deliver a PDF slip of the context.
    """

    @property
    def form_fields(self):
        if self.context.is_postgrad:
            form_fields = grok.AutoFields(
                ICustomPGStudentClearance).omit('clearance_locked')
        else:
            form_fields = grok.AutoFields(
                ICustomUGStudentClearance).omit('clearance_locked')
        if not getattr(self.context, 'officer_comment'):
            form_fields = form_fields.omit('officer_comment')
        return form_fields


class CustomStudentClearanceManageFormPage(
    NigeriaStudentClearanceManageFormPage):
    """ Page to edit student clearance data
    """

    @property
    def form_fields(self):
        if self.context.is_postgrad:
            form_fields = grok.AutoFields(
                ICustomPGStudentClearance).omit('clr_code')
        else:
            form_fields = grok.AutoFields(
                ICustomUGStudentClearance).omit('clr_code')
        return form_fields


class CustomStudentClearanceEditFormPage(NigeriaStudentClearanceEditFormPage):
    """ View to edit student clearance data by student
    """

    def dataNotComplete(self):
        return False

    @property
    def form_fields(self):
        if self.context.is_postgrad:
            form_fields = grok.AutoFields(ICustomPGStudentClearance).omit(
            'clearance_locked', 'nysc_location', 'clr_code', 'officer_comment',
            'physical_clearance_date')
        else:
            form_fields = grok.AutoFields(ICustomUGStudentClearance).omit(
            'clearance_locked', 'clr_code', 'officer_comment',
            'physical_clearance_date')
        return form_fields


class BedTicketAddPage(BedTicketAddPage):
    """ Page to add an online payment ticket
    """
    buttonname = _('Create bed ticket')
    notice = ''
    with_ac = False


class CustomExportPDFAdmissionSlipPage(ExportPDFAdmissionSlipPage):
    """Deliver a PDF Admission slip.
    """
    grok.context(ICustomStudent)

    omit_fields = ('date_of_birth', 'current_level')

    form_fields = grok.AutoFields(ICustomStudent).select(
        'student_id', 'reg_number')

    def render(self):
        if self.context.state in (CREATED, ADMITTED,
                                  CLEARANCE, REQUESTED, CLEARED):
            self.flash('Not allowed.')
            self.redirect(self.url(self.context))
            return
        students_utils = getUtility(IStudentsUtils)
        return students_utils.renderPDFAdmissionLetter(self,
            self.context.student, omit_fields=self.omit_fields)


class ExportPDFAdmissionNotificationPage(UtilityView, grok.View):
    """Deliver a PDF Admission notification slip.
    """
    grok.context(ICustomStudent)
    grok.name('admission_notification.pdf')
    grok.require('waeup.viewStudent')
    prefix = 'form'
    label = 'Notification of Provisional Admission'

    omit_fields = ('date_of_birth', 'current_level')

    form_fields = grok.AutoFields(ICustomStudent).select(
        'student_id', 'reg_number', 'sex', 'lga')

    def render(self):
        if self.context.state not in (ADMITTED, CLEARANCE, REQUESTED, CLEARED):
            self.flash('Not allowed.')
            self.redirect(self.url(self.context))
            return
        students_utils = getUtility(IStudentsUtils)
        pre_text = ''
        post_text = post_text_freshers
        return students_utils.renderPDFAdmissionLetter(self,
            self.context.student, omit_fields=self.omit_fields,
            pre_text=pre_text, post_text=post_text)


# copied from waeup.aaue
class CustomExportPDFCourseRegistrationSlipPage(
    NigeriaExportPDFCourseRegistrationSlipPage):
    """Deliver a PDF slip of the context.
    """
    grok.context(ICustomStudentStudyLevel)
    form_fields = grok.AutoFields(ICustomStudentStudyLevel).omit(
        'level_session', 'level_verdict',
        'validated_by', 'validation_date', 'gpa')

    omit_fields = ('password', 'suspended', 'suspended_comment',
        'phone', 'adm_code', 'sex', 'email', 'date_of_birth',
        'department', 'current_mode', 'current_level')

    @property
    def title(self):
        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
        return translate(_('Credits Registered'), 'waeup.kofa',
            target_language=portal_language)

    def _signatures(self):
        """Signatures as inserted at document bottom.

        As Kwarapoly requires a fancy 'certificate' there, the pre-
        and post-fields of signatures are quite large and unusual
        here.

        This is also a workaround, as we cannot easily insert text
        with signature fields in documents using reportlab platypus.

        The signature boxes we return here contain context infos
        (therefore this has to be a method) and return the following
        layout:

            +-----------------------------------------+
            | (Empty pre text)                        |
            +-------------+---------------------------+
            |Date         | Students Signature        |
            +-------------+---------------------------+
            | (Empty post text)                       |
            +=========================================+
            |            Certification                |
            +-------------+---------------------------+
            |Date         | Director Signature, etc.  |
            +-------------+---------------------------+
            |NOTE: This form is the ...               |
            +-----------------------------------------+


        """
        return (
            [
                ('', _('Student\'s Signature'), ''),
                ],
            [((
                    "<br/>"
                    + "&nbsp;" * 70 +
                    "<u><b><font size='14'>Certification</font></b></u><br/>"
                    "<br/><b><i>"
                    "This is to certify that "
                    "<font size='13'>"
                    + self.context.student.display_fullname +
                    "</font>"
                    " has paid the full School Fees, duly registered and "
                    "therefore, is cleared to sit for examination in the "
                    "courses listed above."
                    "</i></b><br/><br/>"
                    ),
              "Institute Director\'s Signature and Official Stamp",
              (
                    "<b><u>NOTE:</u></b> This form is the property of "
                    "Kwara State Polytechnic, it is not transferable "
                    "and students must properly fill it, get it duly "
                    "endorsed and present it before they can be admitted "
                    "into Examination Venues."
                    )), ]
            )

    def render(self):
        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
        Code = translate('Code', 'waeup.kofa', target_language=portal_language)
        Title = translate('Title', 'waeup.kofa',
                          target_language=portal_language)
        Cred = translate(
            'Cred.', 'waeup.kofa', target_language=portal_language)
        Score = translate('Score', 'waeup.kofa',
                          target_language=portal_language)
        Grade = translate('Grade', 'waeup.kofa',
                          target_language=portal_language)
        Signature = translate(_('HOD\'s Signature'), 'waeup.kwarapoly',
            target_language=portal_language)
        studentview = StudentBasePDFFormPage(self.context.student,
            self.request, self.omit_fields)
        students_utils = getUtility(IStudentsUtils)

        tabledata = []
        tableheader = []
        for i in range(1, 7):
            tabledata.append(sorted(
                [value for value in self.context.values()
                 if value.semester == i],
                key=lambda value: str(value.semester) + value.code))
            tableheader.append([(Code, 'code', 2.0),
                               (Title, 'title', 7),
                               (Cred, 'credits', 1.5),
                               (Score, 'score', 1.4),
                               (Grade, 'grade', 1.4),
                               (Signature, 'dummy', 3),
                               ])
        topMargin = 1.5 + (self.label.count('\n') * 0.2)
        return students_utils.renderPDF(
            self, 'course_registration_slip.pdf',
            self.context.student, studentview,
            tableheader=tableheader,
            tabledata=tabledata,
            signatures=self._signatures(),
            topMargin=topMargin,
            omit_fields=self.omit_fields,
            )


class ExportPDFRegistrationSlipPage(grok.View):
    """Deliver a PDF slip of the context.
    """
    grok.context(ICustomStudent)
    grok.name('registration_slip.pdf')
    grok.require('waeup.viewStudent')
    prefix = 'form'
    omit_fields = (
        'suspended', 'phone',
        'adm_code', 'suspended_comment', 'email',
        'current_mode', 'matric_number', 'date_of_birth', 'current_level')
    title = 'Clearance and Personal Data'
    label = 'Profile Registration Slip'

    form_fields = grok.AutoFields(ICustomStudent).select(
        'date_of_birth', 'lga', 'nationality',
        'perm_address', 'corr_address',
        'marit_stat', 'sponsor_name', 'sponsor_address',
        )

    def render(self):
        if self.context.state in (CREATED, ADMITTED,
                                  CLEARANCE, REQUESTED, CLEARED):
            self.flash('Not allowed.')
            self.redirect(self.url(self.context))
            return
        studentview = StudentBasePDFFormPage(self.context.student,
            self.request, self.omit_fields)
        students_utils = getUtility(IStudentsUtils)
        return students_utils.renderPDF(
            self, 'registration_slip.pdf',
            self.context.student, studentview, signatures=None,
            omit_fields=self.omit_fields,
            note=post_text_registration)

post_text_registration = """<br /><br />
<strong>IMPORTANT NOTICE</strong>
<br /><br />
This registration slip must be supplied before attendance of lectures. Note that:
<br /><br />
1. All fees due must be paid in full.
<br /><br />
2. All information required must be available on the Registration Form.
<br /><br />
3. The Signature of all appropriate Polytechnic Authority must be obtained.
<br /><br />
4. The endorsed Registration Form should be returned to the respective
   Institutes and Administrative Offices.
<br /><br />
5. No student may change his subject or course of study as shown in the
   signed Registration Form without clearance from the Admissions Office
   and the Director of the appropriate Institute.
<br /><br />
6. All candidates admitted into the HND I programmes should submit the
   photocopies of their original certificates of ND and SSCE with scratch
   card online for clearance latest a week after the admission is offered.
   Failure to comply with this directive may lead to the forfeiture of
   the admission.
<br /><br />
<!-- image size: 229x100 pixels; ratio (2.29:1) must be kept -->
<img src="${signature_img_path}" valign="-30"
     height="75" width="172" />
<br />
M.O. Adebayo<br />
Admission Officer<br />
For: Registrar
"""

post_text_freshers = """
<strong>INSTRUCTIONS TO FRESHERS</strong>
<br /><br />
You are hereby offered a provisional admission for the 2014/2015 session subject to the conditions that:
<br /><br />
1. The information given in your Application Form is correct.
<br /><br />
2. The original of your Credentials are presented for scrutiny.
<br /><br />
3. If at any time the Credentials submitted are found to be false/fake
   or incorrect, your admission shall be withdrawn.
<br /><br />
4. The name by which you are admitted and registered shall remain same
   throughout the duration of your programme.
<br /><br />
5. You are to fill and submit the Undertaking Form at the point of
   registration failure which your admission will be forfeited.
<br /><br />
6. You will dress decently covering your nakedness at all times.
<br /><br />
7. Payment of school fees will be once and in full and should be by
   Interswitch to the designated Banks. Failure to pay fees by the
   mode mentioned above by the closing date means you have declined
   the offer and your place will be given to another eligible
   candidate immediately.
<br /><br />
8. You present a Certificate of medical fitness from the Polytechnic
   Clinic.
<br /><br />
9. All indigenes of Kwara State are required to present Certificate of
   Citizenship.
<br /><br />
10. The Polytechnic reserves the right to withdraw your admission at
    any stage, if you are found to be a cultist or an expelled
    individual from any tertiary institution.
<br /><br />
11. You are prepared to take up accommodation provided on the campuses
    by the authority.
<br /><br />
12. There will be no refund of school fees once paid.
<br /><br />
13. If you accept this offer with the above stated conditions, please
    make a photocopy of this document and return it to the Admission
    Office immediately.
<br /><br />
14. You possess the entry requirement for the programme you are admitted.
<br /><br />

<!-- image size: 229x100 pixels; ratio (2.29:1) must be kept -->
<img src="${signature_img_path}" valign="-30"
     height="75" width="172" />
<br />
M.O. Adebayo<br />
Admission Officer<br />
For: Registrar
"""
# XXX: a note for <img /> tag used here (from reportlab docs):
#   The valign attribute may be set to a css like value from "baseline", "sub",
#   "super", "top", "text-top", "middle", "bottom", "text-bottom";
#   the value may also be a numeric percentage or an absolute value.
#
# Burn this remark after reading.
