[5648] | 1 | ## |
---|
| 2 | ## root.py |
---|
| 3 | ## Login : <uli@pu.smp.net> |
---|
| 4 | ## Started on Thu Jan 20 04:17:59 2011 Uli Fouquet |
---|
| 5 | ## $Id$ |
---|
| 6 | ## |
---|
[6478] | 7 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5648] | 8 | ## This program is free software; you can redistribute it and/or modify |
---|
| 9 | ## it under the terms of the GNU General Public License as published by |
---|
| 10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | ## (at your option) any later version. |
---|
| 12 | ## |
---|
| 13 | ## This program is distributed in the hope that it will be useful, |
---|
| 14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | ## GNU General Public License for more details. |
---|
| 17 | ## |
---|
| 18 | ## You should have received a copy of the GNU General Public License |
---|
| 19 | ## along with this program; if not, write to the Free Software |
---|
| 20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 21 | ## |
---|
| 22 | """ |
---|
[5676] | 23 | The root for applicants. |
---|
[5648] | 24 | """ |
---|
| 25 | import grok |
---|
[6121] | 26 | from hurry.query import Eq |
---|
| 27 | from hurry.query.interfaces import IQuery |
---|
| 28 | from zope.component import getUtility |
---|
[6578] | 29 | from waeup.sirp.interfaces import IWAeUPSIRPPluggable |
---|
[5676] | 30 | from waeup.sirp.applicants.interfaces import IApplicantsRoot |
---|
[6475] | 31 | from waeup.sirp.utils.helpers import get_current_principal |
---|
[6578] | 32 | from waeup.sirp.utils.logger import Logger |
---|
[5648] | 33 | |
---|
[6578] | 34 | class ApplicantsRoot(grok.Container, Logger): |
---|
[6119] | 35 | """The root of applicants-related components. It contains only |
---|
[5676] | 36 | containers for applicants. |
---|
[5648] | 37 | """ |
---|
[5676] | 38 | grok.implements(IApplicantsRoot) |
---|
[5670] | 39 | |
---|
[6370] | 40 | local_roles = ['waeup.ApplicationsOfficer'] |
---|
| 41 | |
---|
[6578] | 42 | logger_name = 'waeup.sirp.${sitename}.applicants' |
---|
| 43 | logger_filename = 'applicants.log' |
---|
[6348] | 44 | |
---|
[6475] | 45 | def logger_info(self, target, ob_class, comment=None): |
---|
| 46 | """Get the logger's info method. |
---|
| 47 | """ |
---|
| 48 | user = get_current_principal() |
---|
| 49 | if user is None: |
---|
| 50 | user = 'system' |
---|
| 51 | elif user.title == 'Applicant': |
---|
| 52 | user = 'applicant' |
---|
| 53 | else: |
---|
| 54 | user = user.id |
---|
[6582] | 55 | self.logger.info('%s - %s - %s - %s' % ( |
---|
[6481] | 56 | user, target, ob_class, comment)) |
---|
[6500] | 57 | return |
---|
[6475] | 58 | |
---|
[5676] | 59 | class ApplicantsPlugin(grok.GlobalUtility): |
---|
| 60 | """A WAeUPSIRPPlugin that creates an applicants root in portal. |
---|
[5670] | 61 | |
---|
| 62 | This plugin should be called by a typical |
---|
| 63 | `waeup.sirp.app.Universtiy` instance on creation time. The |
---|
| 64 | :meth:`update` method normally can also be triggered manually over |
---|
| 65 | the main site configuration. |
---|
| 66 | |
---|
| 67 | Implements :class:`waeup.sirp.interfaces.IWAeUPSIRPPluggable` |
---|
| 68 | """ |
---|
[5676] | 69 | grok.name('applicants') |
---|
[5670] | 70 | grok.implements(IWAeUPSIRPPluggable) |
---|
[5676] | 71 | log_prefix = 'ApplicantsPlugin' |
---|
[5670] | 72 | |
---|
| 73 | def setup(self, site, name, logger): |
---|
[5676] | 74 | """Create a new :class:`ApplicantsRoot` instance in `site`. |
---|
[5670] | 75 | """ |
---|
[5676] | 76 | site['applicants'] = ApplicantsRoot() |
---|
[5670] | 77 | logger.info( |
---|
[5676] | 78 | '%s: Installed applicants root.' % (self.log_prefix,) |
---|
[5670] | 79 | ) |
---|
| 80 | return |
---|
| 81 | |
---|
| 82 | def update(self, site, name, logger): |
---|
[5676] | 83 | """Update site wide ``applicants`` root. |
---|
[5670] | 84 | |
---|
[5676] | 85 | If the site already contains a suitable ``applicants`` root, |
---|
[5670] | 86 | leave it that way. If not create one and delete the old one if |
---|
| 87 | appropriate. |
---|
| 88 | """ |
---|
[5676] | 89 | app_folder = site.get('applicants', None) |
---|
[5684] | 90 | site_name = getattr(site, '__name__', '<Unnamed Site>') |
---|
[5676] | 91 | if IApplicantsRoot.providedBy(app_folder): |
---|
| 92 | # Applicants up to date. Return. |
---|
[5670] | 93 | logger.info( |
---|
| 94 | '%s: Updating site at %s: Nothing to do.' % ( |
---|
| 95 | self.log_prefix, site_name,) |
---|
| 96 | ) |
---|
| 97 | return |
---|
| 98 | elif app_folder is not None: |
---|
[5676] | 99 | # Applicants need update. Remove old instance. |
---|
[5670] | 100 | logger.warn( |
---|
[5676] | 101 | '%s: Outdated applicants folder detected at site %s.' |
---|
[5670] | 102 | 'Removing it.' % (self.log_prefix, site_name) |
---|
| 103 | ) |
---|
[5676] | 104 | del site['applicants'] |
---|
| 105 | # Add new applicants. |
---|
[5670] | 106 | logger.info( |
---|
| 107 | '%s: Updating site at %s. Installing ' |
---|
[5676] | 108 | 'applicants.' % (self.log_prefix, site_name,) |
---|
[5670] | 109 | ) |
---|
| 110 | self.setup(site, name, logger) |
---|
| 111 | return |
---|
[5804] | 112 | |
---|
[5815] | 113 | def get_applicant_data(identifier): |
---|
| 114 | """Get applicant data associated with `identifier`. |
---|
[5804] | 115 | |
---|
[5815] | 116 | Returns the applicant object if successful and ``None`` else. |
---|
| 117 | |
---|
| 118 | As `identifier` we expect an access code in format |
---|
[5804] | 119 | like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like |
---|
| 120 | ``APP`` or ``PUDE``, ``XXX`` the access code series and |
---|
| 121 | ``YYYYYYYYYY`` the real accesscode number. |
---|
[6121] | 122 | |
---|
| 123 | This function requires a fully blown setup as it does catalog |
---|
| 124 | lookups for finding applicants. |
---|
[5804] | 125 | """ |
---|
[6121] | 126 | query = getUtility(IQuery) |
---|
| 127 | results = list(query.searchResults( |
---|
| 128 | Eq(('applicants_catalog', 'access_code'), identifier) |
---|
| 129 | )) |
---|
| 130 | if len(results) == 0: |
---|
| 131 | return None |
---|
| 132 | return results[0] |
---|
[5804] | 133 | |
---|
| 134 | def application_exists(identifier): |
---|
| 135 | """Check whether an application for the given identifier already |
---|
| 136 | exists. |
---|
| 137 | |
---|
[5813] | 138 | `identifier` will normally be an access code. |
---|
[5804] | 139 | """ |
---|
[6119] | 140 | applicant = get_applicant_data(identifier) |
---|
| 141 | if applicant is None: |
---|
| 142 | return False |
---|
| 143 | return True |
---|