| 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 |
return rule.__doc__
|
| 4189 |
varun.gupt |
89 |
|
| 1976 |
varun.gupt |
90 |
def close_session():
|
|
|
91 |
if session.is_active:
|
| 3376 |
rajveer |
92 |
session.close()
|
| 4189 |
varun.gupt |
93 |
|
| 3376 |
rajveer |
94 |
def is_alive():
|
|
|
95 |
try:
|
|
|
96 |
session.query(Promotion.id).limit(1).one()
|
|
|
97 |
return True
|
|
|
98 |
except:
|
| 4189 |
varun.gupt |
99 |
return False
|
|
|
100 |
|
| 4494 |
varun.gupt |
101 |
def get_discounts_for_entity(entity_id):
|
|
|
102 |
catalog_client = CatalogClient().get_client()
|
|
|
103 |
items = catalog_client.getItemsByCatalogId(entity_id)
|
|
|
104 |
|
|
|
105 |
discount_finder = ItemDiscountFinder()
|
|
|
106 |
discounts = discount_finder.getCouponsAndDiscountsOnItem(items[0])
|
|
|
107 |
|
|
|
108 |
if len(discounts) > 0:
|
|
|
109 |
return {discounts[0][0]: discounts[0][1]}
|
|
|
110 |
else:
|
|
|
111 |
return {}
|
|
|
112 |
|
| 4189 |
varun.gupt |
113 |
def get_item_discount_map(item_ids):
|
|
|
114 |
|
|
|
115 |
discount_finder = ItemDiscountFinder()
|
|
|
116 |
item_coupons_discounts = []
|
|
|
117 |
|
|
|
118 |
catalog_client = CatalogClient().get_client()
|
|
|
119 |
|
|
|
120 |
for item_id in item_ids:
|
|
|
121 |
item = catalog_client.getItem(item_id)
|
|
|
122 |
|
|
|
123 |
discounts = discount_finder.getCouponsAndDiscountsOnItem(item)
|
|
|
124 |
|
|
|
125 |
for discount in discounts:
|
|
|
126 |
item_coupons_discounts.append((item_id, discount[0], discount[1]))
|
|
|
127 |
break
|
|
|
128 |
|
|
|
129 |
return item_coupons_discounts
|
|
|
130 |
|
|
|
131 |
class ItemDiscountFinder:
|
|
|
132 |
|
|
|
133 |
def __init__(self):
|
|
|
134 |
self.coupon_modules = {}
|
|
|
135 |
coupons = get_active_coupons()
|
|
|
136 |
|
|
|
137 |
for coupon in coupons:
|
| 4941 |
varun.gupt |
138 |
if coupon.coupon_code not in ('4B@R10DER5', 'SAHOLIC4RS', 'SGalaxy', 'SAHOLIC4MJ', 'SAHOLIC4SS'):
|
| 4189 |
varun.gupt |
139 |
coupon_rule = coupon.promotion.rule_execution_src.strip()
|
|
|
140 |
|
|
|
141 |
imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [coupon_rule])
|
|
|
142 |
self.coupon_modules[coupon.coupon_code] = eval("imported_coupon_module." + coupon_rule)
|
|
|
143 |
|
|
|
144 |
def getCouponsAndDiscountsOnItem(self, item):
|
|
|
145 |
discounts = []
|
|
|
146 |
|
|
|
147 |
for coupon_code, coupon_rule_module in self.coupon_modules.iteritems():
|
|
|
148 |
|
|
|
149 |
discount = coupon_rule_module.getDiscountOnItem(item)
|
|
|
150 |
|
|
|
151 |
if discount is not None:
|
|
|
152 |
discounts.append((coupon_code, discount))
|
|
|
153 |
|
|
|
154 |
return discounts
|