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

Last change on this file since 5850 was 5815, checked in by uli, 14 years ago

Fix get_applicant_data to reflect new structure of applicant
containers, applicants root and the like.

File size: 4.7 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
[5670]26from waeup.sirp.interfaces import IWAeUPSIRPPluggable
[5804]27from waeup.sirp.accesscodes import get_access_code
[5676]28from waeup.sirp.applicants.interfaces import IApplicantsRoot
[5648]29
[5676]30class ApplicantsRoot(grok.Container):
31    """The root of applicants-related components. It contains primarily
32    containers for applicants.
[5648]33    """
[5676]34    grok.implements(IApplicantsRoot)
[5670]35
[5676]36    def addApplicantsContainer(self, container, name=None):
37        """Add an applicants container.
[5648]38
[5676]39        Adds an applicants container that implements `interface`
[5648]40        under the name `name`.
41
[5670]42        `container`
43          the container instance to be added. Should
[5676]44          implement :class:`IApplicantsContainer`.
[5648]45
46        `name`
47          the name under which the container will be accessible. We
48          usually use names like ``pume_2011`` to indicate, that the
[5676]49          new container will contain student applicants for a
[5648]50          certain screening type (``pume``) and of the year 2011.
51
52        """
53        self[name] = container
54        return
[5670]55
[5676]56class ApplicantsPlugin(grok.GlobalUtility):
57    """A WAeUPSIRPPlugin that creates an applicants root in portal.
[5670]58
59    This plugin should be called by a typical
60    `waeup.sirp.app.Universtiy` instance on creation time. The
61    :meth:`update` method normally can also be triggered manually over
62    the main site configuration.
63
64    Implements :class:`waeup.sirp.interfaces.IWAeUPSIRPPluggable`
65    """
[5676]66    grok.name('applicants')
[5670]67    grok.implements(IWAeUPSIRPPluggable)
[5676]68    log_prefix = 'ApplicantsPlugin'
[5670]69
70    def setup(self, site, name, logger):
[5676]71        """Create a new :class:`ApplicantsRoot` instance in `site`.
[5670]72        """
[5676]73        site['applicants'] = ApplicantsRoot()
[5670]74        logger.info(
[5676]75            '%s: Installed applicants root.' % (self.log_prefix,)
[5670]76            )
77        return
78
79    def update(self, site, name, logger):
[5676]80        """Update site wide ``applicants`` root.
[5670]81
[5676]82        If the site already contains a suitable ``applicants`` root,
[5670]83        leave it that way. If not create one and delete the old one if
84        appropriate.
85        """
[5676]86        app_folder = site.get('applicants', None)
[5684]87        site_name = getattr(site, '__name__', '<Unnamed Site>')
[5676]88        if IApplicantsRoot.providedBy(app_folder):
89            # Applicants up to date. Return.
[5670]90            logger.info(
91                '%s: Updating site at %s: Nothing to do.' % (
92                    self.log_prefix, site_name,)
93                )
94            return
95        elif app_folder is not None:
[5676]96            # Applicants need update. Remove old instance.
[5670]97            logger.warn(
[5676]98                '%s: Outdated applicants folder detected at site %s.'
[5670]99                'Removing it.' % (self.log_prefix, site_name)
100                    )
[5676]101            del site['applicants']
102        # Add new applicants.
[5670]103        logger.info(
104            '%s: Updating site at %s. Installing '
[5676]105            'applicants.' % (self.log_prefix, site_name,)
[5670]106            )
107        self.setup(site, name, logger)
108        return
[5804]109
[5815]110def get_applicant_data(identifier):
111    """Get applicant data associated with `identifier`.
[5804]112
[5815]113    Returns the applicant object if successful and ``None`` else.
114
115    As `identifier` we expect an access code in format
[5804]116    like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like
117    ``APP`` or ``PUDE``, ``XXX`` the access code series and
118    ``YYYYYYYYYY`` the real accesscode number.
119    """
120    site = grok.getSite()
[5815]121    for container in site['applicants'].values():
122        applicant_data = container.get(identifier, None)
123        if applicant_data is not None:
124            return applicant_data
125    return None
[5804]126
127def application_exists(identifier):
128    """Check whether an application for the given identifier already
129       exists.
130
[5813]131       `identifier` will normally be an access code.
[5804]132    """
133    site = grok.getSite()
[5813]134    for container in site['applicants'].values():
135        if identifier in container.keys():
136            return True
[5804]137    return False
Note: See TracBrowser for help on using the repository browser.