## $Id: browser.py 9017 2012-07-18 10:42:43Z 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,
    StudentFilesUploadPage, emit_lock_message)
from waeup.kofa.students.viewlets import (
    PaymentReceiptActionButton, StudentPassportActionButton)
from kofacustom.nigeria.students.interfaces import (
    INigeriaStudentBase, INigeriaStudent, INigeriaStudentPersonal,
    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')

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

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


class NigeriaStudentPersonalEditFormPage(StudentPersonalEditFormPage):
    """ Page to edit personal data
    """
    form_fields = grok.AutoFields(INigeriaStudentPersonal)


class NigeriaStudentPersonalManageFormPage(StudentPersonalManageFormPage):
    """ Page to edit personal data
    """
    form_fields = grok.AutoFields(INigeriaStudentPersonal)


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

    @property
    def form_fields(self):
        cm = getattr(self.context,'current_mode', None)
        if cm is not None and cm.startswith('pg'):
            form_fields = grok.AutoFields(
                INigeriaPGStudentClearance).omit('clearance_locked')
        else:
            form_fields = grok.AutoFields(
                INigeriaUGStudentClearance).omit('clearance_locked')
        return form_fields

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

    @property
    def form_fields(self):
        cm = getattr(self.context,'current_mode', None)
        if cm is not None and cm.startswith('pg'):
            form_fields = grok.AutoFields(
                INigeriaPGStudentClearance).omit('clearance_locked')
        else:
            form_fields = grok.AutoFields(
                INigeriaUGStudentClearance).omit('clearance_locked')
        return form_fields

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

    @property
    def form_fields(self):
        cm = getattr(self.context,'current_mode', None)
        if cm is not None and cm.startswith('pg'):
            form_fields = grok.AutoFields(INigeriaPGStudentClearance)
        else:
            form_fields = grok.AutoFields(INigeriaUGStudentClearance)
        return form_fields

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

    @property
    def form_fields(self):
        cm = getattr(self.context,'current_mode', None)
        if cm is not None and cm.startswith('pg'):
            form_fields = grok.AutoFields(INigeriaPGStudentClearance).omit(
            'clearance_locked', 'nysc_location')
        else:
            form_fields = grok.AutoFields(INigeriaUGStudentClearance).omit(
            'clearance_locked')
        return form_fields

    def dataNotComplete(self):
        store = getUtility(IExtFileStore)
        if not store.getFileByContext(self.context, attr=u'birth_certificate.jpg'):
            return _('No birth certificate uploaded.')
        if not store.getFileByContext(self.context, attr=u'ref_let.jpg'):
            return _('No referee letter uploaded.')
        if not store.getFileByContext(self.context, attr=u'acc_let.jpg'):
            return _('No acceptance letter uploaded.')
        if not store.getFileByContext(self.context, attr=u'fst_sit_scan.jpg'):
            return _('No first sitting result uploaded.')
        return False

class NigeriaOnlinePaymentDisplayFormPage(OnlinePaymentDisplayFormPage):
    """ Page to view an online payment ticket
    """
    grok.context(INigeriaStudentOnlinePayment)
    form_fields = grok.AutoFields(INigeriaStudentOnlinePayment)
    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)
    form_fields['creation_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')
    form_fields['payment_date'].custom_widget = FriendlyDatetimeDisplayWidget('le')

class StudentPassportActionButton(StudentPassportActionButton):

    @property
    def target_url(self):
        slip = getUtility(IExtFileStore).getFileByContext(
            self.context, 'application_slip')
        if self.context.state != ADMITTED 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):
        slip = getUtility(IExtFileStore).getFileByContext(
            self.context, 'application_slip')
        if self.context.state != ADMITTED or slip is not None:
            emit_lock_message(self)
            return
        super(StudentFilesUploadPage, self).update()
        return