source: main/waeup.sirp/trunk/src/waeup/sirp/jambtables/util.py @ 5446

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

Add helper to find an accesscode entry.

File size: 2.3 KB
Line 
1##
2## util.py
3## Login : <uli@pu.smp.net>
4## Started on  Thu Jul 29 09:39:51 2010 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2010 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"""Little helpers for applicant specific stuff.
23"""
24import grok
25
26def get_applicant_data(reg_no, ac):
27    """Validate credentials and return applicant data.
28   
29    Returns tuple ``(<APPLICANT_ENTRY>, <ACCESSCODE>) on
30    successful validation and ``None`` else.
31
32    We expect a JAMB registration number and an access code in format
33    like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like
34    ``APP`` or ``PUDE``, ``XXX`` the access code series and
35    ``YYYYYYYYYY`` the real accesscode number.
36    """
37    site = grok.getSite()
38    if reg_no not in site['applications'].keys():
39        return None
40    applicant_data = site['applications'][reg_no]
41    entries = site['accesscodes'].search(ac, 'pin')
42    if len(entries) != 1:
43        # XXX: If entries > 1 then we have a problem!
44        return None
45    # XXX: Maybe we have to mark reg_no as used here?
46    return (applicant_data, entries[0])
47
48def get_access_code(access_code):
49    """Get an access code instance.
50
51    Returns ``None`` if the given code cannot be found.
52
53    XXX: This should be moved to `accesscodes` as public API part.
54    """
55    site = grok.getSite()
56    if not isinstance(access_code, basestring):
57        return None
58    try:
59        batch_id, ac_id = access_code.rsplit('-', 1)
60    except:
61        return None
62    if batch_id not in site['accesscodes'].keys():
63        return None
64    batch = site['accesscodes'][batch_id]
65    return batch.getAccessCode(access_code)
Note: See TracBrowser for help on using the repository browser.