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

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

Move (non-JAMB-specific) functions from jambtables.util to applicants.root module.

File size: 5.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 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(reg_no, ac):
111    """Validate credentials and return applicant data.
112   
113    Returns tuple ``(<APPLICANT_ENTRY>, <ACCESSCODE>) on
114    successful validation and ``None`` else.
115
116    We expect a JAMB registration number and an access code in format
117    like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like
118    ``APP`` or ``PUDE``, ``XXX`` the access code series and
119    ``YYYYYYYYYY`` the real accesscode number.
120    """
121    site = grok.getSite()
122    if reg_no is not None:
123        applicant_data = site['applications'].get(reg_no, None)
124    else:
125        # Non-JAMB-screened applicants are stored by ac as key...
126        applicant_data = site['applications'].get(ac, None)
127    access_code = get_access_code(ac)
128    return (applicant_data, access_code)
129
130def application_exists(identifier):
131    """Check whether an application for the given identifier already
132       exists.
133
134       `identifier` can be an access code or a JAMB registration
135       number. For JAMB screened applicants we use the JAMB
136       registration number as key, for other applicants we use the
137       access code.
138    """
139    site = grok.getSite()
140    if identifier in site['applications'].keys():
141        return True
142    return False
Note: See TracBrowser for help on using the repository browser.