add_to_cart.py

<---Back

Select Code Highlighting Style:

Bright | Seashell | DARKNESS

Select Font Size:

Small | Normal | Large

View bugs:

BUG #1

BUG #2

../www/get_script_html_files/strip/add_to_cart.py
    1 #!/usr/bin/python2
    2 
    3 #######################################
    4 #
    5 # This is function to add book to the Shopping cart
    6 # if user is logged in
    7 #
    8 # Version: 1.4
    9 #
   10 #######################################
   11 
   12 
   13 #import all functions from lib.py
   14 
   15 from lib import *
   16 
   17 #get dictionary of values from web form
   18 
   19 form = cgi.FieldStorage()
   20 
   21 try:
   22     book_id = int(form['book_id'].value) #get value of variable from web form
   23 except:
   24     book_id = 0 #if value is not supplied by web form, assign 0
   25 
   26 
   27 #function to set cookie with book_id
   28 
   29 def set_book_id_cookie(book_id):
   30 
   31     result = 1
   32 
   33     if book_id == 0:
   34 
   35         return result
   36 
   37 
   38     try:
   39 
   40         #Set cookies on hard drive of user
   41         #You can go to  Test Portal to see cookies set by ShareLane
   42 
   43         cookie = Cookie.SimpleCookie()
   44 
   45         cookie['book_id'] = book_id
   46 #### BUG #1
   47 #### Expected: cookie variable book_id should be a list of values (so we can add several books to Shopping cart).
   48 ####    e.g. [2,4,5]
   49 #### Actual: only 1 book can be in the Shopping Cart
   50 
   51 
   52 #### BUG #2
   53 #### Expected: we should check if there is an existing variable book_id inside cookie file
   54 #### Actual: new value of book_id overwrites old value (if any)
   55 
   56         #we'll set cookie with book_id for 1 year
   57         time_expire = time.time() + 24 * 3600 * 365
   58 
   59         cookie['book_id']['expires'] = time_expire
   60 
   61         print cookie
   62 
   63         #if all is fine
   64         result = 0
   65 
   66     except:
   67 
   68         pass
   69 
   70     return result
   71 
   72 #execute code below if this script is run as standalone script
   73 #don't execute code below if we import functions from this script
   74 
   75 if __name__ == "__main__":
   76 
   77     ############check if  user is logged in
   78 
   79     is_logged_in = is_logged_in()
   80 
   81     ############print message depending on login
   82 
   83     #if logged in
   84     if is_logged_in == 0:
   85 
   86         cookie_result = set_book_id_cookie(book_id)
   87 
   88         if cookie_result == 0:
   89 
   90             message = "Book was added to the Shopping Cart"
   91 
   92         else:
   93             message = "Oops, error. Probably, your browser doesn't support cookies"
   94 
   95     else:
   96 
   97         message = "Oops, error. You must log in"
   98 
   99 
  100     #############generate and print html
  101 
  102     print 'Content-Type: text/html\n\n'
  103 
  104     body_html = ''
  105 
  106     caption = 'Add to Shopping Cart'
  107 
  108     html=get_html(is_logged_in,body_html,caption,message)
  109 
  110     print html
  111