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

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

Save detailed logging messages (experimental).

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