source: main/waeup.kofa/branches/uli-zc-async/src/waeup/kofa/applicants/container.py @ 9819

Last change on this file since 9819 was 9211, checked in by uli, 12 years ago

Rollback r9209. Looks like multiple merges from trunk confuse svn when merging back into trunk.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1## $Id: container.py 9211 2012-09-21 08:19:35Z uli $
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
25from zope.component import getUtility
26from zope.component.factory import Factory
27from zope.component.interfaces import IFactory
28from waeup.kofa.interfaces import MessageFactory as _
29from waeup.kofa.applicants.interfaces import (
30    IApplicantsContainer, IApplicantsContainerAdd, IApplicant,
31    IApplicantsUtils)
32from waeup.kofa.utils.helpers import attrs_to_fields
33
34
35def generate_applicant_id(container=None):
36    if container is not None:
37        key = r().randint(99999,1000000)
38        while str(key) in container.keys():
39            key = r().randint(99999,1000000)
40        return u"%s_%d" % (container.code, key)
41    else:
42        # In some tests we don't use containers
43        return u"xxx_1234"
44
45class ApplicantsContainer(grok.Container):
46    """An applicants container contains university applicants.
47    """
48    grok.implements(IApplicantsContainer,IApplicantsContainerAdd)
49
50    #: A dictionary to hold per language translations of description string.
51    description_dict = {}
52
53    @property
54    def local_roles(self):
55        return []
56
57    def archive(self, app_ids=None):
58        """Create on-dist archive of applicants stored in this term.
59
60        If app_ids is `None`, all applicants are archived.
61
62        If app_ids contains a single id string, only the respective
63        applicants are archived.
64
65        If app_ids contains a list of id strings all of the respective
66        applicants types are saved to disk.
67        """
68        raise NotImplementedError()
69
70    def clear(self, app_ids=None, archive=True):
71        """Remove applicants of type given by 'id'.
72
73        Optionally archive the applicants.
74
75        If id is `None`, all applicants are archived.
76
77        If id contains a single id string, only the respective
78        applicants are archived.
79
80        If id contains a list of id strings all of the respective
81        applicant types are saved to disk.
82
83        If `archive` is ``False`` none of the archive-handling is done
84        and respective applicants are simply removed from the
85        database.
86        """
87        raise NotImplementedError()
88
89    def addApplicant(self, applicant):
90        """Add an applicant.
91        """
92        if not IApplicant.providedBy(applicant):
93            raise TypeError(
94                'ApplicantsContainers contain only IApplicant instances')
95        if applicant.applicant_id is None:
96            applicant_id = generate_applicant_id(container=self)
97            applicant.applicant_id = applicant_id
98        self[applicant.application_number] = applicant
99        return
100
101    @property
102    def statistics(self):
103        return getUtility(IApplicantsUtils).getApplicantsStatistics(self)
104
105    @property
106    def expired(self):
107        # Check if application has started ...
108        if not self.startdate or (
109            self.startdate > datetime.now(pytz.utc)):
110            return True
111        # ... or ended
112        if not self.enddate or (
113            self.enddate < datetime.now(pytz.utc)):
114            return True
115        return False
116
117
118ApplicantsContainer = attrs_to_fields(ApplicantsContainer)
119
120# ApplicantsContainers must be importable. So we need a factory.
121class ApplicantsContainerFactory(grok.GlobalUtility):
122    """A factory for student online payments.
123    """
124    grok.implements(IFactory)
125    grok.name(u'waeup.ApplicantsContainer')
126    title = u"Create a new container for applicants.",
127    description = u"This factory instantiates new IApplicantsContainer instances."
128
129    def __call__(self, *args, **kw):
130        return ApplicantsContainer()
131
132    def getInterfaces(self):
133        return implementedBy(ApplicantsContainer)
Note: See TracBrowser for help on using the repository browser.