## $Id: browser.py 10707 2013-11-06 16:26:50Z 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.formlib.textwidgets import BytesDisplayWidget
from zope.component import getUtility
from zope.i18n import translate
from waeup.kofa.widgets.datewidget import FriendlyDatetimeDisplayWidget
from waeup.kofa.interfaces import IExtFileStore
from waeup.kofa.browser.layout import action
from waeup.kofa.students.browser import (
    StudentPersonalDisplayFormPage, StudentPersonalManageFormPage,
    StudentClearanceManageFormPage, StudentClearanceEditFormPage,
    StudentClearanceDisplayFormPage, OnlinePaymentFakeApprovePage,
    ExportPDFClearanceSlipPage, StudentBaseManageFormPage,
    StudentBaseDisplayFormPage,
    StudentBaseEditFormPage, StudentPersonalEditFormPage,
    OnlinePaymentDisplayFormPage, OnlinePaymentAddFormPage,
    OnlinePaymentBreadcrumb, ExportPDFPaymentSlipPage,
    ExportPDFCourseRegistrationSlipPage,
    ExportPDFBedTicketSlipPage,
    StudentFilesUploadPage, emit_lock_message)
from waeup.kofa.students.interfaces import IStudentsUtils
from waeup.kofa.students.viewlets import (
    PaymentReceiptActionButton, StudentPassportActionButton)
from kofacustom.nigeria.students.interfaces import (
    INigeriaStudentBase, INigeriaStudent, INigeriaStudentPersonal,
    INigeriaStudentPersonalEdit,
    INigeriaUGStudentClearance,INigeriaPGStudentClearance,
    INigeriaStudentOnlinePayment
    )
from waeup.kofa.students.workflow import ADMITTED
from kofacustom.nigeria.interfaces import MessageFactory as _

class NigeriaOnlinePaymentBreadcrumb(OnlinePaymentBreadcrumb):
    """A breadcrumb for payments.
    """
    grok.context(INigeriaStudentOnlinePayment)

class PaymentReceiptActionButton(PaymentReceiptActionButton):
    grok.order(4)
    grok.context(INigeriaStudentOnlinePayment)

class NigeriaStudentBaseDisplayFormPage(StudentBaseDisplayFormPage):
    """ Page to display student base data
    """
    form_fields = grok.AutoFields(INigeriaStudentBase).omit(
        'password', 'suspended', 'suspended_comment')

class NigeriaStudentBaseManageFormPage(StudentBaseManageFormPage):
    """ View to manage student base data
    """
    form_fields = grok.AutoFields(INigeriaStudentBase).omit(
        'student_id', 'adm_code', 'suspended')

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

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

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

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

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

    @property
    def form_fields(self):
        if self.context.is_postgrad:
            form_fields = grok.AutoFields(
                INigeriaPGStudentClearance).omit('clearance_locked')
        else:
            form_fields = grok.AutoFields(
                INigeriaUGStudentClearance).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 NigeriaExportPDFClearanceSlipPage(ExportPDFClearanceSlipPage):
    """Deliver a PDF slip of the context.
    """
    omit_fields = ('password', 'suspended', 'suspended_comment',
        'phone', 'adm_code', 'email', 'date_of_birth')

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

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

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

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

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

class NigeriaExportPDFCourseRegistrationSlipPage(ExportPDFCourseRegistrationSlipPage):
    """Deliver a PDF slip of the context.
    """
    omit_fields = ('password', 'suspended', 'suspended_comment',
        'phone', 'adm_code', 'sex', 'email', 'date_of_birth')

class NigeriaOnlinePaymentDisplayFormPage(OnlinePaymentDisplayFormPage):
    """ Page to view an online payment ticket
    """
    grok.context(INigeriaStudentOnlinePayment)
    form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).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 NigeriaOnlinePaymentAddFormPage(OnlinePaymentAddFormPage):
    """ Page to add an online payment ticket
    """
    form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).select(
        'p_category')

class NigeriaOnlinePaymentFakeApprovePage(OnlinePaymentFakeApprovePage):
    """ Disable payment approval view for students.

    This view is used for browser tests only and
    has to be neutralized here!
    """
    grok.name('fake_approve')
    grok.require('waeup.managePortal')

    def update(self):
        return

class NigeriaExportPDFPaymentSlipPage(ExportPDFPaymentSlipPage):
    """Deliver a PDF slip of the context.
    """
    grok.context(INigeriaStudentOnlinePayment)
    form_fields = grok.AutoFields(INigeriaStudentOnlinePayment).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')
    omit_fields = ('password', 'suspended', 'suspended_comment', 'phone',
        'adm_code', 'sex', 'email', 'date_of_birth')

class NigeriaExportPDFBedTicketSlipPage(ExportPDFBedTicketSlipPage):
    """Deliver a PDF slip of the context.
    """
    omit_fields = ('password', 'suspended', 'suspended_comment',
        'phone', 'adm_code', 'email', 'date_of_birth')

class StudentPassportActionButton(StudentPassportActionButton):

    @property
    def target_url(self):
        # Passport pictures must not be editable if application slip
        # exists.
        slip = getUtility(IExtFileStore).getFileByContext(
            self.context, 'application_slip')
        PWCHANGE_STATES = getUtility(IStudentsUtils).PWCHANGE_STATES
        if self.context.state not in PWCHANGE_STATES or slip is not None:
            return ''
        return self.view.url(self.view.context, self.target)

class NigeriaStudentFilesUploadPage(StudentFilesUploadPage):
    """ View to upload passport picture.

    Students are not allowed to change the picture if they
    passed the regular Kofa application.
    """

    def update(self):
        # Passport pictures must not be editable if application slip
        # exists.
        slip = getUtility(IExtFileStore).getFileByContext(
            self.context, 'application_slip')
        PWCHANGE_STATES = getUtility(IStudentsUtils).PWCHANGE_STATES
        if self.context.state not in PWCHANGE_STATES or slip is not None:
            emit_lock_message(self)
            return
        super(StudentFilesUploadPage, self).update()
        return