<s> def shopping_cart ( ) : <EOL> """<STR_LIT>""" <EOL> classes = [ ] <EOL> amount = <NUM_LIT:0> <EOL> for class_id in session . cart : <EOL> cart_class = Class ( id = class_id ) <EOL> classes . append ( cart_class ) <EOL> amount += cart_class . course . price - cart_class . course . discount <EOL> return dict ( classes = classes , <EOL> amount = amount ) <EOL> def remove_from_shopping_cart ( ) : <EOL> """<STR_LIT>""" <EOL> class_id = request . args ( <NUM_LIT:0> , cast = int ) <EOL> session . cart . remove ( class_id ) <EOL> redirect ( URL ( '<STR_LIT>' ) ) <EOL> @ auth . requires_login ( ) <EOL> def register_order ( ) : <EOL> """<STR_LIT>""" <EOL> for class_id in session . cart : <EOL> cart_class = Class ( id = int ( class_id ) ) <EOL> if not can_enroll ( cart_class ) : <EOL> session . flash = T ( '<STR_LIT>' ) % cart_class . course . title <EOL> session . cart . remove ( class_id ) <EOL> redirect ( URL ( '<STR_LIT>' ) ) <EOL> order = Order . insert ( <EOL> user_id = auth . user . id , <EOL> order_date = request . now , <EOL> products = [ Class ( id = cart_class ) for cart_class in session . cart ] , <EOL> status = <NUM_LIT:1> <EOL> ) <EOL> session . order = order <EOL> session . cart = [ ] <EOL> message = T ( "<STR_LIT>" ) % order . token <EOL> for product in order . products : <EOL> message += '<STR_LIT:\n>' <EOL> message += T ( "<STR_LIT>" ) % ( product . course . title , product . start_date , product . end_date ) <EOL> message += '<STR_LIT:\n>' <EOL> message += T ( "<STR_LIT>" ) % order . amount <EOL> message += '<STR_LIT>' <EOL> message += T ( "<STR_LIT>" ) <EOL> message += '<STR_LIT:\n>' <EOL> message += T ( "<STR_LIT>" ) <EOL> mail . send ( to = order . user_id . email , <EOL> subject = T ( "<STR_LIT>" ) , <EOL> message = message <EOL> ) <EOL> redirect ( URL ( '<STR_LIT>' ) ) <EOL> @ auth . requires_login ( ) <EOL> def pay_courses ( ) : <EOL> """<STR_LIT>""" <EOL> order = Order ( id = session . order ) <EOL> if order . amount == <NUM_LIT:0> : <EOL> pending = Pending . insert ( <EOL> order_id = session . order , <EOL> confirmed = True <EOL> ) <EOL> confirmed = Confirmed . insert ( <EOL> order_id = order . id , <EOL> pending_id = pending . id , <EOL> confirmation_time = request . now <EOL> ) <EOL> order . update_record ( status = <NUM_LIT:2> ) <EOL> session . pending = pending <EOL> redirect ( URL ( '<STR_LIT:success>' ) ) <EOL> message = T ( '<STR_LIT>' ) % order . token <EOL> message += '<STR_LIT:\n>' <EOL> message += T ( '<STR_LIT>' ) % order . amount <EOL> message += '<STR_LIT:\n>' <EOL> message += T ( '<STR_LIT>' ) <EOL> message += '<STR_LIT>' <EOL> message += T ( '<STR_LIT>' ) <EOL> mail . send ( to = order . user_id . email , <EOL> subject = T ( "<STR_LIT>" ) , <EOL> message = message <EOL> ) <EOL> else : <EOL> pending = Pending . insert ( order_id = order . id ) <EOL> session . pending = pending <EOL> redirect ( URL ( '<STR_LIT>' ) ) <EOL> @ auth . requires_login ( ) <EOL> def paypal ( ) : <EOL> """<STR_LIT>""" <EOL> if not session . pending : <EOL> raise HTTP ( <NUM_LIT> ) <EOL> pending = Pending ( id = session . pending ) <EOL> return dict ( pending = pending ) <EOL> def ipn ( ) : <EOL> """<STR_LIT>""" <EOL> import json <EOL> write_logs ( request ) <EOL> pending = Pending ( id = int ( request . vars . invoice ) , confirmed = False ) <EOL> if not pending : <EOL> raise HTTP ( <NUM_LIT> ) <EOL> already_confirmed = Confirmed ( pending_id = pending . id ) <EOL> if already_confirmed : <EOL> return dict ( status = "<STR_LIT>" , data = already_confirmed ) <EOL> if request . vars . payment_status == '<STR_LIT>' : <EOL> confirmed = Confirmed . insert ( <EOL> order_id = pending . order_id , <EOL> pending_id = pending . id , <EOL> confirmation_time = request . now <EOL> ) <EOL> pending . update_record ( confirmed = True ) <EOL> Order ( id = pending . order_id ) . update_record ( status = <NUM_LIT:2> ) <EOL> message = T ( '<STR_LIT>' ) % pending . order_id . token <EOL> message += '<STR_LIT:\n>' <EOL> message += T ( '<STR_LIT>' ) % pending . order_id . amount <EOL> message += '<STR_LIT:\n>' <EOL> message += T ( '<STR_LIT>' ) <EOL> message += '<STR_LIT>' <EOL> message += T ( '<STR_LIT>' ) <EOL> mail . send ( to = pending . order_id . user_id . email , <EOL> subject = T ( "<STR_LIT>" ) , <EOL> message = message <EOL> ) <EOL> else : <EOL> if request . is_local : <EOL> path = '<STR_LIT>' <EOL> else : <EOL> path = '<STR_LIT>' <EOL> message = '<STR_LIT:->' * <NUM_LIT> <EOL> message += '<STR_LIT>' <EOL> message += str ( request . vars ) <EOL> message += '<STR_LIT:\n>' <EOL> log_in_file ( message , path ) <EOL> Order ( id = pending . order_id ) . update_record ( status = <NUM_LIT:3> ) <EOL> mail_message = T ( '<STR_LIT>' ) <EOL> mail_message += '<STR_LIT>' <EOL> mail_message += T ( "<STR_LIT>" ) <EOL> mail_message += '<STR_LIT:\n>' <EOL> mail_message += T ( "<STR_LIT>" ) <EOL> mail_message += '<STR_LIT>' <EOL> mail_message += T ( "<STR_LIT>" ) <EOL> mail . send ( to = pending . order_id . user_id . email , <EOL> subject = T ( '<STR_LIT>' ) , <EOL> message = mail_message <EOL> ) <EOL> return json . dumps ( request . vars ) <EOL> @ auth . requires_login ( ) <EOL> def success ( ) : <EOL> """<STR_LIT>""" <EOL> if request . is_local : <EOL> log_in_file ( str ( request . vars ) , '<STR_LIT>' ) <EOL> else : <EOL> log_in_file ( str ( request . vars ) , '<STR_LIT>' ) <EOL> pending = Pending ( id = session . pending ) <EOL> if request . is_local : <EOL> confirmed = Confirmed . insert ( <EOL> order_id = pending . order_id , <EOL> pending_id = pending . id , <EOL> confirmation_time = request . now <EOL> ) <EOL> pending . update_record ( confirmed = True ) <EOL> Order ( id = pending . order_id ) . update_record ( status = <NUM_LIT:2> ) <EOL> mail_message = T ( '<STR_LIT>' ) % pending . order_id . token <EOL> mail_message += '<STR_LIT:\n>' <EOL> mail_message += T ( '<STR_LIT>' ) % pending . order_id . amount <EOL> mail_message += '<STR_LIT:\n>' <EOL> mail_message += T ( '<STR_LIT>' ) <EOL> mail_message += '<STR_LIT>' <EOL> mail_message += T ( '<STR_LIT>' ) <EOL> mail . send ( to = pending . order_id . user_id . email , <EOL> subject = T ( "<STR_LIT>" ) , <EOL> message = mail_message <EOL> ) <EOL> if pending . confirmed or request . is_local : <EOL> message = T ( '<STR_LIT>' ) <EOL> for cart_class in pending . order_id . products : <EOL> Student . insert ( student = auth . user . id , class_id = cart_class ) <EOL> else : <EOL> message = T ( '<STR_LIT>' ) <EOL> return dict ( message = message ) <EOL> @ auth . requires_login ( ) <EOL> def history ( ) : <EOL> """<STR_LIT>""" <EOL> history = db ( Order . user_id == auth . user . id ) . select ( ) <EOL> return dict ( history = history ) <EOL> @ auth . requires ( lambda : is_user_order ( order_id = request . args ( <NUM_LIT:0> , cast = int ) ) ) <EOL> def details ( ) : <EOL> """<STR_LIT>""" <EOL> order = Order ( id = request . args ( <NUM_LIT:0> , cast = int ) ) <EOL> return dict ( order = order ) </s>
