[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 | ## |
---|
| 7 | ## Copyright (C) 2011 Uli Fouquet |
---|
| 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 |
---|
[5670] | 26 | from waeup.sirp.interfaces import IWAeUPSIRPPluggable |
---|
[5804] | 27 | from waeup.sirp.accesscodes import get_access_code |
---|
[5676] | 28 | from waeup.sirp.applicants.interfaces import IApplicantsRoot |
---|
[5648] | 29 | |
---|
[5676] | 30 | class ApplicantsRoot(grok.Container): |
---|
| 31 | """The root of applicants-related components. It contains primarily |
---|
| 32 | containers for applicants. |
---|
[5648] | 33 | """ |
---|
[5676] | 34 | grok.implements(IApplicantsRoot) |
---|
[5670] | 35 | |
---|
[5676] | 36 | class ApplicantsPlugin(grok.GlobalUtility): |
---|
| 37 | """A WAeUPSIRPPlugin that creates an applicants root in portal. |
---|
[5670] | 38 | |
---|
| 39 | This plugin should be called by a typical |
---|
| 40 | `waeup.sirp.app.Universtiy` instance on creation time. The |
---|
| 41 | :meth:`update` method normally can also be triggered manually over |
---|
| 42 | the main site configuration. |
---|
| 43 | |
---|
| 44 | Implements :class:`waeup.sirp.interfaces.IWAeUPSIRPPluggable` |
---|
| 45 | """ |
---|
[5676] | 46 | grok.name('applicants') |
---|
[5670] | 47 | grok.implements(IWAeUPSIRPPluggable) |
---|
[5676] | 48 | log_prefix = 'ApplicantsPlugin' |
---|
[5670] | 49 | |
---|
| 50 | def setup(self, site, name, logger): |
---|
[5676] | 51 | """Create a new :class:`ApplicantsRoot` instance in `site`. |
---|
[5670] | 52 | """ |
---|
[5676] | 53 | site['applicants'] = ApplicantsRoot() |
---|
[5670] | 54 | logger.info( |
---|
[5676] | 55 | '%s: Installed applicants root.' % (self.log_prefix,) |
---|
[5670] | 56 | ) |
---|
| 57 | return |
---|
| 58 | |
---|
| 59 | def update(self, site, name, logger): |
---|
[5676] | 60 | """Update site wide ``applicants`` root. |
---|
[5670] | 61 | |
---|
[5676] | 62 | If the site already contains a suitable ``applicants`` root, |
---|
[5670] | 63 | leave it that way. If not create one and delete the old one if |
---|
| 64 | appropriate. |
---|
| 65 | """ |
---|
[5676] | 66 | app_folder = site.get('applicants', None) |
---|
[5684] | 67 | site_name = getattr(site, '__name__', '<Unnamed Site>') |
---|
[5676] | 68 | if IApplicantsRoot.providedBy(app_folder): |
---|
| 69 | # Applicants up to date. Return. |
---|
[5670] | 70 | logger.info( |
---|
| 71 | '%s: Updating site at %s: Nothing to do.' % ( |
---|
| 72 | self.log_prefix, site_name,) |
---|
| 73 | ) |
---|
| 74 | return |
---|
| 75 | elif app_folder is not None: |
---|
[5676] | 76 | # Applicants need update. Remove old instance. |
---|
[5670] | 77 | logger.warn( |
---|
[5676] | 78 | '%s: Outdated applicants folder detected at site %s.' |
---|
[5670] | 79 | 'Removing it.' % (self.log_prefix, site_name) |
---|
| 80 | ) |
---|
[5676] | 81 | del site['applicants'] |
---|
| 82 | # Add new applicants. |
---|
[5670] | 83 | logger.info( |
---|
| 84 | '%s: Updating site at %s. Installing ' |
---|
[5676] | 85 | 'applicants.' % (self.log_prefix, site_name,) |
---|
[5670] | 86 | ) |
---|
| 87 | self.setup(site, name, logger) |
---|
| 88 | return |
---|
[5804] | 89 | |
---|
[5815] | 90 | def get_applicant_data(identifier): |
---|
| 91 | """Get applicant data associated with `identifier`. |
---|
[5804] | 92 | |
---|
[5815] | 93 | Returns the applicant object if successful and ``None`` else. |
---|
| 94 | |
---|
| 95 | As `identifier` we expect an access code in format |
---|
[5804] | 96 | like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like |
---|
| 97 | ``APP`` or ``PUDE``, ``XXX`` the access code series and |
---|
| 98 | ``YYYYYYYYYY`` the real accesscode number. |
---|
| 99 | """ |
---|
| 100 | site = grok.getSite() |
---|
[5815] | 101 | for container in site['applicants'].values(): |
---|
| 102 | applicant_data = container.get(identifier, None) |
---|
| 103 | if applicant_data is not None: |
---|
| 104 | return applicant_data |
---|
| 105 | return None |
---|
[5804] | 106 | |
---|
| 107 | def application_exists(identifier): |
---|
| 108 | """Check whether an application for the given identifier already |
---|
| 109 | exists. |
---|
| 110 | |
---|
[5813] | 111 | `identifier` will normally be an access code. |
---|
[5804] | 112 | """ |
---|
| 113 | site = grok.getSite() |
---|
[5813] | 114 | for container in site['applicants'].values(): |
---|
| 115 | if identifier in container.keys(): |
---|
| 116 | return True |
---|
[5804] | 117 | return False |
---|