source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/root.py @ 6575

Last change on this file since 6575 was 6500, checked in by Henrik Bettermann, 14 years ago

Proposal for structuring the API.

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