lib.py

<---Back

Select Code Highlighting Style:

BRIGHT | Seashell | Darkness

Select Font Size:

Small | Normal | Large
../www/get_script_html_files/strip/lib.py
    1 #!/usr/bin/python2
    2 
    3 #######################################
    4 #
    5 # This is main module used by ShareLane Python scripts
    6 # If function is used twice or more we put it into this module
    7 #
    8 # Code of ShareLane.com was written by Roman Savenkov
    9 # Feel free to use any portion of ShareLane code for your projects
   10 #
   11 # Version: 1.41
   12 #
   13 #######################################
   14 
   15 #import all relevant libraries
   16 
   17 import cgi #this is Python module to accept data, e.g. email,  submitted via web forms
   18 import string ##this is Python module to work with strings
   19 import re #this is Python module to work with regular expressions
   20 import random #this is Python module to generate random numbers
   21 import time #this is Python module to work with time related functions
   22 import os #this is Python module to interact with OS
   23 import Cookie #this is Python module to work with cookies
   24 from db_lib import * #this is our custom module to connect to DB and run SQL query
   25 import sys #this is Python with system functions
   26 import commands #this is another Python module to interact with OS
   27 
   28 
   29 #####################################################
   30 
   31 
   32 #Below is description of functions from db_lib module
   33 
   34 #select(sql_query) - this function returns a list of all matching entries in DB
   35 
   36 #select_one(sql_query) - this function returns first matching entry from DB
   37 
   38 #update(sql_query) - this function is used to do sql commands update, insert and delete.
   39 
   40 
   41 #####################################################
   42 
   43 
   44 #Function to get html code for the page
   45 
   46 def get_html(is_logged_in,body_html,caption,message=''):
   47 
   48     ############Let's create basic HTML tags and import file with stylesheets
   49         html="""
   50     <html>
   51         <title>ShareLane - your test application</title>
   52         <head>
   53             <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   54             <meta http-equiv="description" content="ShareLane is practical addition to tutorial
   55     How to Become a Software Tester. See QATutor.com for details">
   56             <link rel="stylesheet" type="text/css" href="http://www.sharelane.com/styles.css" />
   57         </head>
   58     <body>
   59 
   60     <center>
   61 
   62     <table width=700 class="form_all_underline">
   63 
   64     """
   65 
   66     ############Now let's insert application version.
   67 
   68     #App. version of each particular environment is stored in DB like that:
   69     #build id is stored in column 'build_id' of table 'release_info'
   70     #db version is stored in column 'db_version' of table 'release_info'
   71 
   72 
   73     #get values from table 'release_info'
   74     sql="SELECT build_id,db_version FROM release_info"
   75     tmp_list=select_one(sql)
   76 
   77     build_id=tmp_list[0] #[0] is used to get FIRST element of the returned list
   78     db_version=tmp_list[1]
   79 
   80     #now let's form string in this format: <!-- application version <build id>/<db version> -->
   81 
   82     version_string="<!-- application version "+build_id+"/"+db_version+" -->"
   83 
   84     #insert application version into html
   85     html=html+version_string
   86 
   87 
   88     ############Let's create html for the header
   89 
   90     #if user is logged in we want to show his or her first name
   91 
   92     if is_logged_in == 0:
   93 
   94         first_name = get_user_info()['first_name']
   95 
   96     else:
   97 
   98         first_name = ''
   99 
  100     if first_name != '':
  101 
  102         first_name = "Hello "+first_name
  103 
  104 
  105 
  106     header_html="""
  107 
  108     <tr><td>
  109         <table width=700>
  110             <tr valign='top'>
  111                 <td><a href='./main.py'>
  112                     <img src='../images/logo.jpg' border=0></a></td>
  113                 <td><span class='user'>%s</span></td>
  114                 <td align='right'>
  115                     <a href='./shopping_cart.py'><img src='../images/shopping_cart.gif' border=0>Shopping Cart</a></td>
  116 
  117             </tr>
  118         </table>
  119     </td></tr>
  120 
  121     """ % first_name
  122 
  123     ############Let's create html for search
  124 
  125     search_html="""
  126 
  127     <tr><td>
  128 
  129         <form action='./search.py'>
  130 
  131         <input type='text' name='keyword' size='50'><input type='submit' value='Search'>
  132 
  133 
  134         </form>
  135 
  136     </td></tr>
  137 
  138     """
  139 
  140 
  141     ############Now, let's create Log in/Log out part of the page
  142 
  143     log_in_html="""
  144 
  145     <tr><td>
  146         <table width='100%' class='grey_bg'>
  147 
  148             <form action='./log_in.py'>
  149 
  150             <tr align='right'>
  151                 <td><p>Email<input type='text' name='email'></p></td>
  152                 <td><p>Password<input type='password' name='password'></p></td>
  153                 <td><input type='submit' value='Login'></td>
  154                 <td><a href='./register.py'>Sign up</a></td>
  155             </tr>
  156 
  157 
  158 
  159             </form>
  160 
  161         </table>
  162     </td></tr>
  163 
  164     """
  165 
  166     log_out_html="""
  167 
  168     <tr class='grey_bg' align='right'><td>
  169 
  170         <a href='./log_out.py'>Logout</a>
  171 
  172     </td></tr>
  173 
  174     """
  175 
  176     caption_html = """
  177 
  178     <tr class='grey_bg' align='center'><td>
  179 
  180         <p><b>%s</b></p>
  181 
  182     </td></tr>
  183 
  184     """ % caption
  185 
  186     #UNLESS variable caption not equal to '':
  187     #If boolean is_logged_in is 0 then we display link Log out
  188     #If boolean is_logged_in is 1 then we display interface for Log in and link Sign up
  189 
  190 
  191     if caption == '':
  192 
  193         if is_logged_in == 1:
  194 
  195             account_html=log_in_html
  196 
  197         else:
  198             account_html=log_out_html
  199 
  200     else:
  201 
  202         account_html=caption_html
  203 
  204 
  205 
  206 
  207     ############In some cases, we'll have a message for user
  208     #For example, error message
  209 
  210 
  211     #if message = '', default, we supply blank html row
  212 
  213     if message=='':
  214 
  215         message_html="""
  216 
  217         <tr><td>
  218         </td></tr>
  219 
  220         """
  221 
  222     else:
  223 
  224         #first let's check if this is error message or confirmation message
  225         if re.findall('error',string.lower(message)):
  226 
  227             #in case of errror we want to user font style error_message
  228             message_style='error_message'
  229 
  230         else:
  231             message_style='confirmation_message'
  232 
  233 
  234         message_html="""
  235 
  236         <tr align='center'><td>
  237 
  238         <span class='%s'>%s</span>
  239 
  240         </td></tr>
  241 
  242         <tr><td>
  243 
  244         <HR>
  245 
  246         </td></tr>
  247         """ % (message_style,message)
  248 
  249 
  250     ############Now, let's create footer of the page
  251 
  252     footer_html="""
  253 
  254     <tr class='grey_bg' align='center'><td>
  255 
  256         <a href='http://www.qatutor.com' class='footer'>QATutor.com</a> |
  257         <a href='../test_portal.html' class='footer'>Test Portal</a> | \
  258         <a href='../source_files/%s_bright_normal.html' class='footer'>Python Source File</a> | \
  259         <a href='mailto:qatest@gmail.com' class='footer'>Contact</a>
  260 
  261     </td></tr>
  262 
  263     """ % sys.argv[0].split('.')[0]
  264 
  265 
  266     ############create html body
  267 
  268 
  269     body_html="""
  270 
  271     <tr>
  272         <td>
  273 
  274             %s
  275 
  276         </td>
  277     </tr>
  278 
  279     """ % body_html
  280 
  281 
  282     ############Glue all html block together
  283 
  284     html=html+header_html+search_html+account_html+message_html+body_html+footer_html
  285 
  286 
  287     html=html+"""
  288 
  289     </table>
  290     </center>
  291     </body>
  292     </html>"""
  293 
  294     return html
  295 
  296 
  297 #function to determine if user is logged in
  298 
  299 def is_logged_in():
  300 
  301 
  302     #this flag will be set to 1 unless we find match between values in cookies and DB
  303 
  304     is_logged_in = 1
  305 
  306     try:
  307 
  308         if 'HTTP_COOKIE' in os.environ:
  309 
  310             #try to extract cookies set by ShareLane
  311 
  312             cookies = os.environ['HTTP_COOKIE']
  313 
  314             cookies = cookies.split('; ')
  315 
  316             handler = {}
  317 
  318             for cookie in cookies:
  319 
  320                 cookie = cookie.split('=')
  321 
  322                 handler[cookie[0]] = cookie[1]
  323 
  324             user_id_user = handler['user_id']
  325             email = handler['email']
  326 
  327             #remove double quotes
  328 
  329             email = re.sub("\"",'',email)
  330 
  331             #get account number from DB
  332 
  333             sql = "select id from users where email='%s'" % email
  334 
  335             user_id_db = select_one(sql)[0]
  336 
  337 
  338             #see if account number from cookies matches account number in DB
  339 
  340             if user_id_db.strip() == user_id_user.strip():
  341 
  342                 #if so, set var is_logged_in to 0
  343 
  344                 is_logged_in=0
  345 
  346     except:
  347 
  348         pass
  349 
  350     return is_logged_in
  351 
  352 
  353 #function to generate html to show page with 1 book
  354 
  355 def show_book(book_id):
  356 
  357     book_id = str(book_id)
  358 
  359     table="<table width=500>"
  360 
  361     #get title, author and price of the book
  362 
  363     book_info_dict = get_book_info(book_id)
  364 
  365     title   =   book_info_dict['title']
  366     author  =   book_info_dict['author']
  367     price   =   book_info_dict['price']/100 #convert from cents
  368 
  369     #create sub table with nicely formatted title, author and price info
  370 
  371     sub_table = """
  372             <table width=300>
  373                 <tr>
  374                     <p><b>%s</b></p>
  375                 </tr>
  376 
  377                 <tr>
  378                     <p>%s <a href='./add_to_cart.py?book_id=%s'><img src='../images/add_to_cart.gif' border=0></a></p>
  379                 </tr>
  380 
  381                 <tr>
  382                     <p><b><font color='green'>Price: &#36;%.2f</font></b></p>
  383                 </tr>
  384 
  385             </table>
  386 
  387     """ % (author,title,book_id,price)
  388 
  389     #create main table with picture and book info
  390 
  391     table = """
  392 
  393         <table width=600 align='center'>
  394 
  395             <tr>
  396                 <td><img src='../images/product_%s_large.jpg'></td><td>%s</td>
  397             </tr>
  398 
  399         </table>
  400 
  401     """ % (book_id,sub_table)
  402 
  403     return table
  404 
  405 #get user info from DB using user_id from cookie file
  406 
  407 def get_user_info():
  408 
  409 
  410     #populate dictionary with blanks
  411 
  412     user_info_dict = {}
  413 
  414     user_info_dict['user_id']   =   ''
  415     user_info_dict['first_name']    =   ''
  416     user_info_dict['last_name'] =   ''
  417     user_info_dict['zip_code']  =   ''
  418     user_info_dict['email']     =   ''
  419     user_info_dict['street']    =   ''
  420     user_info_dict['city']      =   ''
  421     user_info_dict['state']     =   ''
  422     user_info_dict['country']   =   ''
  423 
  424     try:
  425 
  426         if 'HTTP_COOKIE' in os.environ:
  427 
  428             #try to extract cookies set by ShareLane
  429 
  430             cookies = os.environ['HTTP_COOKIE']
  431 
  432             cookies = cookies.split('; ')
  433 
  434             handler = {}
  435 
  436             for cookie in cookies:
  437 
  438                 cookie = cookie.split('=')
  439 
  440                 handler[cookie[0]] = cookie[1]
  441 
  442             user_id = handler['user_id']
  443 
  444             if int(user_id) > 0:
  445 
  446                 result = int(user_id)
  447 
  448                 sql = "select first_name,last_name,zip_code,email,street,city,state,country from users where id = %s" % result
  449 
  450                 user_info_list = select_one(sql)
  451 
  452                 user_info_dict['user_id']   =   user_id
  453                 user_info_dict['first_name']    =   user_info_list[0]
  454                 user_info_dict['last_name'] =   user_info_list[1]
  455                 user_info_dict['zip_code']  =   user_info_list[2]
  456                 user_info_dict['email']     =   user_info_list[3]
  457                 user_info_dict['street']    =   user_info_list[4]
  458                 user_info_dict['city']      =   user_info_list[5]
  459                 user_info_dict['state']     =   user_info_list[6]
  460                 user_info_dict['country']   =   user_info_list[7]
  461 
  462     except:
  463 
  464         pass
  465 
  466     return user_info_dict
  467 
  468 #function to get book info from DB
  469 
  470 def get_book_info(book_id):
  471 
  472     #this dictionary will hold key-value pairs for title,author and price
  473 
  474     book_info_dict = {}
  475 
  476     #get title, author and price of the book
  477 
  478     sql = "select title, author, price from books where id = " + str(book_id)
  479     book_info_list=select_one(sql)
  480 
  481     title   =   book_info_list[0]
  482     author  =   book_info_list[1]
  483     price   =   book_info_list[2]
  484 
  485     #change type to integer
  486 
  487     price = int(price)
  488 
  489     #populate dictionary
  490 
  491     book_info_dict['title'] = title
  492     book_info_dict['author'] = author
  493     book_info_dict['price'] = price
  494 
  495     return book_info_dict
  496