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

Last change on this file since 10843 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
Line 
1## $Id: container.py 10655 2013-09-26 09:37:25Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
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.
8##
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.
13##
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"""
19Containers for university applicants.
20"""
21from random import SystemRandom as r
22import grok
23import pytz
24from datetime import datetime
25import zope.location.location
26from zope.component import getUtility
27from zope.component.factory import Factory
28from zope.component.interfaces import IFactory
29from waeup.kofa.interfaces import MessageFactory as _
30from waeup.kofa.applicants.interfaces import (
31    IApplicantsContainer, IApplicantsContainerAdd, IApplicant,
32    IApplicantsUtils)
33from waeup.kofa.utils.helpers import attrs_to_fields
34from waeup.kofa.utils.batching import VirtualExportJobContainer
35
36def generate_applicant_id(container=None):
37    if container is not None:
38        key = r().randint(99999,1000000)
39        while str(key) in container.keys():
40            key = r().randint(99999,1000000)
41        return u"%s_%d" % (container.code, key)
42    else:
43        # In some tests we don't use containers
44        return u"xxx_1234"
45
46class VirtualApplicantsExportJobContainer(VirtualExportJobContainer):
47    """A virtual export job container for certificates.
48    """
49
50class ApplicantsContainer(grok.Container):
51    """An applicants container contains university applicants.
52    """
53    grok.implements(IApplicantsContainer,IApplicantsContainerAdd)
54
55    #: A dictionary to hold per language translations of description string.
56    description_dict = {}
57
58    local_roles = []
59
60    def archive(self, app_ids=None):
61        """Create on-dist archive of applicants stored in this term.
62
63        If app_ids is `None`, all applicants are archived.
64
65        If app_ids contains a single id string, only the respective
66        applicants are archived.
67
68        If app_ids contains a list of id strings all of the respective
69        applicants types are saved to disk.
70        """
71        raise NotImplementedError()
72
73    def clear(self, app_ids=None, archive=True):
74        """Remove applicants of type given by 'id'.
75
76        Optionally archive the applicants.
77
78        If id is `None`, all applicants are archived.
79
80        If id contains a single id string, only the respective
81        applicants are archived.
82
83        If id contains a list of id strings all of the respective
84        applicant types are saved to disk.
85
86        If `archive` is ``False`` none of the archive-handling is done
87        and respective applicants are simply removed from the
88        database.
89        """
90        raise NotImplementedError()
91
92    def addApplicant(self, applicant):
93        """Add an applicant.
94        """
95        if not IApplicant.providedBy(applicant):
96            raise TypeError(
97                'ApplicantsContainers contain only IApplicant instances')
98        if applicant.applicant_id is None:
99            applicant_id = generate_applicant_id(container=self)
100            applicant.applicant_id = applicant_id
101        self[applicant.application_number] = applicant
102        return
103
104    @property
105    def statistics(self):
106        return getUtility(IApplicantsUtils).getApplicantsStatistics(self)
107
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
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
125
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
136ApplicantsContainer = attrs_to_fields(ApplicantsContainer)
137
138# ApplicantsContainers must be importable. So we need a factory.
139class ApplicantsContainerFactory(grok.GlobalUtility):
140    """A factory for student online payments.
141    """
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."
146
147    def __call__(self, *args, **kw):
148        return ApplicantsContainer()
149
150    def getInterfaces(self):
151        return implementedBy(ApplicantsContainer)
Note: See TracBrowser for help on using the repository browser.