1 | ## $Id: container.py 7903 2012-03-17 23:54:05Z uli $ |
---|
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 | #: A dictionary to hold per language translations of description string. |
---|
62 | description_dict = {} |
---|
63 | |
---|
64 | @property |
---|
65 | def local_roles(self): |
---|
66 | return [] |
---|
67 | |
---|
68 | def archive(self, app_ids=None): |
---|
69 | """Create on-dist archive of applicants stored in this term. |
---|
70 | |
---|
71 | If app_ids is `None`, all applicants are archived. |
---|
72 | |
---|
73 | If app_ids contains a single id string, only the respective |
---|
74 | applicants are archived. |
---|
75 | |
---|
76 | If app_ids contains a list of id strings all of the respective |
---|
77 | applicants types are saved to disk. |
---|
78 | """ |
---|
79 | raise NotImplementedError() |
---|
80 | |
---|
81 | def clear(self, app_ids=None, archive=True): |
---|
82 | """Remove applicants of type given by 'id'. |
---|
83 | |
---|
84 | Optionally archive the applicants. |
---|
85 | |
---|
86 | If id is `None`, all applicants are archived. |
---|
87 | |
---|
88 | If id contains a single id string, only the respective |
---|
89 | applicants are archived. |
---|
90 | |
---|
91 | If id contains a list of id strings all of the respective |
---|
92 | applicant types are saved to disk. |
---|
93 | |
---|
94 | If `archive` is ``False`` none of the archive-handling is done |
---|
95 | and respective applicants are simply removed from the |
---|
96 | database. |
---|
97 | """ |
---|
98 | raise NotImplementedError() |
---|
99 | |
---|
100 | def addApplicant(self, applicant): |
---|
101 | """Add an applicant. |
---|
102 | """ |
---|
103 | if not self.iface.providedBy(applicant): |
---|
104 | raise TypeError( |
---|
105 | '%s containers contain only %s instances' %( |
---|
106 | self.container_title, self.iface)) |
---|
107 | applicant_id = generate_applicant_id(container=self) |
---|
108 | applicant.applicant_id = applicant_id |
---|
109 | self[applicant.application_number] = applicant |
---|
110 | return |
---|
111 | |
---|
112 | ApplicantsContainer = attrs_to_fields(ApplicantsContainer) |
---|
113 | |
---|
114 | class ApplicantsContainerProvider(grok.GlobalUtility): |
---|
115 | """A utility that provides basic applicants containers. |
---|
116 | """ |
---|
117 | grok.implements(IApplicantsContainerProvider) |
---|
118 | grok.name('waeup.kofa.applicants.ApplicantsContainer') |
---|
119 | |
---|
120 | #: The applicants container type this provider provides: |
---|
121 | #: :class:`ApplicantsContainer`. |
---|
122 | factory = ApplicantsContainer |
---|
123 | |
---|
124 | factory = Factory(lambda : ApplicantsContainer, 'ApplicantsContainer') |
---|
125 | grok.global_utility(factory, IFactory, name='waeup.ApplicantsContainer') |
---|