shopping_cart.py

<---Back

Select Code Highlighting Style:

Bright | Seashell | DARKNESS

Select Font Size:

Small | Normal | Large

View bugs:

BUG #1

BUG #2

BUG #3

BUG #4

BUG #5

BUG #6

../www/get_script_html_files/strip/shopping_cart.py
    1 #!/usr/bin/python2
    2 
    3 #######################################
    4 #
    5 # This is script to show Shopping cart and update number of books
    6 #
    7 # Version: 1.4
    8 #
    9 #######################################
   10 
   11 
   12 #import all functions from lib.py
   13 
   14 from lib import *
   15 
   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 #we'll use variable q for quantity of books
   28 
   29 try:
   30     q = int(form['q'].value)
   31 except:
   32     q = 1
   33 
   34 
   35 #function to calculate discount
   36 #-----------------------------
   37 #basket     | discount, %
   38 #-----------------------------
   39 #20 - 49    | 2
   40 #50 - 99    | 3
   41 #100-499    | 4
   42 #500-999    | 5
   43 #1000-4999  | 6
   44 #5000-9999  | 7
   45 #10000 and more | 8
   46 
   47 def get_discount(q):
   48 
   49     discount = 0
   50 
   51 
   52 #### BUG #1
   53 #### Expected: 20
   54 #### Actual: 12
   55 #Bug is discovered by test 'D' (see book/Test Preps/Methods of Test Selection/Boundary Values)
   56 
   57     if q >= 12 and q <= 49:
   58 
   59         discount = 2
   60 
   61 #### BUG #2
   62 #### Expected: >= 50
   63 #### Actual: > 50
   64 #Bug is discovered by test 'A'
   65 
   66     elif q > 50 and q <= 99:
   67 
   68         discount = 3
   69 
   70 #### BUG #3
   71 #### Expected: 499
   72 #### Actual: 399
   73 #Bug is discovered by test 'B'
   74 
   75     elif q >= 100 and q <= 399:
   76 
   77 
   78         discount = 4
   79 
   80 #### BUG #4
   81 #### Expected: >= 500, <= 999
   82 #### Actual: <= 500, >=999
   83 #Bug is discovered by test 'C'
   84 
   85     elif q <= 500 and q >= 999:
   86 
   87         discount = 5
   88 
   89 #WOW! No bugs here :)
   90 
   91     elif q >=1000 and q <= 4999:
   92 
   93         discount = 6
   94 
   95 #### BUG #5
   96 #### Expected: logical 'and'
   97 #### Actual: logical 'or'
   98 #Bug is discovered by test 'E'
   99 
  100 #IMPORTANT!!! Any quantity that haven't been matched before will get 7% discount
  101 
  102     elif q >= 5000 or q <=9999:
  103 
  104         discount = 7
  105 
  106 #code is correct but it doesn't matter, because previous statement takes care
  107 #of any quantity.
  108 
  109     elif q >= 10000:
  110 
  111         discount = 8
  112 
  113 
  114     return discount
  115 
  116 #function to get value of book_id from cookie file
  117 
  118 def get_book_id_cookie():
  119 
  120     book_id = 0
  121 
  122     try:
  123 
  124         if 'HTTP_COOKIE' in os.environ:
  125 
  126             #try to extract cookies set by ShareLane
  127 
  128             cookies = os.environ['HTTP_COOKIE']
  129 
  130             cookies = cookies.split('; ')
  131 
  132             handler = {}
  133 
  134             for cookie in cookies:
  135 
  136                 cookie = cookie.split('=')
  137 
  138                 handler[cookie[0]] = cookie[1]
  139 
  140             book_id = int(handler['book_id'])
  141 
  142             #check if book with this id exists in DB
  143 
  144             sql = "select id from books id=" + book_id
  145 
  146             book_id = select_one(sql)[0]
  147 
  148 
  149     except:
  150 
  151         pass
  152 
  153     return book_id
  154 
  155 #function to generate page
  156 
  157 def show_cart(book_id,q):
  158 
  159     #get title, author and price of the book
  160 
  161     book_info_dict = get_book_info(book_id)
  162 
  163     title   =   book_info_dict['title']
  164     author  =   book_info_dict['author']
  165     price   =   book_info_dict['price']
  166 
  167     #get discount
  168 
  169     discount_rate = get_discount(q)
  170 
  171     #get total price
  172 
  173     total = price * q
  174 
  175     discount_amount = total / 100.00 * discount_rate
  176 
  177 #### BUG #6
  178 #### Expected: amount of discount must be subtracted from total. Sign must be '-'
  179 #### Actual: amount discount is added to total. Sign is '+'
  180 
  181     total = total + discount_amount
  182 
  183     #create sub table with nicely formatted title, author and price info
  184 
  185     table = """
  186 
  187         <table width=700 align='center' cellpadding=3>
  188 
  189             <tr align='center' class='yellow_bg'>
  190 
  191                 <form action='./shopping_cart.py'>
  192 
  193                 <td>author</td><td>title</td><td>quantity</td><td>price</td>\
  194                 <td>discount,&#37;</td><td>discount,&#36;</td><td>total,&#36;</td>
  195 
  196             </tr>
  197 
  198             <tr align='center'>
  199 
  200                 <td>%s</td><td>%s</td><td>
  201 
  202                 <input type='text' name='q' size='4' value='%s'>
  203 
  204                 </td><td>%.2f</td><td><p><b>%s</b></td><td>%s</td><td>%.2f</td>
  205 
  206             </tr>""" % (author,title,q,price/100,discount_rate,discount_amount/100,total/100)
  207 
  208 
  209     table = table + """
  210 
  211             <tr>
  212 
  213                 <td colspan=7><HR></td>
  214 
  215             </tr>
  216 
  217             <tr>
  218 
  219                 <td colspan=7>
  220 
  221 
  222                             <input type='submit' value='Update'>
  223 
  224                     <input type='hidden' name='book_id' value='%s'>
  225 
  226 
  227                 </td>
  228 
  229                 </form>
  230 
  231             </tr>""" % (book_id)
  232 
  233     table = table + """
  234 
  235             <tr>
  236 
  237                 <td colspan=7>
  238 
  239                     <form action='./checkout.py'>
  240 
  241                             <input type='submit' value='Proceed to Checkout'>
  242 
  243                     <input type='hidden' name='book_id' value='%s'>
  244 
  245                     <input type='hidden' name='q' value='%s'>
  246 
  247                     <input type='hidden' name='total' value='%s'>
  248 
  249                             </form>
  250 
  251             </tr>
  252 
  253 
  254         </table>""" % (book_id,q,int(total))
  255 
  256     return table
  257 
  258 #error-handling function for quantity:
  259 #check if input consists of numbers, not 0 and not negative
  260 
  261 def check_quantity(q):
  262 
  263     q = str(q)
  264 
  265     if q == '0':
  266 
  267         return 1
  268 
  269     for i in q:
  270 
  271         if ord(i) not in range(48,59):
  272 
  273             return 1
  274 
  275     return int(q)
  276 
  277 #execute code below if this script is run as standalone script
  278 #don't execute code below if we import functions from this script
  279 
  280 if __name__ == "__main__":
  281 
  282     q = check_quantity(q)
  283 
  284     ############check if  user is logged in
  285 
  286     is_logged_in = is_logged_in()
  287 
  288     ############logical part
  289 
  290     if is_logged_in == 0:
  291 
  292         #first case: no book_id is provided by web form
  293         if book_id == 0:
  294 
  295             #check cookie file
  296             book_id = get_book_id_cookie()
  297 
  298             #generate message if there is nothing in cookie file
  299             if book_id == 0:
  300 
  301                 body_html = ''
  302 
  303                 message = "Cart is empty"
  304 
  305             #otherwise generate html with table
  306             else:
  307 
  308                 body_html = show_cart(book_id,q)
  309 
  310                 message = ''
  311 
  312         #second case, book_id is provided
  313         else:
  314 
  315             body_html = show_cart(book_id,q)
  316 
  317             message = 'Cart Updated'
  318     else:
  319         body_html = ''
  320         message = "Oops, error. You must log in"
  321 
  322     #############generate and print html
  323 
  324     print 'Content-Type: text/html\n\n'
  325 
  326     caption = 'Shopping Cart'
  327 
  328     html=get_html(is_logged_in,body_html,caption,message)
  329 
  330     print html
  331