##
## util.py
## Login : <uli@pu.smp.net>
## Started on  Thu Jul 29 09:39:51 2010 Uli Fouquet
## $Id$
## 
## Copyright (C) 2010 Uli Fouquet
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""Little helpers for applicant specific stuff.
"""
import grok
from waeup.sirp.accesscodes import get_access_code

def get_applicant_data(reg_no, ac):
    """Validate credentials and return applicant data.
    
    Returns tuple ``(<APPLICANT_ENTRY>, <ACCESSCODE>) on
    successful validation and ``None`` else.

    We expect a JAMB registration number and an access code in format
    like ``PREFIX-XXX-YYYYYYYY`` where ``PREFIX`` is something like
    ``APP`` or ``PUDE``, ``XXX`` the access code series and
    ``YYYYYYYYYY`` the real accesscode number.
    """
    site = grok.getSite()
    if reg_no is not None:
        applicant_data = site['applications'].get(reg_no, None)
    else:
        # Non-JAMB-screened applicants are stored by ac as key...
        applicant_data = site['applications'].get(ac, None)
    access_code = get_access_code(ac)
    return (applicant_data, access_code)

def application_exists(identifier):
    """Check whether an application for the given identifier already
       exists.

       `identifier` can be an access code or a JAMB registration
       number. For JAMB screened applicants we use the JAMB
       registration number as key, for other applicants we use the
       access code.
    """
    site = grok.getSite()
    if identifier in site['applications'].keys():
        return True
    return False
