1 | ## $Id: container.py 7260 2011-12-04 07:56:39Z 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.sirp.applicants.interfaces import ( |
---|
26 | IApplicantsContainer, IApplicantsContainerAdd, |
---|
27 | IApplicantsContainerProvider, IApplicant |
---|
28 | ) |
---|
29 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
30 | |
---|
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 | |
---|
41 | class ApplicantsContainer(grok.Container): |
---|
42 | """An applicants container contains university applicants. |
---|
43 | """ |
---|
44 | grok.implements(IApplicantsContainer,IApplicantsContainerAdd) |
---|
45 | |
---|
46 | #: These are 'class-attributes'. Do not fiddle around with it in |
---|
47 | #: instances of this class. |
---|
48 | #: |
---|
49 | #: The title of this container as displayed in add-forms. It should |
---|
50 | #: give some idea about what kind of container this is (one or two words) |
---|
51 | container_title = u'Basic' |
---|
52 | |
---|
53 | #: Another 'class-attribute'. Do not fiddle around with it in instances |
---|
54 | #: of this class. |
---|
55 | #: |
---|
56 | #: The `container_description` gives a short explanation about for what |
---|
57 | #: purposes this container type serves (a few words only). |
---|
58 | container_description = u'handles basic applicants' |
---|
59 | |
---|
60 | @property |
---|
61 | def local_roles(cls): |
---|
62 | return [] |
---|
63 | |
---|
64 | def archive(self, app_ids=None): |
---|
65 | """Create on-dist archive of applicants stored in this term. |
---|
66 | |
---|
67 | If app_ids is `None`, all applicants are archived. |
---|
68 | |
---|
69 | If app_ids contains a single id string, only the respective |
---|
70 | applicants are archived. |
---|
71 | |
---|
72 | If app_ids contains a list of id strings all of the respective |
---|
73 | applicants types are saved to disk. |
---|
74 | """ |
---|
75 | raise NotImplementedError() |
---|
76 | |
---|
77 | def clear(self, app_ids=None, archive=True): |
---|
78 | """Remove applicants of type given by 'id'. |
---|
79 | |
---|
80 | Optionally archive the applicants. |
---|
81 | |
---|
82 | If id is `None`, all applicants are archived. |
---|
83 | |
---|
84 | If id contains a single id string, only the respective |
---|
85 | applicants are archived. |
---|
86 | |
---|
87 | If id contains a list of id strings all of the respective |
---|
88 | applicant types are saved to disk. |
---|
89 | |
---|
90 | If `archive` is ``False`` none of the archive-handling is done |
---|
91 | and respective applicants are simply removed from the |
---|
92 | database. |
---|
93 | """ |
---|
94 | raise NotImplementedError() |
---|
95 | |
---|
96 | def addApplicant(self, applicant): |
---|
97 | """Add an applicant. |
---|
98 | """ |
---|
99 | if not IApplicant.providedBy(applicant): |
---|
100 | raise TypeError( |
---|
101 | 'ApplicantsContainers contain only IApplicant instances') |
---|
102 | applicant_id = generate_applicant_id(container=self) |
---|
103 | applicant.applicant_id = applicant_id |
---|
104 | self[applicant.application_number] = applicant |
---|
105 | return |
---|
106 | |
---|
107 | ApplicantsContainer = attrs_to_fields(ApplicantsContainer) |
---|
108 | |
---|
109 | class ApplicantsContainerProvider(grok.GlobalUtility): |
---|
110 | """A utility that provides basic applicants containers. |
---|
111 | """ |
---|
112 | grok.implements(IApplicantsContainerProvider) |
---|
113 | grok.name('waeup.sirp.applicants.ApplicantsContainer') |
---|
114 | |
---|
115 | #: The applicants container type this provider provides: |
---|
116 | #: :class:`ApplicantsContainer`. |
---|
117 | factory = ApplicantsContainer |
---|
118 | |
---|
119 | factory = Factory(lambda : ApplicantsContainer, 'ApplicantsContainer') |
---|
120 | grok.global_utility(factory, IFactory, name='waeup.ApplicantsContainer') |
---|