main.py

<---Back

Select Code Highlighting Style:

BRIGHT | Seashell | Darkness

Select Font Size:

Small | Normal | Large
../www/get_script_html_files/strip/main.py
    1 #!/usr/bin/python2
    2 
    3 
    4 #######################################
    5 #
    6 # This script displays homepage
    7 #
    8 # Version: 1.8
    9 #
   10 #######################################
   11 
   12 
   13 #import all functions from lib.py
   14 
   15 from lib import *
   16 
   17 ############On homepage we want to randombly display 2 books.
   18 
   19 #let's get all ids from table 'books'
   20 
   21 sql='select id from books'
   22 tmp_list=select(sql)
   23 
   24 #tmp_list would look like that
   25 #[['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9'],['10']]
   26 #so, we need to extract each element and change its type to integer
   27 
   28 book_list=[]
   29 
   30 for line in tmp_list:
   31 
   32     book_list.append(int(line[0]))
   33 
   34 random_list = random.sample(book_list,2) #this new list have 2 random numbers out of book_list
   35 
   36 
   37 ############Each of 2 books will be placed into its own sell inside the product table
   38 #Product table will have 1 row and 2 columns.
   39 
   40 #This function populates 1 sell
   41 def populate_sell(book_id):
   42 
   43     #let's get info from table books
   44 
   45     sql="select title,author,price from books where id="+str(book_id)
   46     tmp_list=select_one(sql)
   47 
   48     title   =   tmp_list[0]
   49     author  =   tmp_list[1]
   50     price   =   tmp_list[2]
   51 
   52     #price is stored in cents, so we should convert it to dollars
   53 
   54     price = int(price)/100
   55 
   56 
   57     #Let's create URL for image file.
   58     #Naming convention for small version of image files is this: product_<book id>_img.jpg
   59 
   60     image_url = "../images/product_%s_small.jpg" % book_id
   61 
   62     #let's create html table that we'll nest into the sell of product table
   63 
   64     table = """
   65 
   66                 <table width='300'>
   67 
   68                     <tr align='center' valign='top'>
   69                         <td><a href='./show_book.py?book_id=%s'><img src='%s' height='150' border=0></a></td>
   70                     </tr>
   71 
   72                     <tr align='center'>
   73                         <td><p><b>%s</b></p></td>
   74                     </tr>
   75 
   76                     <tr align='center'>
   77                         <td><a href='./show_book.py?book_id=%s'>%s</a></td>
   78                     </tr>
   79                     <tr align='center'>
   80                         <td><p>&#36;%.2f</p></td>
   81                     </tr>
   82 
   83                 </table>
   84 
   85     """ % (book_id,image_url,author,book_id,title,price)
   86 
   87     return table
   88 
   89 #now let's create product table
   90 body_html = """
   91 
   92     <table align='center' valign='top'>
   93 
   94         <tr>
   95             <td>%s</td><td>%s</td>
   96         </tr>
   97 
   98     </table>
   99 
  100     """ % (populate_sell(random_list[0]),populate_sell(random_list[1]))
  101 
  102 
  103 #execute code below if this script is run as standalone script
  104 #don't execute code below if we import functions from this script
  105 
  106 if __name__ == "__main__":
  107 
  108     ############Determine if user is logged in
  109 
  110     is_logged_in = is_logged_in()
  111 
  112     #############generate and print html
  113 
  114     print 'Content-Type: text/html\n\n'
  115 
  116     caption = ''
  117 
  118     html=get_html(is_logged_in,body_html,caption)
  119 
  120     print html
  121