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

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

Add further permissions to the local ApplicationsManager? role and
make it assignable at applicants container level.

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