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