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