1 | ## $Id: container.py 9531 2012-11-04 21:51:57Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
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. |
---|
8 | ## |
---|
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. |
---|
13 | ## |
---|
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 | """ |
---|
19 | Containers for university applicants. |
---|
20 | """ |
---|
21 | from random import SystemRandom as r |
---|
22 | import grok |
---|
23 | import pytz |
---|
24 | from datetime import datetime |
---|
25 | from zope.component import getUtility |
---|
26 | from zope.component.factory import Factory |
---|
27 | from zope.component.interfaces import IFactory |
---|
28 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
29 | from waeup.kofa.applicants.interfaces import ( |
---|
30 | IApplicantsContainer, IApplicantsContainerAdd, IApplicant, |
---|
31 | IApplicantsUtils) |
---|
32 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
33 | |
---|
34 | |
---|
35 | def generate_applicant_id(container=None): |
---|
36 | if container is not None: |
---|
37 | key = r().randint(99999,1000000) |
---|
38 | while str(key) in container.keys(): |
---|
39 | key = r().randint(99999,1000000) |
---|
40 | return u"%s_%d" % (container.code, key) |
---|
41 | else: |
---|
42 | # In some tests we don't use containers |
---|
43 | return u"xxx_1234" |
---|
44 | |
---|
45 | class ApplicantsContainer(grok.Container): |
---|
46 | """An applicants container contains university applicants. |
---|
47 | """ |
---|
48 | grok.implements(IApplicantsContainer,IApplicantsContainerAdd) |
---|
49 | |
---|
50 | #: A dictionary to hold per language translations of description string. |
---|
51 | description_dict = {} |
---|
52 | |
---|
53 | local_roles = [] |
---|
54 | |
---|
55 | def archive(self, app_ids=None): |
---|
56 | """Create on-dist archive of applicants stored in this term. |
---|
57 | |
---|
58 | If app_ids is `None`, all applicants are archived. |
---|
59 | |
---|
60 | If app_ids contains a single id string, only the respective |
---|
61 | applicants are archived. |
---|
62 | |
---|
63 | If app_ids contains a list of id strings all of the respective |
---|
64 | applicants types are saved to disk. |
---|
65 | """ |
---|
66 | raise NotImplementedError() |
---|
67 | |
---|
68 | def clear(self, app_ids=None, archive=True): |
---|
69 | """Remove applicants of type given by 'id'. |
---|
70 | |
---|
71 | Optionally archive the applicants. |
---|
72 | |
---|
73 | If id is `None`, all applicants are archived. |
---|
74 | |
---|
75 | If id contains a single id string, only the respective |
---|
76 | applicants are archived. |
---|
77 | |
---|
78 | If id contains a list of id strings all of the respective |
---|
79 | applicant types are saved to disk. |
---|
80 | |
---|
81 | If `archive` is ``False`` none of the archive-handling is done |
---|
82 | and respective applicants are simply removed from the |
---|
83 | database. |
---|
84 | """ |
---|
85 | raise NotImplementedError() |
---|
86 | |
---|
87 | def addApplicant(self, applicant): |
---|
88 | """Add an applicant. |
---|
89 | """ |
---|
90 | if not IApplicant.providedBy(applicant): |
---|
91 | raise TypeError( |
---|
92 | 'ApplicantsContainers contain only IApplicant instances') |
---|
93 | if applicant.applicant_id is None: |
---|
94 | applicant_id = generate_applicant_id(container=self) |
---|
95 | applicant.applicant_id = applicant_id |
---|
96 | self[applicant.application_number] = applicant |
---|
97 | return |
---|
98 | |
---|
99 | @property |
---|
100 | def statistics(self): |
---|
101 | return getUtility(IApplicantsUtils).getApplicantsStatistics(self) |
---|
102 | |
---|
103 | @property |
---|
104 | def expired(self): |
---|
105 | # Check if application has started ... |
---|
106 | if not self.startdate or ( |
---|
107 | self.startdate > datetime.now(pytz.utc)): |
---|
108 | return True |
---|
109 | # ... or ended |
---|
110 | if not self.enddate or ( |
---|
111 | self.enddate < datetime.now(pytz.utc)): |
---|
112 | return True |
---|
113 | return False |
---|
114 | |
---|
115 | def writeLogMessage(self, view, message): |
---|
116 | ob_class = view.__implemented__.__name__.replace('waeup.kofa.','') |
---|
117 | self.__parent__.logger.info( |
---|
118 | '%s - %s - %s' % (ob_class, self.code, message)) |
---|
119 | return |
---|
120 | |
---|
121 | ApplicantsContainer = attrs_to_fields(ApplicantsContainer) |
---|
122 | |
---|
123 | # ApplicantsContainers must be importable. So we need a factory. |
---|
124 | class ApplicantsContainerFactory(grok.GlobalUtility): |
---|
125 | """A factory for student online payments. |
---|
126 | """ |
---|
127 | grok.implements(IFactory) |
---|
128 | grok.name(u'waeup.ApplicantsContainer') |
---|
129 | title = u"Create a new container for applicants.", |
---|
130 | description = u"This factory instantiates new IApplicantsContainer instances." |
---|
131 | |
---|
132 | def __call__(self, *args, **kw): |
---|
133 | return ApplicantsContainer() |
---|
134 | |
---|
135 | def getInterfaces(self): |
---|
136 | return implementedBy(ApplicantsContainer) |
---|