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

Last change on this file since 7159 was 7137, checked in by Henrik Bettermann, 13 years ago

Set value Id for property svn:keywords in all Python files.

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