## $Id: utils.py 10274 2013-06-03 15:06:01Z 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
##
"""General helper functions and utilities for the application section.
"""

from time import time
import sys
import grok
from kofacustom.nigeria.applicants.utils import NigeriaApplicantsUtils
from waeup.futminna.interfaces import MessageFactory as _


class ApplicantsUtils(NigeriaApplicantsUtils):
    """A collection of parameters and methods subject to customization.
    """

    SEPARATORS_DICT = {
        'form.applicant_id': _(u'Base Data'),
        'form.course1': _(u'Programmes/Courses Desired'),
        'form.hq_type': _(u'Higher Education Record'),
        #'form.notice': _(u'Application Process Information'),
        'form.pp_school': _(u'Post Primary School Qualification'),
        'form.notice': _(u'Process Data'),
        'form.jamb_subjects': _(u'JAMB Data'),
      }

    def filterCertificates(self, context, resultset):
        """Filter and sort certificates in AppCatCertificateSource.
        """
        certs = sorted(resultset, key=lambda value: value.code)
        if context.jamb_score is not None:
            resultlist = [cert for cert in certs
                if cert.custom_float_1 is None
                  or context.jamb_score >= cert.custom_float_1]
        else:
            resultlist = certs
        curr_course = context.course1
        if curr_course is not None and curr_course not in resultlist:
            # display also current course even if certificate has been removed
            # or does not meet requirement
            resultlist = [curr_course,] + resultlist
        return resultlist

    def getCertTitle(self, context, value):
        """Compose the titles in AppCatCertificateSource.
        """
        if None not in (getattr(value, 'custom_textline_1'),
            getattr(value, 'custom_float_1')):
            return "%s - %s [%s, %d]" % (
                value.code, value.title,
                value.custom_textline_1, value.custom_float_1)
        return "%s - %s" % (value.code, value.title)
