## $Id: container.py 7260 2011-12-04 07:56:39Z 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
##
"""
Containers for university applicants.
"""
from random import SystemRandom as r
import grok
from zope.component.factory import Factory
from zope.component.interfaces import IFactory
from waeup.sirp.applicants.interfaces import (
    IApplicantsContainer, IApplicantsContainerAdd,
    IApplicantsContainerProvider, IApplicant
    )
from waeup.sirp.utils.helpers import attrs_to_fields

def generate_applicant_id(container=None):
    if container is not None:
        aid = u"%s_%d" % (container.code, r().randint(99999,1000000))
        while aid in container.keys():
            aid = u"%s_%d" % (container.code, r().randint(99999,1000000))
        return aid
    else:
        # In some tests we don't use containers
        return u"xxx_1234"

class ApplicantsContainer(grok.Container):
    """An applicants container contains university applicants.
    """
    grok.implements(IApplicantsContainer,IApplicantsContainerAdd)

    #: These are 'class-attributes'. Do not fiddle around with it in
    #: instances of this class.
    #:
    #: The title of this container as displayed in add-forms. It should
    #: give some idea about what kind of container this is (one or two words)
    container_title = u'Basic'

    #: Another 'class-attribute'. Do not fiddle around with it in instances
    #: of this class.
    #:
    #: The `container_description` gives a short explanation about for what
    #: purposes this container type serves (a few words only).
    container_description = u'handles basic applicants'

    @property
    def local_roles(cls):
        return []

    def archive(self, app_ids=None):
        """Create on-dist archive of applicants stored in this term.

        If app_ids is `None`, all applicants are archived.

        If app_ids contains a single id string, only the respective
        applicants are archived.

        If app_ids contains a list of id strings all of the respective
        applicants types are saved to disk.
        """
        raise NotImplementedError()

    def clear(self, app_ids=None, archive=True):
        """Remove applicants of type given by 'id'.

        Optionally archive the applicants.

        If id is `None`, all applicants are archived.

        If id contains a single id string, only the respective
        applicants are archived.

        If id contains a list of id strings all of the respective
        applicant types are saved to disk.

        If `archive` is ``False`` none of the archive-handling is done
        and respective applicants are simply removed from the
        database.
        """
        raise NotImplementedError()

    def addApplicant(self, applicant):
        """Add an applicant.
        """
        if not IApplicant.providedBy(applicant):
            raise TypeError(
                'ApplicantsContainers contain only IApplicant instances')
        applicant_id = generate_applicant_id(container=self)
        applicant.applicant_id = applicant_id
        self[applicant.application_number] = applicant
        return

ApplicantsContainer = attrs_to_fields(ApplicantsContainer)

class ApplicantsContainerProvider(grok.GlobalUtility):
    """A utility that provides basic applicants containers.
    """
    grok.implements(IApplicantsContainerProvider)
    grok.name('waeup.sirp.applicants.ApplicantsContainer')

    #: The applicants container type this provider provides:
    #: :class:`ApplicantsContainer`.
    factory = ApplicantsContainer

factory = Factory(lambda : ApplicantsContainer, 'ApplicantsContainer')
grok.global_utility(factory, IFactory, name='waeup.ApplicantsContainer')
