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