[5649] | 1 | ## $Id: container.py 7847 2012-03-12 15:31:57Z 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] | 19 | Containers for university applicants. |
---|
[5649] | 20 | """ |
---|
[7260] | 21 | from random import SystemRandom as r |
---|
[5649] | 22 | import grok |
---|
[6280] | 23 | from zope.component.factory import Factory |
---|
| 24 | from zope.component.interfaces import IFactory |
---|
[7847] | 25 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7811] | 26 | from waeup.kofa.applicants.interfaces import ( |
---|
[6069] | 27 | IApplicantsContainer, IApplicantsContainerAdd, |
---|
[7240] | 28 | IApplicantsContainerProvider, IApplicant |
---|
[5821] | 29 | ) |
---|
[7811] | 30 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
[5649] | 31 | |
---|
[7260] | 32 | def 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] | 42 | class 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' |
---|
[6077] | 61 | |
---|
[6184] | 62 | @property |
---|
[7800] | 63 | def local_roles(self): |
---|
[7182] | 64 | return [] |
---|
[6184] | 65 | |
---|
[5649] | 66 | def archive(self, app_ids=None): |
---|
[5676] | 67 | """Create on-dist archive of applicants stored in this term. |
---|
[5649] | 68 | |
---|
[5676] | 69 | If app_ids is `None`, all applicants are archived. |
---|
[5649] | 70 | |
---|
| 71 | If app_ids contains a single id string, only the respective |
---|
[5676] | 72 | applicants are archived. |
---|
[5649] | 73 | |
---|
| 74 | If app_ids contains a list of id strings all of the respective |
---|
[5676] | 75 | applicants types are saved to disk. |
---|
[5649] | 76 | """ |
---|
[6601] | 77 | raise NotImplementedError() |
---|
[5649] | 78 | |
---|
| 79 | def clear(self, app_ids=None, archive=True): |
---|
[5676] | 80 | """Remove applicants of type given by 'id'. |
---|
[5649] | 81 | |
---|
[5676] | 82 | Optionally archive the applicants. |
---|
[6077] | 83 | |
---|
[5676] | 84 | If id is `None`, all applicants are archived. |
---|
[5649] | 85 | |
---|
| 86 | If id contains a single id string, only the respective |
---|
[5676] | 87 | applicants are archived. |
---|
[5649] | 88 | |
---|
| 89 | If id contains a list of id strings all of the respective |
---|
[5676] | 90 | applicant types are saved to disk. |
---|
[5649] | 91 | |
---|
| 92 | If `archive` is ``False`` none of the archive-handling is done |
---|
[5676] | 93 | and respective applicants are simply removed from the |
---|
[5649] | 94 | database. |
---|
| 95 | """ |
---|
[6601] | 96 | raise NotImplementedError() |
---|
[6072] | 97 | |
---|
[7240] | 98 | def addApplicant(self, applicant): |
---|
| 99 | """Add an applicant. |
---|
| 100 | """ |
---|
[7847] | 101 | if not self.iface.providedBy(applicant): |
---|
[7240] | 102 | raise TypeError( |
---|
[7847] | 103 | '%s containers contain only %s instances' %( |
---|
| 104 | self.container_title, self.iface)) |
---|
[7260] | 105 | applicant_id = generate_applicant_id(container=self) |
---|
| 106 | applicant.applicant_id = applicant_id |
---|
[7240] | 107 | self[applicant.application_number] = applicant |
---|
| 108 | return |
---|
| 109 | |
---|
[6072] | 110 | ApplicantsContainer = attrs_to_fields(ApplicantsContainer) |
---|
[6077] | 111 | |
---|
[5821] | 112 | class ApplicantsContainerProvider(grok.GlobalUtility): |
---|
| 113 | """A utility that provides basic applicants containers. |
---|
| 114 | """ |
---|
[5846] | 115 | grok.implements(IApplicantsContainerProvider) |
---|
[7811] | 116 | grok.name('waeup.kofa.applicants.ApplicantsContainer') |
---|
[5821] | 117 | |
---|
[6074] | 118 | #: The applicants container type this provider provides: |
---|
| 119 | #: :class:`ApplicantsContainer`. |
---|
[5821] | 120 | factory = ApplicantsContainer |
---|
[6280] | 121 | |
---|
| 122 | factory = Factory(lambda : ApplicantsContainer, 'ApplicantsContainer') |
---|
| 123 | grok.global_utility(factory, IFactory, name='waeup.ApplicantsContainer') |
---|