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

Last change on this file since 8629 was 8601, checked in by Henrik Bettermann, 13 years ago

Display numper of submissions per faculty.

  • Property svn:keywords set to Id
File size: 4.6 KB
RevLine 
[5649]1## $Id: container.py 8601 2012-06-02 11:25:33Z 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
[6280]23from zope.component.factory import Factory
24from zope.component.interfaces import IFactory
[7847]25from waeup.kofa.interfaces import MessageFactory as _
[7811]26from waeup.kofa.applicants.interfaces import (
[8008]27    IApplicantsContainer, IApplicantsContainerAdd, IApplicant
[5821]28    )
[7811]29from waeup.kofa.utils.helpers import attrs_to_fields
[8563]30from waeup.kofa.applicants.workflow import (INITIALIZED,
31    STARTED, PAID, ADMITTED, NOT_ADMITTED, SUBMITTED, CREATED)
[5649]32
[7260]33def generate_applicant_id(container=None):
34    if container is not None:
[8540]35        key = r().randint(99999,1000000)
[8543]36        while str(key) in container.keys():
[8540]37            key = r().randint(99999,1000000)
38        return u"%s_%d" % (container.code, key)
[7260]39    else:
40        # In some tests we don't use containers
41        return u"xxx_1234"
42
[5676]43class ApplicantsContainer(grok.Container):
44    """An applicants container contains university applicants.
[5649]45    """
[6069]46    grok.implements(IApplicantsContainer,IApplicantsContainerAdd)
[5649]47
[7903]48    #: A dictionary to hold per language translations of description string.
49    description_dict = {}
[6077]50
[6184]51    @property
[7800]52    def local_roles(self):
[7182]53        return []
[6184]54
[5649]55    def archive(self, app_ids=None):
[5676]56        """Create on-dist archive of applicants stored in this term.
[5649]57
[5676]58        If app_ids is `None`, all applicants are archived.
[5649]59
60        If app_ids contains a single id string, only the respective
[5676]61        applicants are archived.
[5649]62
63        If app_ids contains a list of id strings all of the respective
[5676]64        applicants types are saved to disk.
[5649]65        """
[6601]66        raise NotImplementedError()
[5649]67
68    def clear(self, app_ids=None, archive=True):
[5676]69        """Remove applicants of type given by 'id'.
[5649]70
[5676]71        Optionally archive the applicants.
[6077]72
[5676]73        If id is `None`, all applicants are archived.
[5649]74
75        If id contains a single id string, only the respective
[5676]76        applicants are archived.
[5649]77
78        If id contains a list of id strings all of the respective
[5676]79        applicant types are saved to disk.
[5649]80
81        If `archive` is ``False`` none of the archive-handling is done
[5676]82        and respective applicants are simply removed from the
[5649]83        database.
84        """
[6601]85        raise NotImplementedError()
[6072]86
[7240]87    def addApplicant(self, applicant):
88        """Add an applicant.
89        """
[8008]90        if not IApplicant.providedBy(applicant):
[7240]91            raise TypeError(
[8008]92                'ApplicantsContainers contain only IApplicant instances')
[8290]93        if applicant.applicant_id is None:
94            applicant_id = generate_applicant_id(container=self)
95            applicant.applicant_id = applicant_id
[7240]96        self[applicant.application_number] = applicant
97        return
98
[8563]99    @property
100    def statistics(self):
[8591]101        state_stats = {INITIALIZED:0, STARTED:0, PAID:0, SUBMITTED:0,
[8563]102            ADMITTED:0, NOT_ADMITTED:0, CREATED:0}
[8591]103        faculty_keys = grok.getSite()['faculties'].keys()
104        fac_stats = dict([(i,0) for i in faculty_keys])
[8563]105        for key in self.keys():
[8601]106            state = self[key].state
107            state_stats[state] += 1
[8591]108            cert = getattr(self[key],'course1',None)
[8601]109            if  cert is not None and state == SUBMITTED:
[8591]110                faculty = cert.__parent__.__parent__.__parent__
111                fac_stats[faculty.__name__] += 1
112        return state_stats, fac_stats
[8563]113
[6072]114ApplicantsContainer = attrs_to_fields(ApplicantsContainer)
[6077]115
[8008]116# ApplicantsContainers must be importable. So we need a factory.
117class ApplicantsContainerFactory(grok.GlobalUtility):
118    """A factory for student online payments.
[5821]119    """
[8008]120    grok.implements(IFactory)
121    grok.name(u'waeup.ApplicantsContainer')
122    title = u"Create a new container for applicants.",
123    description = u"This factory instantiates new IApplicantsContainer instances."
[5821]124
[8008]125    def __call__(self, *args, **kw):
126        return ApplicantsContainer()
[6280]127
[8008]128    def getInterfaces(self):
129        return implementedBy(ApplicantsContainer)
Note: See TracBrowser for help on using the repository browser.