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

Last change on this file since 13100 was 13077, checked in by Henrik Bettermann, 9 years ago

More adjustments for a proper documentation.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1## $Id: container.py 13077 2015-06-19 15:36:21Z 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, ComponentLookupError
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    description_dict = {}
56
57    local_roles = []
58
59    def addApplicant(self, applicant):
60        """Add an applicant.
61        """
62        if not IApplicant.providedBy(applicant):
63            raise TypeError(
64                'ApplicantsContainers contain only IApplicant instances')
65        if applicant.applicant_id is None:
66            applicant_id = generate_applicant_id(container=self)
67            applicant.applicant_id = applicant_id
68        self[applicant.application_number] = applicant
69        return
70
71    @property
72    def statistics(self):
73        try:
74          statistics = getUtility(
75              IApplicantsUtils).getApplicantsStatistics(self)
76        except ComponentLookupError:  # happens in unit tests
77            return
78        return statistics
79
80    @property
81    def expired(self):
82        # Check if application has started ...
83        if not self.startdate or (
84            self.startdate > datetime.now(pytz.utc)):
85            return True
86        # ... or ended
87        if not self.enddate or (
88            self.enddate < datetime.now(pytz.utc)):
89            return True
90        return False
91
92    def writeLogMessage(self, view, message):
93        ob_class = view.__implemented__.__name__.replace('waeup.kofa.','')
94        self.__parent__.logger.info(
95            '%s - %s - %s' % (ob_class, self.code, message))
96        return
97
98    def traverse(self, name):
99        """Deliver virtual export container.
100        """
101        if name == 'exports':
102            # create a virtual exports container and return it
103            container = VirtualApplicantsExportJobContainer()
104            zope.location.location.located(container, self, 'exports')
105            return container
106        return None
107
108ApplicantsContainer = attrs_to_fields(ApplicantsContainer)
109
110# ApplicantsContainers must be importable. So we need a factory.
111class ApplicantsContainerFactory(grok.GlobalUtility):
112    """A factory for student online payments.
113    """
114    grok.implements(IFactory)
115    grok.name(u'waeup.ApplicantsContainer')
116    title = u"Create a new container for applicants.",
117    description = u"This factory instantiates new IApplicantsContainer instances."
118
119    def __call__(self, *args, **kw):
120        return ApplicantsContainer()
121
122    def getInterfaces(self):
123        return implementedBy(ApplicantsContainer)
Note: See TracBrowser for help on using the repository browser.