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

Last change on this file since 6123 was 6121, checked in by uli, 14 years ago

Use catalogs when looking up applicants.

File size: 4.2 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##
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]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
[5670]29from waeup.sirp.interfaces import IWAeUPSIRPPluggable
[5804]30from waeup.sirp.accesscodes import get_access_code
[5676]31from waeup.sirp.applicants.interfaces import IApplicantsRoot
[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
[5676]39class ApplicantsPlugin(grok.GlobalUtility):
40    """A WAeUPSIRPPlugin that creates an applicants root in portal.
[5670]41
42    This plugin should be called by a typical
43    `waeup.sirp.app.Universtiy` instance on creation time. The
44    :meth:`update` method normally can also be triggered manually over
45    the main site configuration.
46
47    Implements :class:`waeup.sirp.interfaces.IWAeUPSIRPPluggable`
48    """
[5676]49    grok.name('applicants')
[5670]50    grok.implements(IWAeUPSIRPPluggable)
[5676]51    log_prefix = 'ApplicantsPlugin'
[5670]52
53    def setup(self, site, name, logger):
[5676]54        """Create a new :class:`ApplicantsRoot` instance in `site`.
[5670]55        """
[5676]56        site['applicants'] = ApplicantsRoot()
[5670]57        logger.info(
[5676]58            '%s: Installed applicants root.' % (self.log_prefix,)
[5670]59            )
60        return
61
62    def update(self, site, name, logger):
[5676]63        """Update site wide ``applicants`` root.
[5670]64
[5676]65        If the site already contains a suitable ``applicants`` root,
[5670]66        leave it that way. If not create one and delete the old one if
67        appropriate.
68        """
[5676]69        app_folder = site.get('applicants', None)
[5684]70        site_name = getattr(site, '__name__', '<Unnamed Site>')
[5676]71        if IApplicantsRoot.providedBy(app_folder):
72            # Applicants up to date. Return.
[5670]73            logger.info(
74                '%s: Updating site at %s: Nothing to do.' % (
75                    self.log_prefix, site_name,)
76                )
77            return
78        elif app_folder is not None:
[5676]79            # Applicants need update. Remove old instance.
[5670]80            logger.warn(
[5676]81                '%s: Outdated applicants folder detected at site %s.'
[5670]82                'Removing it.' % (self.log_prefix, site_name)
83                    )
[5676]84            del site['applicants']
85        # Add new applicants.
[5670]86        logger.info(
87            '%s: Updating site at %s. Installing '
[5676]88            'applicants.' % (self.log_prefix, site_name,)
[5670]89            )
90        self.setup(site, name, logger)
91        return
[5804]92
[5815]93def get_applicant_data(identifier):
94    """Get applicant data associated with `identifier`.
[5804]95
[5815]96    Returns the applicant object if successful and ``None`` else.
97
98    As `identifier` we expect an access code in format
[5804]99    like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like
100    ``APP`` or ``PUDE``, ``XXX`` the access code series and
101    ``YYYYYYYYYY`` the real accesscode number.
[6121]102
103    This function requires a fully blown setup as it does catalog
104    lookups for finding applicants.
[5804]105    """
[6121]106    query = getUtility(IQuery)
107    results = list(query.searchResults(
108            Eq(('applicants_catalog', 'access_code'), identifier)
109            ))
110    if len(results) == 0:
111        return None
112    return results[0]
[5804]113
114def application_exists(identifier):
115    """Check whether an application for the given identifier already
116       exists.
117
[5813]118       `identifier` will normally be an access code.
[5804]119    """
[6119]120    applicant = get_applicant_data(identifier)
121    if applicant is None:
122        return False
123    return True
Note: See TracBrowser for help on using the repository browser.