Subversion Repositories SmartDukaan

Rev

Rev 2278 | Rev 2466 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2278 Rev 2389
Line 6... Line 6...
6
from shop2020.clients.InventoryClient import InventoryClient
6
from shop2020.clients.InventoryClient import InventoryClient
7
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
7
from shop2020.thriftpy.model.v1.user.ttypes import PromotionException
8
from shop2020.model.v1.user.impl import Dataservice
8
from shop2020.model.v1.user.impl import Dataservice
9
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon, PromotionTracker
9
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon, PromotionTracker
10
from shop2020.utils.Utils import to_py_date
10
from shop2020.utils.Utils import to_py_date
11
from shop2020.model.v1.user.promotionrules import rule_for_5perc_discount_on_5k_purchase, rule_500_off_on_MI310
-
 
12
 
11
 
13
from elixir import session
12
from elixir import session
14
import datetime
13
import datetime, traceback
15
 
14
 
16
def initialize(dbname = 'user'):
15
def initialize(dbname = 'user'):
17
    Dataservice.initialize(dbname)
16
    Dataservice.initialize(dbname)
18
 
17
 
19
def create_promotion(name, rule_execution_src, start_on, end_on):
18
def create_promotion(name, rule_execution_src, start_on, end_on):
Line 41... Line 40...
41
    coupon = Coupon.get_by(coupon_code = coupon_code)
40
    coupon = Coupon.get_by(coupon_code = coupon_code)
42
    
41
    
43
    if coupon:
42
    if coupon:
44
        user_client = UserClient().get_client()
43
        user_client = UserClient().get_client()
45
        cart = user_client.getCart(cart_id)
44
        cart = user_client.getCart(cart_id)
-
 
45
        coupon_module = coupon.promotion.rule_execution_src.strip()
46
 
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)
47
        updated_cart = eval(coupon.promotion.rule_execution_src.strip() + ".execute(cart, coupon_code, {})")
50
            updated_cart = rule.execute(cart, coupon_code, {})
48
        user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
51
            user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
49
        return updated_cart
52
            return updated_cart
-
 
53
        
-
 
54
        except ImportError as e:
-
 
55
            traceback.print_stack()
-
 
56
            #TODO: Better message
-
 
57
            raise PromotionException(100, 'Internal error occurred.')
50
    else:
58
    else:
51
        print 'Invalid Coupon Code'
59
        print 'Invalid Coupon Code'
52
        raise PromotionException(101, 'Invalid Coupon Code')
60
        raise PromotionException(101, 'Invalid Coupon Code')
53
 
61
 
54
def track_coupon_usage(coupon_code, transaction_id, user_id):
62
def track_coupon_usage(coupon_code, transaction_id, user_id):