##
## applicants.py
## Login : <uli@pu.smp.net>
## Started on  Fri Jul 16 11:46:55 2010 Uli Fouquet
## $Id$
## 
## Copyright (C) 2010 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 grok import index
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
from waeup.sirp.interfaces import IObjectHistory
from waeup.sirp.app import University
from waeup.sirp.applicants.interfaces import (
    IResultEntry, IApplicant, IApplicantEdit, default_passport_image,
    )
from waeup.sirp.utils.helpers import attrs_to_fields

class ResultEntry(grok.Context):
    grok.implements(IResultEntry)

    def __init__(self, subject=None, score=None):
        self.subject = subject
        self.score = score

class Applicant(grok.Model):
    grok.implements(IApplicant,IApplicantEdit)
    grok.provides(IApplicant)

    def __init__(self):
        super(Applicant, self).__init__()
        IWorkflowInfo(self).fireTransition('init')
        self.application_date = None
        return

    #def getApplicantsRootLogger(self):
    #    return grok.getSite()['applicants'].logger

    def loggerInfo(self, ob_class, comment=None):
        target = self.__name__
        return grok.getSite()['applicants'].logger_info(ob_class,target,comment)

    @property
    def state(self):
        state = IWorkflowState(self).getState()
        return state

    @property
    def history(self):
        history = IObjectHistory(self)
        return history

# Set all attributes of Applicant required in IApplicant as field
# properties. Doing this, we do not have to set initial attributes
# ourselves and as a bonus we get free validation when an attribute is
# set.
Applicant = attrs_to_fields(Applicant)

class ApplicantCatalog(grok.Indexes):
    """A catalog indexing :class:`Applicant` instances in the ZODB.
    """
    grok.site(University)
    grok.name('applicants_catalog')
    grok.context(IApplicant)

    access_code = index.Field(attribute='access_code')

class ApplicantTraverser(grok.Traverser):
    """Get image of the context applicant.

    Each applicant can provide a passport photograph which will be
    returned by this traverser if:

    - we request the exact filename of the picture or

    - ask for a picture named 'passport.jpg'.

    If no picture was stored yet, we get a placeholder image when
    asking for `passport.jpg`.

    If none of the above applies, we return ``None``, most probably
    resulting a :exc:`NotFound` exception.

    """
    grok.context(IApplicant)
    def traverse(self, name):
        passport_filename = getattr(self.context.passport, 'filename', None)
        if name == passport_filename:
            return self.context.passport
        if name == 'passport.jpg':
            if self.context.passport is not None:
                return self.context.passport
        return

class ApplicantFactory(grok.GlobalUtility):
    """A factory for applicants.
    """
    grok.implements(IFactory)
    grok.name(u'waeup.Applicant')
    title = u"Create a new applicant.",
    description = u"This factory instantiates new applicant instances."

    def __call__(self, *args, **kw):
        return Applicant()

    def getInterfaces(self):
        return implementedBy(Applicant)
