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

Last change on this file since 16133 was 16133, checked in by Henrik Bettermann, 4 years ago

picture_editable

  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1## $Id: container.py 16133 2020-06-28 17:43:38Z 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 zope.catalog.interfaces import ICatalog
30from waeup.kofa.interfaces import MessageFactory as _
31from waeup.kofa.applicants.interfaces import (
32    IApplicantsContainer, IApplicantsContainerAdd, IApplicant,
33    IApplicantsUtils)
34from waeup.kofa.utils.helpers import attrs_to_fields
35from waeup.kofa.utils.batching import VirtualExportJobContainer
36
37def generate_applicant_id(container=None):
38    if container is not None:
39        key = r().randint(99999,1000000)
40        while str(key) in container.keys():
41            key = r().randint(99999,1000000)
42        return u"%s_%d" % (container.code, key)
43    else:
44        # In some tests we don't use containers
45        return u"xxx_1234"
46
47class VirtualApplicantsExportJobContainer(VirtualExportJobContainer):
48    """A virtual export job container for applicants container.
49    """
50
51class ApplicantsContainer(grok.Container):
52    """An applicants container contains university applicants.
53    """
54    grok.implements(IApplicantsContainer,IApplicantsContainerAdd)
55
56    description_dict = {}
57
58    local_roles = [
59        'waeup.local.ApplicationsManager',
60        ]
61
62    @property
63    def picture_editable(self):
64        # not used in base package
65        return self.with_picture
66
67    def addApplicant(self, applicant):
68        """Add an applicant.
69        """
70        if not IApplicant.providedBy(applicant):
71            raise TypeError(
72                'ApplicantsContainers contain only IApplicant instances')
73        if applicant.applicant_id is None:
74            applicant_id = generate_applicant_id(container=self)
75            applicant.applicant_id = applicant_id
76        self[applicant.application_number] = applicant
77        return
78
79    @property
80    def counts(self):
81        total = len(self)
82        code = self.code + '+'
83        cat = getUtility(ICatalog, name='applicants_catalog')
84        results = list(
85           cat.searchResults(container_code=(code, code)))
86        return len(self), len(results)
87
88    @property
89    def first_unused(self):
90        code = self.code + '-'
91        cat = getUtility(ICatalog, name='applicants_catalog')
92        results = list(
93           cat.searchResults(container_code=(code, code)))
94        if results:
95            return results[0]
96        return
97
98    @property
99    def statistics(self):
100        try:
101          statistics = getUtility(
102              IApplicantsUtils).getApplicantsStatistics(self)
103        except ComponentLookupError:  # happens in unit tests
104            return
105        return statistics
106
107    @property
108    def expired(self):
109        # Check if application has started ...
110        if not self.startdate or (
111            self.startdate > datetime.now(pytz.utc)):
112            return True
113        # ... or ended
114        if not self.enddate or (
115            self.enddate < datetime.now(pytz.utc)):
116            return True
117        return False
118
119    def writeLogMessage(self, view, message):
120        ob_class = view.__implemented__.__name__.replace('waeup.kofa.','')
121        self.__parent__.logger.info(
122            '%s - %s - %s' % (ob_class, self.code, message))
123        return
124
125    def traverse(self, name):
126        """Deliver virtual export container.
127        """
128        if name == 'exports':
129            # create a virtual exports container and return it
130            container = VirtualApplicantsExportJobContainer()
131            zope.location.location.located(container, self, 'exports')
132            return container
133        return None
134
135ApplicantsContainer = attrs_to_fields(ApplicantsContainer)
136
137# ApplicantsContainers must be importable. So we need a factory.
138class ApplicantsContainerFactory(grok.GlobalUtility):
139    """A factory for student online payments.
140    """
141    grok.implements(IFactory)
142    grok.name(u'waeup.ApplicantsContainer')
143    title = u"Create a new container for applicants.",
144    description = u"This factory instantiates new IApplicantsContainer instances."
145
146    def __call__(self, *args, **kw):
147        return ApplicantsContainer()
148
149    def getInterfaces(self):
150        return implementedBy(ApplicantsContainer)
Note: See TracBrowser for help on using the repository browser.