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

Last change on this file since 6119 was 6119, checked in by uli, 13 years ago

Shorten application_exists function and make it more reliable. If we
change the applicant lookup method in get_applicant_data() we now
automatically have that reflect in applicant_exists.

File size: 4.0 KB
Line 
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"""
23The root for applicants.
24"""
25import grok
26from waeup.sirp.interfaces import IWAeUPSIRPPluggable
27from waeup.sirp.accesscodes import get_access_code
28from waeup.sirp.applicants.interfaces import IApplicantsRoot
29
30class ApplicantsRoot(grok.Container):
31    """The root of applicants-related components. It contains only
32    containers for applicants.
33    """
34    grok.implements(IApplicantsRoot)
35
36class ApplicantsPlugin(grok.GlobalUtility):
37    """A WAeUPSIRPPlugin that creates an applicants root in portal.
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    """
46    grok.name('applicants')
47    grok.implements(IWAeUPSIRPPluggable)
48    log_prefix = 'ApplicantsPlugin'
49
50    def setup(self, site, name, logger):
51        """Create a new :class:`ApplicantsRoot` instance in `site`.
52        """
53        site['applicants'] = ApplicantsRoot()
54        logger.info(
55            '%s: Installed applicants root.' % (self.log_prefix,)
56            )
57        return
58
59    def update(self, site, name, logger):
60        """Update site wide ``applicants`` root.
61
62        If the site already contains a suitable ``applicants`` root,
63        leave it that way. If not create one and delete the old one if
64        appropriate.
65        """
66        app_folder = site.get('applicants', None)
67        site_name = getattr(site, '__name__', '<Unnamed Site>')
68        if IApplicantsRoot.providedBy(app_folder):
69            # Applicants up to date. Return.
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:
76            # Applicants need update. Remove old instance.
77            logger.warn(
78                '%s: Outdated applicants folder detected at site %s.'
79                'Removing it.' % (self.log_prefix, site_name)
80                    )
81            del site['applicants']
82        # Add new applicants.
83        logger.info(
84            '%s: Updating site at %s. Installing '
85            'applicants.' % (self.log_prefix, site_name,)
86            )
87        self.setup(site, name, logger)
88        return
89
90def get_applicant_data(identifier):
91    """Get applicant data associated with `identifier`.
92
93    Returns the applicant object if successful and ``None`` else.
94
95    As `identifier` we expect an access code in format
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()
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
106
107def application_exists(identifier):
108    """Check whether an application for the given identifier already
109       exists.
110
111       `identifier` will normally be an access code.
112    """
113    applicant = get_applicant_data(identifier)
114    if applicant is None:
115        return False
116    return True
Note: See TracBrowser for help on using the repository browser.