[5649] | 1 | ## $Id: container.py 8008 2012-03-30 10:23:32Z 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 ( |
---|
[8008] | 27 | IApplicantsContainer, IApplicantsContainerAdd, IApplicant |
---|
[5821] | 28 | ) |
---|
[7811] | 29 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
[5649] | 30 | |
---|
[7260] | 31 | def 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] | 41 | class ApplicantsContainer(grok.Container): |
---|
| 42 | """An applicants container contains university applicants. |
---|
[5649] | 43 | """ |
---|
[6069] | 44 | grok.implements(IApplicantsContainer,IApplicantsContainerAdd) |
---|
[5649] | 45 | |
---|
[7903] | 46 | #: A dictionary to hold per language translations of description string. |
---|
| 47 | description_dict = {} |
---|
[6077] | 48 | |
---|
[6184] | 49 | @property |
---|
[7800] | 50 | def local_roles(self): |
---|
[7182] | 51 | return [] |
---|
[6184] | 52 | |
---|
[5649] | 53 | def archive(self, app_ids=None): |
---|
[5676] | 54 | """Create on-dist archive of applicants stored in this term. |
---|
[5649] | 55 | |
---|
[5676] | 56 | If app_ids is `None`, all applicants are archived. |
---|
[5649] | 57 | |
---|
| 58 | If app_ids contains a single id string, only the respective |
---|
[5676] | 59 | applicants are archived. |
---|
[5649] | 60 | |
---|
| 61 | If app_ids contains a list of id strings all of the respective |
---|
[5676] | 62 | applicants types are saved to disk. |
---|
[5649] | 63 | """ |
---|
[6601] | 64 | raise NotImplementedError() |
---|
[5649] | 65 | |
---|
| 66 | def clear(self, app_ids=None, archive=True): |
---|
[5676] | 67 | """Remove applicants of type given by 'id'. |
---|
[5649] | 68 | |
---|
[5676] | 69 | Optionally archive the applicants. |
---|
[6077] | 70 | |
---|
[5676] | 71 | If id is `None`, all applicants are archived. |
---|
[5649] | 72 | |
---|
| 73 | If id contains a single id string, only the respective |
---|
[5676] | 74 | applicants are archived. |
---|
[5649] | 75 | |
---|
| 76 | If id contains a list of id strings all of the respective |
---|
[5676] | 77 | applicant types are saved to disk. |
---|
[5649] | 78 | |
---|
| 79 | If `archive` is ``False`` none of the archive-handling is done |
---|
[5676] | 80 | and respective applicants are simply removed from the |
---|
[5649] | 81 | database. |
---|
| 82 | """ |
---|
[6601] | 83 | raise NotImplementedError() |
---|
[6072] | 84 | |
---|
[7240] | 85 | def addApplicant(self, applicant): |
---|
| 86 | """Add an applicant. |
---|
| 87 | """ |
---|
[8008] | 88 | if not IApplicant.providedBy(applicant): |
---|
[7240] | 89 | raise TypeError( |
---|
[8008] | 90 | 'ApplicantsContainers contain only IApplicant instances') |
---|
[7260] | 91 | applicant_id = generate_applicant_id(container=self) |
---|
| 92 | applicant.applicant_id = applicant_id |
---|
[7240] | 93 | self[applicant.application_number] = applicant |
---|
| 94 | return |
---|
| 95 | |
---|
[6072] | 96 | ApplicantsContainer = attrs_to_fields(ApplicantsContainer) |
---|
[6077] | 97 | |
---|
[8008] | 98 | # ApplicantsContainers must be importable. So we need a factory. |
---|
| 99 | class ApplicantsContainerFactory(grok.GlobalUtility): |
---|
| 100 | """A factory for student online payments. |
---|
[5821] | 101 | """ |
---|
[8008] | 102 | grok.implements(IFactory) |
---|
| 103 | grok.name(u'waeup.ApplicantsContainer') |
---|
| 104 | title = u"Create a new container for applicants.", |
---|
| 105 | description = u"This factory instantiates new IApplicantsContainer instances." |
---|
[5821] | 106 | |
---|
[8008] | 107 | def __call__(self, *args, **kw): |
---|
| 108 | return ApplicantsContainer() |
---|
[6280] | 109 | |
---|
[8008] | 110 | def getInterfaces(self): |
---|
| 111 | return implementedBy(ApplicantsContainer) |
---|