1 | ## $Id: root.py 13226 2015-08-24 19:53:21Z 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 | The root for applicants. |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from hurry.query import Eq |
---|
23 | from hurry.query.interfaces import IQuery |
---|
24 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
25 | from zope.component import getUtility |
---|
26 | from zope.component.interfaces import ComponentLookupError |
---|
27 | from zope.catalog.interfaces import ICatalog |
---|
28 | from zope.catalog.field import FieldIndex |
---|
29 | from zope.schema import getFields |
---|
30 | from waeup.kofa.interfaces import IKofaPluggable |
---|
31 | from waeup.kofa.applicants.interfaces import IApplicantsRoot |
---|
32 | from waeup.kofa.utils.logger import Logger |
---|
33 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
34 | |
---|
35 | class ApplicantsRoot(grok.Container, Logger): |
---|
36 | """The root of applicants-related components. It contains only |
---|
37 | containers for applicants. |
---|
38 | """ |
---|
39 | grok.implements(IApplicantsRoot) |
---|
40 | |
---|
41 | local_roles = [] |
---|
42 | description_dict = {} |
---|
43 | logger_name = 'waeup.kofa.${sitename}.applicants' |
---|
44 | logger_filename = 'applicants.log' |
---|
45 | |
---|
46 | ApplicantsRoot = attrs_to_fields(ApplicantsRoot) |
---|
47 | |
---|
48 | class ApplicantsPlugin(grok.GlobalUtility): |
---|
49 | """A KofaPlugin that creates an applicants root in portal. |
---|
50 | |
---|
51 | This plugin could be called by a typical |
---|
52 | `waeup.kofa.app.Universtiy` instance on creation time. The |
---|
53 | :meth:`update` method normally can also be triggered manually over |
---|
54 | the main site configuration. |
---|
55 | |
---|
56 | Implements :class:`waeup.kofa.interfaces.IKofaPluggable` |
---|
57 | """ |
---|
58 | grok.name('applicants') |
---|
59 | grok.implements(IKofaPluggable) |
---|
60 | log_prefix = 'ApplicantsPlugin' |
---|
61 | |
---|
62 | def setup(self, site, name, logger): |
---|
63 | """Create a new :class:`ApplicantsRoot` instance in `site`. |
---|
64 | """ |
---|
65 | site['applicants'] = ApplicantsRoot() |
---|
66 | logger.info( |
---|
67 | '%s: Installed applicants root.' % (self.log_prefix,) |
---|
68 | ) |
---|
69 | return |
---|
70 | |
---|
71 | def update(self, site, name, logger): |
---|
72 | """Update site wide ``applicants`` root. |
---|
73 | |
---|
74 | If the site already contains a suitable ``applicants`` root, |
---|
75 | leave it that way. If not create one and delete the old one if |
---|
76 | appropriate. |
---|
77 | |
---|
78 | Update applicants catalog. |
---|
79 | |
---|
80 | Remove deprecated applicant ids from global role map. |
---|
81 | """ |
---|
82 | app_folder = site.get('applicants', None) |
---|
83 | site_name = getattr(site, '__name__', '<Unnamed Site>') |
---|
84 | if IApplicantsRoot.providedBy(app_folder): |
---|
85 | items = getFields(IApplicantsRoot).items() |
---|
86 | nothing_to_do = True |
---|
87 | #for i in items: |
---|
88 | # if not hasattr(app_folder,i[0]): |
---|
89 | # nothing_to_do = False |
---|
90 | # setattr(app_folder,i[0],i[1].missing_value) |
---|
91 | # logger.info( |
---|
92 | # '%s: %s added to root.' % (self.log_prefix,i[0])) |
---|
93 | # can be removed after upgrading futminna |
---|
94 | #if not hasattr(app_folder, 'description_dict'): |
---|
95 | # nothing_to_do = False |
---|
96 | # setattr(app_folder,'description_dict',{}) |
---|
97 | # logger.info( |
---|
98 | # '%s: description_dict added to root.' % self.log_prefix) |
---|
99 | try: |
---|
100 | cat = getUtility(ICatalog, name='applicants_catalog') |
---|
101 | if 'container_code' not in cat.keys(): |
---|
102 | nothing_to_do = False |
---|
103 | cat[u'container_code'] = FieldIndex(field_name=u'container_code') |
---|
104 | cat.updateIndexes() |
---|
105 | logger.info( |
---|
106 | '%s: container_code index added to applicants_catalog.' |
---|
107 | % self.log_prefix) |
---|
108 | except ComponentLookupError: # in unit tests |
---|
109 | pass |
---|
110 | |
---|
111 | try: |
---|
112 | removed_applicant_ids = [] |
---|
113 | cat = getUtility(ICatalog, name='applicants_catalog') |
---|
114 | role_manager = IPrincipalRoleManager(grok.getSite()) |
---|
115 | principals = role_manager.getPrincipalsForRole('waeup.Applicant') |
---|
116 | for principal in principals: |
---|
117 | applicant_id = principal[0] |
---|
118 | results = cat.searchResults(applicant_id=(applicant_id, applicant_id)) |
---|
119 | if len(results): |
---|
120 | continue |
---|
121 | # The applicant does no longer exist. |
---|
122 | nothing_to_do = False |
---|
123 | role_manager.unsetRoleForPrincipal( |
---|
124 | 'waeup.Applicant', applicant_id) |
---|
125 | removed_applicant_ids.append(applicant_id) |
---|
126 | if len(removed_applicant_ids): |
---|
127 | logger.info( |
---|
128 | '%s: %s removed from global role map.' |
---|
129 | % (self.log_prefix, removed_applicant_ids)) |
---|
130 | except ComponentLookupError: # in unit tests |
---|
131 | pass |
---|
132 | |
---|
133 | if nothing_to_do: |
---|
134 | logger.info( |
---|
135 | '%s: Updating site at %s: Nothing to do.' % ( |
---|
136 | self.log_prefix, site_name,) |
---|
137 | ) |
---|
138 | return |
---|
139 | elif app_folder is not None: |
---|
140 | # Applicants need update. Remove old instance. |
---|
141 | logger.warn( |
---|
142 | '%s: Outdated applicants folder detected at site %s.' |
---|
143 | 'Removing it.' % (self.log_prefix, site_name) |
---|
144 | ) |
---|
145 | del site['applicants'] |
---|
146 | # Add new applicants. |
---|
147 | logger.info( |
---|
148 | '%s: Updating site at %s. Installing ' |
---|
149 | 'applicants.' % (self.log_prefix, site_name,) |
---|
150 | ) |
---|
151 | self.setup(site, name, logger) |
---|
152 | return |
---|