| 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
|
| 5469 |
rajveer |
9 |
from shop2020.model.v1.user.impl.Dataservice import Promotion, Coupon, PromotionTracker,\
|
|
|
10 |
RechargeVoucher
|
| 1976 |
varun.gupt |
11 |
from shop2020.utils.Utils import to_py_date
|
|
|
12 |
|
|
|
13 |
from elixir import session
|
| 2389 |
varun.gupt |
14 |
import datetime, traceback
|
| 1976 |
varun.gupt |
15 |
|
| 3187 |
rajveer |
16 |
def initialize(dbname = 'user', db_hostname="localhost"):
|
|
|
17 |
Dataservice.initialize(dbname, db_hostname)
|
| 1976 |
varun.gupt |
18 |
|
|
|
19 |
def create_promotion(name, rule_execution_src, start_on, end_on):
|
|
|
20 |
promotion = Promotion()
|
|
|
21 |
promotion.name = name
|
|
|
22 |
promotion.rule_execution_src = rule_execution_src
|
|
|
23 |
promotion.start_on = to_py_date(start_on)
|
|
|
24 |
promotion.end_on = to_py_date(end_on)
|
|
|
25 |
promotion.created_on = datetime.datetime.now()
|
|
|
26 |
session.commit()
|
|
|
27 |
|
|
|
28 |
def get_all_promotions():
|
|
|
29 |
return Promotion.query.all()
|
|
|
30 |
|
|
|
31 |
def generate_coupons_for_promotion(promotion_id, coupon_code):
|
|
|
32 |
promotion = Promotion.get_by(id = promotion_id)
|
|
|
33 |
|
|
|
34 |
coupon = Coupon()
|
|
|
35 |
coupon.promotion = promotion
|
|
|
36 |
coupon.coupon_code = coupon_code
|
|
|
37 |
coupon.arguments = ""
|
|
|
38 |
session.commit()
|
|
|
39 |
|
|
|
40 |
def apply_coupon(coupon_code, cart_id):
|
|
|
41 |
coupon = Coupon.get_by(coupon_code = coupon_code)
|
|
|
42 |
|
|
|
43 |
if coupon:
|
|
|
44 |
user_client = UserClient().get_client()
|
|
|
45 |
cart = user_client.getCart(cart_id)
|
| 3554 |
varun.gupt |
46 |
|
| 2389 |
varun.gupt |
47 |
coupon_module = coupon.promotion.rule_execution_src.strip()
|
|
|
48 |
|
|
|
49 |
try:
|
| 3554 |
varun.gupt |
50 |
user_client.deleteDiscountsFromCart(cart_id)
|
| 2389 |
varun.gupt |
51 |
imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [coupon_module])
|
|
|
52 |
rule = eval("imported_coupon_module." + coupon_module)
|
| 2466 |
varun.gupt |
53 |
|
|
|
54 |
args = eval(coupon.arguments) if coupon.arguments is not None else {}
|
|
|
55 |
|
| 3554 |
varun.gupt |
56 |
updated_cart, discounts = rule.execute(cart, coupon_code, args)
|
|
|
57 |
|
|
|
58 |
if discounts and len(discounts) > 0: user_client.saveDiscounts(discounts)
|
|
|
59 |
|
| 2389 |
varun.gupt |
60 |
user_client.applyCouponToCart(cart_id, coupon_code, updated_cart.totalPrice, updated_cart.discountedPrice)
|
| 3554 |
varun.gupt |
61 |
|
| 2389 |
varun.gupt |
62 |
return updated_cart
|
|
|
63 |
|
|
|
64 |
except ImportError as e:
|
|
|
65 |
traceback.print_stack()
|
|
|
66 |
#TODO: Better message
|
|
|
67 |
raise PromotionException(100, 'Internal error occurred.')
|
| 1976 |
varun.gupt |
68 |
else:
|
|
|
69 |
print 'Invalid Coupon Code'
|
|
|
70 |
raise PromotionException(101, 'Invalid Coupon Code')
|
|
|
71 |
|
|
|
72 |
def track_coupon_usage(coupon_code, transaction_id, user_id):
|
|
|
73 |
promotion_tracker = PromotionTracker()
|
|
|
74 |
promotion_tracker.coupon_code = coupon_code
|
|
|
75 |
promotion_tracker.transaction_id = transaction_id
|
|
|
76 |
promotion_tracker.user_id = user_id
|
|
|
77 |
promotion_tracker.applied_on = datetime.datetime.now()
|
|
|
78 |
session.commit()
|
|
|
79 |
|
| 3386 |
varun.gupt |
80 |
def get_active_coupons():
|
|
|
81 |
return Coupon.query.all()
|
|
|
82 |
|
|
|
83 |
def get_successful_payment_count_for_coupon(coupon_code):
|
|
|
84 |
return PromotionTracker.query.filter_by(coupon_code = coupon_code).count()
|
|
|
85 |
|
|
|
86 |
def get_rule_doc_string(rule_name):
|
|
|
87 |
imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [rule_name])
|
|
|
88 |
rule = eval("imported_coupon_module." + rule_name)
|
|
|
89 |
return rule.__doc__
|
| 4189 |
varun.gupt |
90 |
|
| 1976 |
varun.gupt |
91 |
def close_session():
|
|
|
92 |
if session.is_active:
|
| 3376 |
rajveer |
93 |
session.close()
|
| 4189 |
varun.gupt |
94 |
|
| 3376 |
rajveer |
95 |
def is_alive():
|
|
|
96 |
try:
|
|
|
97 |
session.query(Promotion.id).limit(1).one()
|
|
|
98 |
return True
|
|
|
99 |
except:
|
| 4189 |
varun.gupt |
100 |
return False
|
|
|
101 |
|
| 4494 |
varun.gupt |
102 |
def get_discounts_for_entity(entity_id):
|
|
|
103 |
catalog_client = CatalogClient().get_client()
|
|
|
104 |
items = catalog_client.getItemsByCatalogId(entity_id)
|
|
|
105 |
|
|
|
106 |
discount_finder = ItemDiscountFinder()
|
|
|
107 |
discounts = discount_finder.getCouponsAndDiscountsOnItem(items[0])
|
|
|
108 |
|
|
|
109 |
if len(discounts) > 0:
|
|
|
110 |
return {discounts[0][0]: discounts[0][1]}
|
|
|
111 |
else:
|
|
|
112 |
return {}
|
|
|
113 |
|
| 4189 |
varun.gupt |
114 |
def get_item_discount_map(item_ids):
|
|
|
115 |
|
|
|
116 |
discount_finder = ItemDiscountFinder()
|
|
|
117 |
item_coupons_discounts = []
|
|
|
118 |
|
|
|
119 |
catalog_client = CatalogClient().get_client()
|
|
|
120 |
|
|
|
121 |
for item_id in item_ids:
|
|
|
122 |
item = catalog_client.getItem(item_id)
|
|
|
123 |
|
|
|
124 |
discounts = discount_finder.getCouponsAndDiscountsOnItem(item)
|
|
|
125 |
|
|
|
126 |
for discount in discounts:
|
|
|
127 |
item_coupons_discounts.append((item_id, discount[0], discount[1]))
|
|
|
128 |
break
|
|
|
129 |
|
|
|
130 |
return item_coupons_discounts
|
|
|
131 |
|
| 5469 |
rajveer |
132 |
def add_voucher(t_voucher):
|
|
|
133 |
voucher = RechargeVoucher()
|
|
|
134 |
voucher.available = True
|
|
|
135 |
voucher.amount = t_voucher.amount
|
|
|
136 |
voucher.voucherCode = t_voucher.voucherCode
|
|
|
137 |
voucher.voucherType = t_voucher.voucherType
|
|
|
138 |
if t_voucher.issuedOn:
|
|
|
139 |
voucher.issuedOn = to_py_date(t_voucher.issuedOn)
|
|
|
140 |
if t_voucher.expiredOn:
|
|
|
141 |
voucher.expiredOn = to_py_date(t_voucher.expiredOn)
|
|
|
142 |
session.commit()
|
|
|
143 |
|
|
|
144 |
def assign_voucher(userId, userEmail, voucherType, amount):
|
|
|
145 |
voucher = RechargeVoucher.query.filter_by(voucherType = voucherType, amount = amount, available = True).first()
|
|
|
146 |
if not voucher:
|
|
|
147 |
raise PromotionException(501, 'Voucher of this type is not available.')
|
|
|
148 |
voucher.userId = userId
|
|
|
149 |
voucher.available = False
|
|
|
150 |
voucher.issuedOn = datetime.datetime.now()
|
|
|
151 |
voucher.email = userEmail
|
|
|
152 |
session.commit()
|
|
|
153 |
return voucher
|
|
|
154 |
|
|
|
155 |
def mark_voucher_as_redeemed(voucherCode, redeemedOn):
|
|
|
156 |
voucher = RechargeVoucher.query.filter_by(voucherCode = voucherCode).first()
|
|
|
157 |
voucher.redeemedOn = to_py_date(redeemedOn)
|
|
|
158 |
voucher.redeemed = True
|
|
|
159 |
session.commit()
|
|
|
160 |
return True
|
|
|
161 |
|
| 4189 |
varun.gupt |
162 |
class ItemDiscountFinder:
|
|
|
163 |
|
|
|
164 |
def __init__(self):
|
|
|
165 |
self.coupon_modules = {}
|
|
|
166 |
coupons = get_active_coupons()
|
|
|
167 |
|
|
|
168 |
for coupon in coupons:
|
| 4941 |
varun.gupt |
169 |
if coupon.coupon_code not in ('4B@R10DER5', 'SAHOLIC4RS', 'SGalaxy', 'SAHOLIC4MJ', 'SAHOLIC4SS'):
|
| 4189 |
varun.gupt |
170 |
coupon_rule = coupon.promotion.rule_execution_src.strip()
|
|
|
171 |
|
|
|
172 |
imported_coupon_module = __import__("shop2020.model.v1.user.promotionrules", globals(), locals(), [coupon_rule])
|
|
|
173 |
self.coupon_modules[coupon.coupon_code] = eval("imported_coupon_module." + coupon_rule)
|
|
|
174 |
|
|
|
175 |
def getCouponsAndDiscountsOnItem(self, item):
|
|
|
176 |
discounts = []
|
|
|
177 |
|
|
|
178 |
for coupon_code, coupon_rule_module in self.coupon_modules.iteritems():
|
|
|
179 |
|
|
|
180 |
discount = coupon_rule_module.getDiscountOnItem(item)
|
|
|
181 |
|
|
|
182 |
if discount is not None:
|
|
|
183 |
discounts.append((coupon_code, discount))
|
|
|
184 |
|
|
|
185 |
return discounts
|