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

Last change on this file since 7913 was 7903, checked in by uli, 13 years ago

Make description_dict a simple attribute.

  • Property svn:keywords set to Id
File size: 4.6 KB
RevLine 
[5649]1## $Id: container.py 7903 2012-03-17 23:54:05Z uli $
[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 (
[6069]27    IApplicantsContainer, IApplicantsContainerAdd,
[7240]28    IApplicantsContainerProvider, IApplicant
[5821]29    )
[7811]30from waeup.kofa.utils.helpers import attrs_to_fields
[5649]31
[7260]32def generate_applicant_id(container=None):
33    if container is not None:
34        aid = u"%s_%d" % (container.code, r().randint(99999,1000000))
35        while aid in container.keys():
36            aid = u"%s_%d" % (container.code, r().randint(99999,1000000))
37        return aid
38    else:
39        # In some tests we don't use containers
40        return u"xxx_1234"
41
[5676]42class ApplicantsContainer(grok.Container):
43    """An applicants container contains university applicants.
[5649]44    """
[6069]45    grok.implements(IApplicantsContainer,IApplicantsContainerAdd)
[5649]46
[6074]47    #: These are 'class-attributes'. Do not fiddle around with it in
48    #: instances of this class.
49    #:
50    #: The title of this container as displayed in add-forms. It should
51    #: give some idea about what kind of container this is (one or two words)
[7847]52    container_title = _(u'Basic')
[6074]53    #: The `container_description` gives a short explanation about for what
54    #: purposes this container type serves (a few words only).
[7847]55    container_description = _(u'handles basic applicants')
56    #: `iface` is the interface of the child objects.
57    iface = IApplicant
58    #: The name of the child object factory. This name is used in views
59    #: to create proper applicant objects.
60    factory_name = 'waeup.Applicant'
[7903]61    #: A dictionary to hold per language translations of description string.
62    description_dict = {}
[6077]63
[6184]64    @property
[7800]65    def local_roles(self):
[7182]66        return []
[6184]67
[5649]68    def archive(self, app_ids=None):
[5676]69        """Create on-dist archive of applicants stored in this term.
[5649]70
[5676]71        If app_ids is `None`, all applicants are archived.
[5649]72
73        If app_ids contains a single id string, only the respective
[5676]74        applicants are archived.
[5649]75
76        If app_ids contains a list of id strings all of the respective
[5676]77        applicants types are saved to disk.
[5649]78        """
[6601]79        raise NotImplementedError()
[5649]80
81    def clear(self, app_ids=None, archive=True):
[5676]82        """Remove applicants of type given by 'id'.
[5649]83
[5676]84        Optionally archive the applicants.
[6077]85
[5676]86        If id is `None`, all applicants are archived.
[5649]87
88        If id contains a single id string, only the respective
[5676]89        applicants are archived.
[5649]90
91        If id contains a list of id strings all of the respective
[5676]92        applicant types are saved to disk.
[5649]93
94        If `archive` is ``False`` none of the archive-handling is done
[5676]95        and respective applicants are simply removed from the
[5649]96        database.
97        """
[6601]98        raise NotImplementedError()
[6072]99
[7240]100    def addApplicant(self, applicant):
101        """Add an applicant.
102        """
[7847]103        if not self.iface.providedBy(applicant):
[7240]104            raise TypeError(
[7847]105                '%s containers contain only %s instances' %(
106                self.container_title, self.iface))
[7260]107        applicant_id = generate_applicant_id(container=self)
108        applicant.applicant_id = applicant_id
[7240]109        self[applicant.application_number] = applicant
110        return
111
[6072]112ApplicantsContainer = attrs_to_fields(ApplicantsContainer)
[6077]113
[5821]114class ApplicantsContainerProvider(grok.GlobalUtility):
115    """A utility that provides basic applicants containers.
116    """
[5846]117    grok.implements(IApplicantsContainerProvider)
[7811]118    grok.name('waeup.kofa.applicants.ApplicantsContainer')
[5821]119
[6074]120    #: The applicants container type this provider provides:
121    #: :class:`ApplicantsContainer`.
[5821]122    factory = ApplicantsContainer
[6280]123
124factory = Factory(lambda : ApplicantsContainer, 'ApplicantsContainer')
125grok.global_utility(factory, IFactory, name='waeup.ApplicantsContainer')
Note: See TracBrowser for help on using the repository browser.