source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/container.py @ 10891

Last change on this file since 10891 was 10655, checked in by Henrik Bettermann, 11 years ago

Implement an VirtualApplicantsExportJobContainer? which allows to export applicants locally. On each container page there is now an'Export applicants' button which directs to the exports overview page. Unlike student exporters, the applicants exporter can't be configured. It just exports all applicants in the container.

  • Property svn:keywords set to Id
File size: 5.2 KB
RevLine 
[5649]1## $Id: container.py 10655 2013-09-26 09:37:25Z henrik $
[6077]2##
[6478]3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
[5649]4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
[6077]8##
[5649]9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
[6077]13##
[5649]14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""
[5676]19Containers for university applicants.
[5649]20"""
[7260]21from random import SystemRandom as r
[5649]22import grok
[8665]23import pytz
24from datetime import datetime
[10655]25import zope.location.location
[8643]26from zope.component import getUtility
[6280]27from zope.component.factory import Factory
28from zope.component.interfaces import IFactory
[7847]29from waeup.kofa.interfaces import MessageFactory as _
[7811]30from waeup.kofa.applicants.interfaces import (
[8645]31    IApplicantsContainer, IApplicantsContainerAdd, IApplicant,
32    IApplicantsUtils)
[7811]33from waeup.kofa.utils.helpers import attrs_to_fields
[10655]34from waeup.kofa.utils.batching import VirtualExportJobContainer
[5649]35
[7260]36def generate_applicant_id(container=None):
37    if container is not None:
[8540]38        key = r().randint(99999,1000000)
[8543]39        while str(key) in container.keys():
[8540]40            key = r().randint(99999,1000000)
41        return u"%s_%d" % (container.code, key)
[7260]42    else:
43        # In some tests we don't use containers
44        return u"xxx_1234"
45
[10655]46class VirtualApplicantsExportJobContainer(VirtualExportJobContainer):
47    """A virtual export job container for certificates.
48    """
49
[5676]50class ApplicantsContainer(grok.Container):
51    """An applicants container contains university applicants.
[5649]52    """
[6069]53    grok.implements(IApplicantsContainer,IApplicantsContainerAdd)
[5649]54
[7903]55    #: A dictionary to hold per language translations of description string.
56    description_dict = {}
[6077]57
[8993]58    local_roles = []
[6184]59
[5649]60    def archive(self, app_ids=None):
[5676]61        """Create on-dist archive of applicants stored in this term.
[5649]62
[5676]63        If app_ids is `None`, all applicants are archived.
[5649]64
65        If app_ids contains a single id string, only the respective
[5676]66        applicants are archived.
[5649]67
68        If app_ids contains a list of id strings all of the respective
[5676]69        applicants types are saved to disk.
[5649]70        """
[6601]71        raise NotImplementedError()
[5649]72
73    def clear(self, app_ids=None, archive=True):
[5676]74        """Remove applicants of type given by 'id'.
[5649]75
[5676]76        Optionally archive the applicants.
[6077]77
[5676]78        If id is `None`, all applicants are archived.
[5649]79
80        If id contains a single id string, only the respective
[5676]81        applicants are archived.
[5649]82
83        If id contains a list of id strings all of the respective
[5676]84        applicant types are saved to disk.
[5649]85
86        If `archive` is ``False`` none of the archive-handling is done
[5676]87        and respective applicants are simply removed from the
[5649]88        database.
89        """
[6601]90        raise NotImplementedError()
[6072]91
[7240]92    def addApplicant(self, applicant):
93        """Add an applicant.
94        """
[8008]95        if not IApplicant.providedBy(applicant):
[7240]96            raise TypeError(
[8008]97                'ApplicantsContainers contain only IApplicant instances')
[8290]98        if applicant.applicant_id is None:
99            applicant_id = generate_applicant_id(container=self)
100            applicant.applicant_id = applicant_id
[7240]101        self[applicant.application_number] = applicant
102        return
103
[8563]104    @property
[8643]105    def statistics(self):
[8645]106        return getUtility(IApplicantsUtils).getApplicantsStatistics(self)
[8643]107
[8665]108    @property
109    def expired(self):
110        # Check if application has started ...
111        if not self.startdate or (
112            self.startdate > datetime.now(pytz.utc)):
113            return True
114        # ... or ended
115        if not self.enddate or (
116            self.enddate < datetime.now(pytz.utc)):
117            return True
118        return False
119
[9531]120    def writeLogMessage(self, view, message):
121        ob_class = view.__implemented__.__name__.replace('waeup.kofa.','')
122        self.__parent__.logger.info(
123            '%s - %s - %s' % (ob_class, self.code, message))
124        return
[8665]125
[10655]126    def traverse(self, name):
127        """Deliver appropriate containers.
128        """
129        if name == 'exports':
130            # create a virtual exports container and return it
131            container = VirtualApplicantsExportJobContainer()
132            zope.location.location.located(container, self, 'exports')
133            return container
134        return None
135
[6072]136ApplicantsContainer = attrs_to_fields(ApplicantsContainer)
[6077]137
[8008]138# ApplicantsContainers must be importable. So we need a factory.
139class ApplicantsContainerFactory(grok.GlobalUtility):
140    """A factory for student online payments.
[5821]141    """
[8008]142    grok.implements(IFactory)
143    grok.name(u'waeup.ApplicantsContainer')
144    title = u"Create a new container for applicants.",
145    description = u"This factory instantiates new IApplicantsContainer instances."
[5821]146
[8008]147    def __call__(self, *args, **kw):
148        return ApplicantsContainer()
[6280]149
[8008]150    def getInterfaces(self):
151        return implementedBy(ApplicantsContainer)
Note: See TracBrowser for help on using the repository browser.