source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/container.py @ 7409

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

Let's generate the applicant_id only when the applicant is added to its container to be compliant with the generic batch processor (importer).

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