[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 | """ |
---|
[6348] | 25 | import logging |
---|
[5648] | 26 | import grok |
---|
[6348] | 27 | import os |
---|
[6121] | 28 | from hurry.query import Eq |
---|
| 29 | from hurry.query.interfaces import IQuery |
---|
| 30 | from zope.component import getUtility |
---|
[5670] | 31 | from waeup.sirp.interfaces import IWAeUPSIRPPluggable |
---|
[5804] | 32 | from waeup.sirp.accesscodes import get_access_code |
---|
[5676] | 33 | from waeup.sirp.applicants.interfaces import IApplicantsRoot |
---|
[5648] | 34 | |
---|
[5676] | 35 | class ApplicantsRoot(grok.Container): |
---|
[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 | |
---|
[6184] | 41 | @property |
---|
[6348] | 42 | def logger(self): |
---|
| 43 | """Get a logger for applicants. |
---|
| 44 | """ |
---|
| 45 | # We need a different logger for every site... |
---|
| 46 | site = grok.getSite() |
---|
| 47 | sitename = getattr(site, '__name__', 'app') |
---|
| 48 | loggername = 'waeup.sirp.%s.applicants' % sitename |
---|
| 49 | logger = logging.getLogger(loggername) |
---|
| 50 | if not logger.handlers: |
---|
| 51 | logger = self._setupLogger(logger) |
---|
| 52 | return logger |
---|
| 53 | |
---|
| 54 | def _setupLogger(self, logger): |
---|
| 55 | """Setup applicants logger. |
---|
| 56 | """ |
---|
| 57 | logdir = os.path.join(grok.getSite()['datacenter'].storage, 'logs') |
---|
| 58 | if not os.path.exists(logdir): |
---|
| 59 | os.mkdir(logdir) |
---|
| 60 | filename = os.path.join(logdir, 'applicants.log') |
---|
| 61 | |
---|
| 62 | # Create a rotating file handler logger for datacenter. |
---|
| 63 | handler = logging.handlers.RotatingFileHandler( |
---|
| 64 | filename, maxBytes=5*1024**1, backupCount=5) |
---|
| 65 | formatter = logging.Formatter( |
---|
| 66 | '%(asctime)s - %(levelname)s - %(message)s') |
---|
| 67 | handler.setFormatter(formatter) |
---|
| 68 | |
---|
| 69 | # Here we decide, whether our messages will _also_ go to |
---|
| 70 | # application log. |
---|
| 71 | logger.propagate = False |
---|
| 72 | logger.setLevel(logging.DEBUG) |
---|
| 73 | logger.addHandler(handler) |
---|
| 74 | return logger |
---|
| 75 | |
---|
| 76 | @property |
---|
[6184] | 77 | def local_roles(cls): |
---|
[6198] | 78 | return ['waeup.ApplicationsOfficer'] |
---|
[6184] | 79 | |
---|
[5676] | 80 | class ApplicantsPlugin(grok.GlobalUtility): |
---|
| 81 | """A WAeUPSIRPPlugin that creates an applicants root in portal. |
---|
[5670] | 82 | |
---|
| 83 | This plugin should be called by a typical |
---|
| 84 | `waeup.sirp.app.Universtiy` instance on creation time. The |
---|
| 85 | :meth:`update` method normally can also be triggered manually over |
---|
| 86 | the main site configuration. |
---|
| 87 | |
---|
| 88 | Implements :class:`waeup.sirp.interfaces.IWAeUPSIRPPluggable` |
---|
| 89 | """ |
---|
[5676] | 90 | grok.name('applicants') |
---|
[5670] | 91 | grok.implements(IWAeUPSIRPPluggable) |
---|
[5676] | 92 | log_prefix = 'ApplicantsPlugin' |
---|
[5670] | 93 | |
---|
| 94 | def setup(self, site, name, logger): |
---|
[5676] | 95 | """Create a new :class:`ApplicantsRoot` instance in `site`. |
---|
[5670] | 96 | """ |
---|
[5676] | 97 | site['applicants'] = ApplicantsRoot() |
---|
[5670] | 98 | logger.info( |
---|
[5676] | 99 | '%s: Installed applicants root.' % (self.log_prefix,) |
---|
[5670] | 100 | ) |
---|
| 101 | return |
---|
| 102 | |
---|
| 103 | def update(self, site, name, logger): |
---|
[5676] | 104 | """Update site wide ``applicants`` root. |
---|
[5670] | 105 | |
---|
[5676] | 106 | If the site already contains a suitable ``applicants`` root, |
---|
[5670] | 107 | leave it that way. If not create one and delete the old one if |
---|
| 108 | appropriate. |
---|
| 109 | """ |
---|
[5676] | 110 | app_folder = site.get('applicants', None) |
---|
[5684] | 111 | site_name = getattr(site, '__name__', '<Unnamed Site>') |
---|
[5676] | 112 | if IApplicantsRoot.providedBy(app_folder): |
---|
| 113 | # Applicants up to date. Return. |
---|
[5670] | 114 | logger.info( |
---|
| 115 | '%s: Updating site at %s: Nothing to do.' % ( |
---|
| 116 | self.log_prefix, site_name,) |
---|
| 117 | ) |
---|
| 118 | return |
---|
| 119 | elif app_folder is not None: |
---|
[5676] | 120 | # Applicants need update. Remove old instance. |
---|
[5670] | 121 | logger.warn( |
---|
[5676] | 122 | '%s: Outdated applicants folder detected at site %s.' |
---|
[5670] | 123 | 'Removing it.' % (self.log_prefix, site_name) |
---|
| 124 | ) |
---|
[5676] | 125 | del site['applicants'] |
---|
| 126 | # Add new applicants. |
---|
[5670] | 127 | logger.info( |
---|
| 128 | '%s: Updating site at %s. Installing ' |
---|
[5676] | 129 | 'applicants.' % (self.log_prefix, site_name,) |
---|
[5670] | 130 | ) |
---|
| 131 | self.setup(site, name, logger) |
---|
| 132 | return |
---|
[5804] | 133 | |
---|
[5815] | 134 | def get_applicant_data(identifier): |
---|
| 135 | """Get applicant data associated with `identifier`. |
---|
[5804] | 136 | |
---|
[5815] | 137 | Returns the applicant object if successful and ``None`` else. |
---|
| 138 | |
---|
| 139 | As `identifier` we expect an access code in format |
---|
[5804] | 140 | like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like |
---|
| 141 | ``APP`` or ``PUDE``, ``XXX`` the access code series and |
---|
| 142 | ``YYYYYYYYYY`` the real accesscode number. |
---|
[6121] | 143 | |
---|
| 144 | This function requires a fully blown setup as it does catalog |
---|
| 145 | lookups for finding applicants. |
---|
[5804] | 146 | """ |
---|
[6121] | 147 | query = getUtility(IQuery) |
---|
| 148 | results = list(query.searchResults( |
---|
| 149 | Eq(('applicants_catalog', 'access_code'), identifier) |
---|
| 150 | )) |
---|
| 151 | if len(results) == 0: |
---|
| 152 | return None |
---|
| 153 | return results[0] |
---|
[5804] | 154 | |
---|
| 155 | def application_exists(identifier): |
---|
| 156 | """Check whether an application for the given identifier already |
---|
| 157 | exists. |
---|
| 158 | |
---|
[5813] | 159 | `identifier` will normally be an access code. |
---|
[5804] | 160 | """ |
---|
[6119] | 161 | applicant = get_applicant_data(identifier) |
---|
| 162 | if applicant is None: |
---|
| 163 | return False |
---|
| 164 | return True |
---|