##parameters=REQUEST
# $Id: retrieve_password.py 3367 2008-03-21 13:29:31Z henrik $
"""
request Clearance
"""
import logging
logger = logging.getLogger('Skins.retrieve_password')
validate = REQUEST.has_key("cpsdocument_create_button")

from urllib import urlencode

mhost = context.MailHost
prop = context.portal_properties
REQUEST = context.REQUEST

short_msg="""
From: %s <%s>
To: %s
Reply-To: %s
Subject: %s
Your SRP Member Details
-----------------------

Fullname: %s
Student Id: %s
Password: %s

"""

mtool = context.portal_membership
is_anon = mtool.isAnonymousUser()
member = mtool.getAuthenticatedMember()
lt = context.portal_layouts
#pr = context.portal_registration

res,psm,ds = lt.renderLayout('retrieve_password',
                             'retrieve_password',
                             context,
                             mapping=validate and REQUEST,
                             layout_mode='create',
                             ob={},
                             commit=False,
                             formaction = "retrieve_password",
                             button = "Send Notification Email",
                             )
if psm == 'invalid':
    reg_no = ds.get('reg_no') 
    try:
        email = ds.get('email')
        record = ds.get('record')    
        email_cat = record.email 
        if email:
            logger.info('%s: %s does not match %s' % (reg_no,email,email_cat))
        else:
            logger.info('%s: no email provided' % (reg_no))
    except:
        logger.info('wrong reg number %s' % (reg_no))
    return context.retrieve_password_form(rendered = res,
                                 psm = "Please correct your input!",
                                 #psm = "%s, %s" % (psm,ds),
                                 firstlayout = True,
                                 lastlayout = True,
                                 ds = ds,
                                 )
elif psm == '':
    return context.retrieve_password_form(rendered = res,
                                 psm = None,
                                 firstlayout = True,
                                 lastlayout = True,
                                 ds = ds,
                                 )
elif psm == 'valid':

    reg_no = ds.get('reg_no')
    record = ds.get('record')
    s_id = record.id
    pw = context.waeup_tool.getCredential(s_id)

    if is_anon and not s_id:
        return context.retrieve_password_form(rendered = res,
                                 psm = "You are not allowed to call this form in this context. Please try to log in.",
                                 #psm = "%s, %s" % (psm,ds),
                                 firstlayout = True,
                                 lastlayout = True,
                                 ds = ds,
                                 )
    elif not is_anon:
        return context.retrieve_password_form(rendered = res,
                                 psm = "You are already logged in!",
                                 #psm = "%s, %s" % (psm,ds),
                                 firstlayout = True,
                                 lastlayout = True,
                                 ds = ds,
                                 )
    
    logger.info('%s successfully retrieved id %s and password %s' % (reg_no,s_id,pw))
    
    email = record.email
    co_name = prop.email_from_name
    co_email = prop.email_from_address
    probtype = 'Your Student Id and Password'


    message = short_msg %(co_name,co_email,email,co_email,probtype,record.name,s_id,pw)
    mhost.send(message)

    return context.retrieve_password_form(rendered = res,
                             psm = "Notification has been sent!",
                             firstlayout = True,
                             lastlayout = True,
                             ds = ds,
                             )
        

