#!/usr/bin/python3
###################################################################################
# Enter a login
# Redirect to another page if login is entered
###################################################################################
import os
import cgi, cgitb

cgitb.enable()

elements = cgi.FieldStorage()
username = elements.getvalue('user') or 'Guest'   #get html form fields
password = elements.getvalue('pswd') or ''        
    
msg = ''

#-------------------------------------------------------------------------------------
# Function to validate entry fields 
#-------------------------------------------------------------------------------------
def validate():
    global msg                             #needed to modify global variable
                                           #no need if access to global is read only
    if username=='':
        msg = 'Please enter a login username (any value is OK)'
    if password=='':
        msg = 'Please enter a password (any value is OK)'

#-------------------------------------------------------------------------------------
# Function to display the form 
#-------------------------------------------------------------------------------------
def display():
                                           
    print('Content-Type: text/html')            #reponse header             
    print('\n')                                 #end all response headers

    print(F'''
        <!DOCTYPE html>
        <html>
        <body bgcolor="lightyellow">
        <h1><center>Enter a Login</center></h1>
        <form name="form1" method="POST" >             <!--the name of the current Python script-->
        <fieldset style='width:580px; height:100px; border-color:gold'>
        <legend><i>Enter login below</i></legend>    
        <table bgcolor=lightgray width=570px>
            <tr><td><b>Username
                <td><input type="text" name="user" value="{username}">
            <tr><td><b>Password
                <td><input type="text" name="pswd" value="{password}">
                <i>(Any value is OK)</i>
            <tr><td><td><input type="submit" value="  Login " /></td>
        </table>
        <br><p style='color:red'> {msg} </p>
        </body>
        </html>
    ''')                                       
    
#==========================================================================
# main
#==========================================================================
if not elements:                                    #if first time 
    display()                                       #display the page

if elements:                                        #if subsequent time
    validate()                                      #validate                            

    if msg:                                         #if errors
        display()                                   #redisplay the same page
    else:
        print('Set-Cookie: login=' + username)      #set a cookie     
        print('Location: page2.py')                 #REDIRECT to another page     
        print('\n')                                 #end the response headers




#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================