Subversion Repositories SmartDukaan

Rev

Rev 3386 | Rev 4189 | 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)
3554 varun.gupt 45
 
2389 varun.gupt 46
        coupon_module = coupon.promotion.rule_execution_src.strip()
47
 
48
        try:
3554 varun.gupt 49
            user_client.deleteDiscountsFromCart(cart_id)
2389 varun.gupt 50
            imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [coupon_module])
51
            rule = eval("imported_coupon_module." + coupon_module)
2466 varun.gupt 52
 
53
            args = eval(coupon.arguments) if coupon.arguments is not None else {}
54
 
3554 varun.gupt 55
            updated_cart, discounts = rule.execute(cart, coupon_code, args)
56
 
57
            if discounts and len(discounts) > 0:    user_client.saveDiscounts(discounts)
58
 
2389 varun.gupt 59
            user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
3554 varun.gupt 60
 
2389 varun.gupt 61
            return updated_cart
62
 
63
        except ImportError as e:
64
            traceback.print_stack()
65
            #TODO: Better message
66
            raise PromotionException(100, 'Internal error occurred.')
1976 varun.gupt 67
    else:
68
        print 'Invalid Coupon Code'
69
        raise PromotionException(101, 'Invalid Coupon Code')
70
 
71
def track_coupon_usage(coupon_code, transaction_id, user_id):
72
    promotion_tracker = PromotionTracker()
73
    promotion_tracker.coupon_code = coupon_code
74
    promotion_tracker.transaction_id = transaction_id
75
    promotion_tracker.user_id = user_id
76
    promotion_tracker.applied_on = datetime.datetime.now()
77
    session.commit()
78
 
3386 varun.gupt 79
def get_active_coupons():
80
    return Coupon.query.all()
81
 
82
def get_successful_payment_count_for_coupon(coupon_code):
83
    return PromotionTracker.query.filter_by(coupon_code = coupon_code).count()
84
 
85
def get_rule_doc_string(rule_name):
86
    imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [rule_name])
87
    rule = eval("imported_coupon_module." + rule_name)
88
    print rule.__doc__
89
    return rule.__doc__
90
 
1976 varun.gupt 91
def close_session():
92
    if session.is_active:
3376 rajveer 93
        session.close()
94
 
95
def is_alive():
96
    try:
97
        session.query(Promotion.id).limit(1).one()
98
        return True
99
    except:
100
        return False