Rev 2389 | Rev 3187 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on May 24, 2010@author: Varun Gupta'''from shop2020.clients.UserClient import UserClientfrom shop2020.clients.InventoryClient import InventoryClientfrom shop2020.thriftpy.model.v1.user.ttypes import PromotionExceptionfrom shop2020.model.v1.user.impl import Dataservicefrom shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon, PromotionTrackerfrom shop2020.utils.Utils import to_py_datefrom elixir import sessionimport datetime, tracebackdef initialize(dbname = 'user'):Dataservice.initialize(dbname)def create_promotion(name, rule_execution_src, start_on, end_on):promotion = Promotion()promotion.name = namepromotion.rule_execution_src = rule_execution_srcpromotion.start_on = to_py_date(start_on)promotion.end_on = to_py_date(end_on)promotion.created_on = datetime.datetime.now()session.commit()def get_all_promotions():return Promotion.query.all()def generate_coupons_for_promotion(promotion_id, coupon_code):promotion = Promotion.get_by(id = promotion_id)coupon = Coupon()coupon.promotion = promotioncoupon.coupon_code = coupon_codecoupon.arguments = ""session.commit()def apply_coupon(coupon_code, cart_id):coupon = Coupon.get_by(coupon_code = coupon_code)if coupon:user_client = UserClient().get_client()cart = user_client.getCart(cart_id)coupon_module = coupon.promotion.rule_execution_src.strip()try:imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [coupon_module])rule = eval("imported_coupon_module." + coupon_module)args = eval(coupon.arguments) if coupon.arguments is not None else {}updated_cart = rule.execute(cart, coupon_code, args)user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)return updated_cartexcept ImportError as e:traceback.print_stack()#TODO: Better messageraise PromotionException(100, 'Internal error occurred.')else:print 'Invalid Coupon Code'raise PromotionException(101, 'Invalid Coupon Code')def track_coupon_usage(coupon_code, transaction_id, user_id):promotion_tracker = PromotionTracker()promotion_tracker.coupon_code = coupon_codepromotion_tracker.transaction_id = transaction_idpromotion_tracker.user_id = user_idpromotion_tracker.applied_on = datetime.datetime.now()session.commit()def close_session():if session.is_active:session.close()