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

Last change on this file since 5852 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
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 primarily
32    containers for applicants.
33    """
34    grok.implements(IApplicantsRoot)
35
36    def addApplicantsContainer(self, container, name=None):
37        """Add an applicants container.
38
39        Adds an applicants container that implements `interface`
40        under the name `name`.
41
42        `container`
43          the container instance to be added. Should
44          implement :class:`IApplicantsContainer`.
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
49          new container will contain student applicants for a
50          certain screening type (``pume``) and of the year 2011.
51
52        """
53        self[name] = container
54        return
55
56class ApplicantsPlugin(grok.GlobalUtility):
57    """A WAeUPSIRPPlugin that creates an applicants root in portal.
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    """
66    grok.name('applicants')
67    grok.implements(IWAeUPSIRPPluggable)
68    log_prefix = 'ApplicantsPlugin'
69
70    def setup(self, site, name, logger):
71        """Create a new :class:`ApplicantsRoot` instance in `site`.
72        """
73        site['applicants'] = ApplicantsRoot()
74        logger.info(
75            '%s: Installed applicants root.' % (self.log_prefix,)
76            )
77        return
78
79    def update(self, site, name, logger):
80        """Update site wide ``applicants`` root.
81
82        If the site already contains a suitable ``applicants`` root,
83        leave it that way. If not create one and delete the old one if
84        appropriate.
85        """
86        app_folder = site.get('applicants', None)
87        site_name = getattr(site, '__name__', '<Unnamed Site>')
88        if IApplicantsRoot.providedBy(app_folder):
89            # Applicants up to date. Return.
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:
96            # Applicants need update. Remove old instance.
97            logger.warn(
98                '%s: Outdated applicants folder detected at site %s.'
99                'Removing it.' % (self.log_prefix, site_name)
100                    )
101            del site['applicants']
102        # Add new applicants.
103        logger.info(
104            '%s: Updating site at %s. Installing '
105            'applicants.' % (self.log_prefix, site_name,)
106            )
107        self.setup(site, name, logger)
108        return
109
110def get_applicant_data(identifier):
111    """Get applicant data associated with `identifier`.
112
113    Returns the applicant object if successful and ``None`` else.
114
115    As `identifier` we expect an access code in format
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()
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
126
127def application_exists(identifier):
128    """Check whether an application for the given identifier already
129       exists.
130
131       `identifier` will normally be an access code.
132    """
133    site = grok.getSite()
134    for container in site['applicants'].values():
135        if identifier in container.keys():
136            return True
137    return False
Note: See TracBrowser for help on using the repository browser.