Subversion Repositories SmartDukaan

Rev

Rev 3133 | Rev 3376 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1976 varun.gupt 1
'''
2
Created on May 24, 2010
3
@author: Varun Gupta
4
'''
5
from shop2020.clients.UserClient import UserClient
3133 rajveer 6
from shop2020.clients.CatalogClient import CatalogClient
1976 varun.gupt 7
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
8
from shop2020.model.v1.user.impl import Dataservice
2121 varun.gupt 9
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon, PromotionTracker
1976 varun.gupt 10
from shop2020.utils.Utils import to_py_date
11
 
12
from elixir import session
2389 varun.gupt 13
import datetime, traceback
1976 varun.gupt 14
 
3187 rajveer 15
def initialize(dbname = 'user', db_hostname="localhost"):
16
    Dataservice.initialize(dbname, db_hostname)
1976 varun.gupt 17
 
18
def create_promotion(name, rule_execution_src, start_on, end_on):
19
    promotion = Promotion()
20
    promotion.name = name
21
    promotion.rule_execution_src = rule_execution_src
22
    promotion.start_on = to_py_date(start_on)
23
    promotion.end_on = to_py_date(end_on)
24
    promotion.created_on = datetime.datetime.now()
25
    session.commit()
26
 
27
def get_all_promotions():
28
    return Promotion.query.all()
29
 
30
def generate_coupons_for_promotion(promotion_id, coupon_code):
31
    promotion = Promotion.get_by(id = promotion_id)
32
 
33
    coupon = Coupon()
34
    coupon.promotion = promotion
35
    coupon.coupon_code = coupon_code
36
    coupon.arguments = ""
37
    session.commit()
38
 
39
def apply_coupon(coupon_code, cart_id):
40
    coupon = Coupon.get_by(coupon_code = coupon_code)
41
 
42
    if coupon:
43
        user_client = UserClient().get_client()
44
        cart = user_client.getCart(cart_id)
2389 varun.gupt 45
        coupon_module = coupon.promotion.rule_execution_src.strip()
46
 
47
        try:
48
            imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [coupon_module])
49
            rule = eval("imported_coupon_module." + coupon_module)
2466 varun.gupt 50
 
51
            args = eval(coupon.arguments) if coupon.arguments is not None else {}
52
 
53
            updated_cart = rule.execute(cart, coupon_code, args)
2389 varun.gupt 54
            user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
55
            return updated_cart
56
 
57
        except ImportError as e:
58
            traceback.print_stack()
59
            #TODO: Better message
60
            raise PromotionException(100, 'Internal error occurred.')
1976 varun.gupt 61
    else:
62
        print 'Invalid Coupon Code'
63
        raise PromotionException(101, 'Invalid Coupon Code')
64
 
65
def track_coupon_usage(coupon_code, transaction_id, user_id):
66
    promotion_tracker = PromotionTracker()
67
    promotion_tracker.coupon_code = coupon_code
68
    promotion_tracker.transaction_id = transaction_id
69
    promotion_tracker.user_id = user_id
70
    promotion_tracker.applied_on = datetime.datetime.now()
71
    session.commit()
72
 
73
def close_session():
74
    if session.is_active:
75
        session.close()