[7192] | 1 | ## $Id: root.py 7321 2011-12-10 06:15:17Z 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 |
---|
| 24 | from zope.component import getUtility |
---|
[7321] | 25 | from waeup.sirp.interfaces import ISIRPPluggable |
---|
[5676] | 26 | from waeup.sirp.applicants.interfaces import IApplicantsRoot |
---|
[6475] | 27 | from waeup.sirp.utils.helpers import get_current_principal |
---|
[6578] | 28 | from waeup.sirp.utils.logger import Logger |
---|
[5648] | 29 | |
---|
[6578] | 30 | class ApplicantsRoot(grok.Container, Logger): |
---|
[6119] | 31 | """The root of applicants-related components. It contains only |
---|
[5676] | 32 | containers for applicants. |
---|
[5648] | 33 | """ |
---|
[5676] | 34 | grok.implements(IApplicantsRoot) |
---|
[5670] | 35 | |
---|
[7182] | 36 | local_roles = [] |
---|
[6370] | 37 | |
---|
[6578] | 38 | logger_name = 'waeup.sirp.${sitename}.applicants' |
---|
| 39 | logger_filename = 'applicants.log' |
---|
[6348] | 40 | |
---|
[6475] | 41 | def logger_info(self, target, ob_class, comment=None): |
---|
| 42 | """Get the logger's info method. |
---|
| 43 | """ |
---|
| 44 | user = get_current_principal() |
---|
[7063] | 45 | user = getattr(user, 'id', 'system') |
---|
[6582] | 46 | self.logger.info('%s - %s - %s - %s' % ( |
---|
[6481] | 47 | user, target, ob_class, comment)) |
---|
[6500] | 48 | return |
---|
[6475] | 49 | |
---|
[5676] | 50 | class ApplicantsPlugin(grok.GlobalUtility): |
---|
[7321] | 51 | """A SIRPPlugin that creates an applicants root in portal. |
---|
[5670] | 52 | |
---|
| 53 | This plugin should be called by a typical |
---|
| 54 | `waeup.sirp.app.Universtiy` instance on creation time. The |
---|
| 55 | :meth:`update` method normally can also be triggered manually over |
---|
| 56 | the main site configuration. |
---|
| 57 | |
---|
[7321] | 58 | Implements :class:`waeup.sirp.interfaces.ISIRPPluggable` |
---|
[5670] | 59 | """ |
---|
[5676] | 60 | grok.name('applicants') |
---|
[7321] | 61 | grok.implements(ISIRPPluggable) |
---|
[5676] | 62 | log_prefix = 'ApplicantsPlugin' |
---|
[5670] | 63 | |
---|
| 64 | def setup(self, site, name, logger): |
---|
[5676] | 65 | """Create a new :class:`ApplicantsRoot` instance in `site`. |
---|
[5670] | 66 | """ |
---|
[5676] | 67 | site['applicants'] = ApplicantsRoot() |
---|
[5670] | 68 | logger.info( |
---|
[5676] | 69 | '%s: Installed applicants root.' % (self.log_prefix,) |
---|
[5670] | 70 | ) |
---|
| 71 | return |
---|
| 72 | |
---|
| 73 | def update(self, site, name, logger): |
---|
[5676] | 74 | """Update site wide ``applicants`` root. |
---|
[5670] | 75 | |
---|
[5676] | 76 | If the site already contains a suitable ``applicants`` root, |
---|
[5670] | 77 | leave it that way. If not create one and delete the old one if |
---|
| 78 | appropriate. |
---|
| 79 | """ |
---|
[5676] | 80 | app_folder = site.get('applicants', None) |
---|
[5684] | 81 | site_name = getattr(site, '__name__', '<Unnamed Site>') |
---|
[5676] | 82 | if IApplicantsRoot.providedBy(app_folder): |
---|
| 83 | # Applicants up to date. Return. |
---|
[5670] | 84 | logger.info( |
---|
| 85 | '%s: Updating site at %s: Nothing to do.' % ( |
---|
| 86 | self.log_prefix, site_name,) |
---|
| 87 | ) |
---|
| 88 | return |
---|
| 89 | elif app_folder is not None: |
---|
[5676] | 90 | # Applicants need update. Remove old instance. |
---|
[5670] | 91 | logger.warn( |
---|
[5676] | 92 | '%s: Outdated applicants folder detected at site %s.' |
---|
[5670] | 93 | 'Removing it.' % (self.log_prefix, site_name) |
---|
| 94 | ) |
---|
[5676] | 95 | del site['applicants'] |
---|
| 96 | # Add new applicants. |
---|
[5670] | 97 | logger.info( |
---|
| 98 | '%s: Updating site at %s. Installing ' |
---|
[5676] | 99 | 'applicants.' % (self.log_prefix, site_name,) |
---|
[5670] | 100 | ) |
---|
| 101 | self.setup(site, name, logger) |
---|
| 102 | return |
---|
[5804] | 103 | |
---|
[5815] | 104 | def get_applicant_data(identifier): |
---|
| 105 | """Get applicant data associated with `identifier`. |
---|
[5804] | 106 | |
---|
[5815] | 107 | Returns the applicant object if successful and ``None`` else. |
---|
| 108 | |
---|
| 109 | As `identifier` we expect an access code in format |
---|
[5804] | 110 | like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like |
---|
| 111 | ``APP`` or ``PUDE``, ``XXX`` the access code series and |
---|
| 112 | ``YYYYYYYYYY`` the real accesscode number. |
---|
[6121] | 113 | |
---|
| 114 | This function requires a fully blown setup as it does catalog |
---|
| 115 | lookups for finding applicants. |
---|
[5804] | 116 | """ |
---|
[6121] | 117 | query = getUtility(IQuery) |
---|
| 118 | results = list(query.searchResults( |
---|
| 119 | Eq(('applicants_catalog', 'access_code'), identifier) |
---|
| 120 | )) |
---|
| 121 | if len(results) == 0: |
---|
| 122 | return None |
---|
| 123 | return results[0] |
---|
[5804] | 124 | |
---|
| 125 | def application_exists(identifier): |
---|
| 126 | """Check whether an application for the given identifier already |
---|
| 127 | exists. |
---|
| 128 | |
---|
[5813] | 129 | `identifier` will normally be an access code. |
---|
[5804] | 130 | """ |
---|
[6119] | 131 | applicant = get_applicant_data(identifier) |
---|
| 132 | if applicant is None: |
---|
| 133 | return False |
---|
| 134 | return True |
---|