[5649] | 1 | ## $Id: container.py 13077 2015-06-19 15:36:21Z 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 |
---|
[8665] | 23 | import pytz |
---|
| 24 | from datetime import datetime |
---|
[10655] | 25 | import zope.location.location |
---|
[13077] | 26 | from zope.component import getUtility, ComponentLookupError |
---|
[6280] | 27 | from zope.component.factory import Factory |
---|
| 28 | from zope.component.interfaces import IFactory |
---|
[7847] | 29 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7811] | 30 | from waeup.kofa.applicants.interfaces import ( |
---|
[8645] | 31 | IApplicantsContainer, IApplicantsContainerAdd, IApplicant, |
---|
| 32 | IApplicantsUtils) |
---|
[7811] | 33 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
[10655] | 34 | from waeup.kofa.utils.batching import VirtualExportJobContainer |
---|
[5649] | 35 | |
---|
[7260] | 36 | def generate_applicant_id(container=None): |
---|
| 37 | if container is not None: |
---|
[8540] | 38 | key = r().randint(99999,1000000) |
---|
[8543] | 39 | while str(key) in container.keys(): |
---|
[8540] | 40 | key = r().randint(99999,1000000) |
---|
| 41 | return u"%s_%d" % (container.code, key) |
---|
[7260] | 42 | else: |
---|
| 43 | # In some tests we don't use containers |
---|
| 44 | return u"xxx_1234" |
---|
| 45 | |
---|
[10655] | 46 | class VirtualApplicantsExportJobContainer(VirtualExportJobContainer): |
---|
| 47 | """A virtual export job container for certificates. |
---|
| 48 | """ |
---|
| 49 | |
---|
[5676] | 50 | class ApplicantsContainer(grok.Container): |
---|
| 51 | """An applicants container contains university applicants. |
---|
[5649] | 52 | """ |
---|
[6069] | 53 | grok.implements(IApplicantsContainer,IApplicantsContainerAdd) |
---|
[5649] | 54 | |
---|
[7903] | 55 | description_dict = {} |
---|
[6077] | 56 | |
---|
[8993] | 57 | local_roles = [] |
---|
[6184] | 58 | |
---|
[7240] | 59 | def addApplicant(self, applicant): |
---|
| 60 | """Add an applicant. |
---|
| 61 | """ |
---|
[8008] | 62 | if not IApplicant.providedBy(applicant): |
---|
[7240] | 63 | raise TypeError( |
---|
[8008] | 64 | 'ApplicantsContainers contain only IApplicant instances') |
---|
[8290] | 65 | if applicant.applicant_id is None: |
---|
| 66 | applicant_id = generate_applicant_id(container=self) |
---|
| 67 | applicant.applicant_id = applicant_id |
---|
[7240] | 68 | self[applicant.application_number] = applicant |
---|
| 69 | return |
---|
| 70 | |
---|
[8563] | 71 | @property |
---|
[8643] | 72 | def statistics(self): |
---|
[13077] | 73 | try: |
---|
| 74 | statistics = getUtility( |
---|
| 75 | IApplicantsUtils).getApplicantsStatistics(self) |
---|
| 76 | except ComponentLookupError: # happens in unit tests |
---|
| 77 | return |
---|
| 78 | return statistics |
---|
[8643] | 79 | |
---|
[8665] | 80 | @property |
---|
| 81 | def expired(self): |
---|
| 82 | # Check if application has started ... |
---|
| 83 | if not self.startdate or ( |
---|
| 84 | self.startdate > datetime.now(pytz.utc)): |
---|
| 85 | return True |
---|
| 86 | # ... or ended |
---|
| 87 | if not self.enddate or ( |
---|
| 88 | self.enddate < datetime.now(pytz.utc)): |
---|
| 89 | return True |
---|
| 90 | return False |
---|
| 91 | |
---|
[9531] | 92 | def writeLogMessage(self, view, message): |
---|
| 93 | ob_class = view.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 94 | self.__parent__.logger.info( |
---|
| 95 | '%s - %s - %s' % (ob_class, self.code, message)) |
---|
| 96 | return |
---|
[8665] | 97 | |
---|
[10655] | 98 | def traverse(self, name): |
---|
[13077] | 99 | """Deliver virtual export container. |
---|
[10655] | 100 | """ |
---|
| 101 | if name == 'exports': |
---|
| 102 | # create a virtual exports container and return it |
---|
| 103 | container = VirtualApplicantsExportJobContainer() |
---|
| 104 | zope.location.location.located(container, self, 'exports') |
---|
| 105 | return container |
---|
| 106 | return None |
---|
| 107 | |
---|
[6072] | 108 | ApplicantsContainer = attrs_to_fields(ApplicantsContainer) |
---|
[6077] | 109 | |
---|
[8008] | 110 | # ApplicantsContainers must be importable. So we need a factory. |
---|
| 111 | class ApplicantsContainerFactory(grok.GlobalUtility): |
---|
| 112 | """A factory for student online payments. |
---|
[5821] | 113 | """ |
---|
[8008] | 114 | grok.implements(IFactory) |
---|
| 115 | grok.name(u'waeup.ApplicantsContainer') |
---|
| 116 | title = u"Create a new container for applicants.", |
---|
| 117 | description = u"This factory instantiates new IApplicantsContainer instances." |
---|
[5821] | 118 | |
---|
[8008] | 119 | def __call__(self, *args, **kw): |
---|
| 120 | return ApplicantsContainer() |
---|
[6280] | 121 | |
---|
[8008] | 122 | def getInterfaces(self): |
---|
| 123 | return implementedBy(ApplicantsContainer) |
---|