Subversion Repositories SmartDukaan

Rev

Rev 11900 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1976 varun.gupt 1
'''
2
Created on: May 24, 2011
3
@author: Varun Gupta
4
'''
5
 
11741 amit.gupta 6
from shop2020.clients import CatalogClient
1976 varun.gupt 7
from shop2020.model.v1.user.impl import PromotionDataAccessors
6250 amit.gupta 8
from shop2020.model.v1.user.impl.Converters import to_t_coupon, \
7746 amit.gupta 9
    to_t_item_coupon_discount, to_t_voucher, to_t_promotion
6250 amit.gupta 10
from shop2020.model.v1.user.impl.PromotionDataAccessors import initialize, \
11
    create_promotion, generate_coupons_for_promotion, apply_coupon, \
12
    track_coupon_usage, is_alive, get_active_coupons, \
13
    get_successful_payment_count_for_coupon, get_rule_doc_string, \
14
    get_item_discount_map, get_discounts_for_entity, add_voucher, assign_voucher, \
6497 amit.gupta 15
    mark_voucher_as_redeemed, create_coupon, get_coupon, apply_recharge_coupon, \
7746 amit.gupta 16
    get_active_gvs, delete_coupon, get_emi_discount, get_all_coupons_by_promotion_id, \
17
    get_all_promotions, remove_all_coupons_by_promotion_id
6250 amit.gupta 18
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import \
19
    get_coupon_usage_count_by_user
1976 varun.gupt 20
 
21
class PromotionServiceHandler:
22
 
3187 rajveer 23
    def __init__(self, dbname='user', db_hostname='localhost'):
24
        initialize(dbname, db_hostname)
1976 varun.gupt 25
 
26
    def createPromotion(self, name, ruleExecutionSrc, startOn, endOn):
27
        '''
28
        Parameters:
29
        - name
30
        - ruleExecutionSrc
31
        - startOn
32
        - endOn
33
        '''
34
        try:
35
            create_promotion(name, ruleExecutionSrc, startOn, endOn)
36
        finally:
37
            PromotionDataAccessors.close_session()
38
 
39
    def getAllPromotions(self):
40
        try:
7746 amit.gupta 41
            return [to_t_promotion(promotion) for promotion in get_all_promotions()]
1976 varun.gupt 42
        finally:
43
            PromotionDataAccessors.close_session()
7746 amit.gupta 44
 
45
    def removeAllCouponsByPromotionId(self, promotionId):
46
        try:
47
            return remove_all_coupons_by_promotion_id(promotionId)
48
        finally:
49
            PromotionDataAccessors.close_session()
6301 amit.gupta 50
 
51
    def getCoupon(self, coupon_code):
52
        try:
53
            return to_t_coupon(get_coupon(coupon_code))
54
        finally:
55
            PromotionDataAccessors.close_session()
1976 varun.gupt 56
 
6301 amit.gupta 57
    def isGiftVoucher(self, coupon_code):
6355 amit.gupta 58
        isGiftVoucher = False
6301 amit.gupta 59
        try:
6367 amit.gupta 60
            promotion_type = get_coupon(coupon_code).promotion.type
61
            isGiftVoucher = promotion_type == 1
6301 amit.gupta 62
        finally:
63
            PromotionDataAccessors.close_session()
6355 amit.gupta 64
            return isGiftVoucher
6301 amit.gupta 65
 
11820 amit.gupta 66
    def isCodApplicable(self, cart):
6355 amit.gupta 67
        isCod = True
11900 amit.gupta 68
        coupon_code = cart.couponCode
6355 amit.gupta 69
        try:
70
            coupon = get_coupon(coupon_code)
71
            if coupon.arguments:
72
                args = eval(coupon.arguments)
73
                if 'isCod' in args:
74
                    isCod = args['isCod']
11741 amit.gupta 75
            else:
76
                #We have private deal coupon
77
                if cart.lines is not None:
78
                    itemIds = []
79
                    [itemIds.append(line.itemId) for line in cart.lines]
80
                    catalog_client = CatalogClient().get_client()
81
                    privateDeals= catalog_client.getAllActivePrivateDeals(itemIds,0)
82
                    for privateDeal in privateDeals.values():
83
                        if not privateDeal.isCod:
84
                            return False
6355 amit.gupta 85
        finally:
86
            PromotionDataAccessors.close_session()
87
            return isCod
88
 
1976 varun.gupt 89
    def getPromotionById(self, promotionId):
90
        '''
91
        Parameters:
92
        - promotionId
93
        '''
94
        try:
95
            pass
96
        finally:
97
            PromotionDataAccessors.close_session()
98
 
99
    def generateCouponsForPromotion(self, promotionId, couponCode):
100
        '''
101
        Parameters:
102
        - promotionId
103
        - couponCode
104
        '''
105
        try:
106
            generate_coupons_for_promotion(promotionId, couponCode)
107
        finally:
108
            PromotionDataAccessors.close_session()
109
 
8707 manish.sha 110
    def createCoupon(self, promotionId, couponCode, couponCategory, arguments, isCod, prefix):
6250 amit.gupta 111
        try:
8707 manish.sha 112
            return create_coupon(promotionId, couponCategory, couponCode, arguments, isCod, prefix)
6250 amit.gupta 113
        finally:
114
            PromotionDataAccessors.close_session()
6433 anupam.sin 115
 
6561 amit.gupta 116
 
6433 anupam.sin 117
    def applyRechargeCoupon(self, couponCode, rechargeOrderId, userId):
118
        try:
119
            return apply_recharge_coupon(couponCode, rechargeOrderId, userId)
120
        finally:
121
            PromotionDataAccessors.close_session()
6250 amit.gupta 122
 
1976 varun.gupt 123
    def applyCoupon(self, couponCode, cartId):
124
        '''
125
        Parameters:
126
        - couponCode
127
        - cartId
128
        '''
129
        try:
130
            print 'Calling apply_coupon'
131
            return apply_coupon(couponCode, cartId)
132
        finally:
133
            PromotionDataAccessors.close_session()
6736 amit.gupta 134
 
135
    def getEmiDiscount(self, cartId):
136
        '''
137
        Parameters:
138
        - cartId
139
        '''
140
        try:
141
            print 'Calling get_emi_discount'
142
            return get_emi_discount(cartId)
143
        finally:
144
            PromotionDataAccessors.close_session()
145
 
1976 varun.gupt 146
 
13521 amit.gupta 147
    def trackCouponUsage(self, couponCode, transactionId, userId,amount, isDigital):
1976 varun.gupt 148
        '''
149
        Parameters:
150
        - couponCode
151
        - transactionId
152
        - userId
153
        '''
154
        try:
13521 amit.gupta 155
            track_coupon_usage(couponCode, transactionId, userId, amount, isDigital)
1976 varun.gupt 156
        finally:
157
            PromotionDataAccessors.close_session()
158
 
159
    def getCouponUsageCountByUser(self, couponCode, userId):
160
        '''
161
        Parameters:
162
        - couponCode
163
        - userId
164
        '''
165
        try:
166
            return get_coupon_usage_count_by_user(couponCode, userId)
167
        finally:
3376 rajveer 168
            PromotionDataAccessors.close_session()
3386 varun.gupt 169
 
170
    def getActiveCoupons(self):
171
        '''
172
        Returns a list of active coupons
173
        '''
174
        try:
175
            return [to_t_coupon(coupon) for coupon in get_active_coupons()]
176
        finally:
177
            PromotionDataAccessors.close_session()
178
 
6497 amit.gupta 179
 
180
    def deleteCoupon(self, couponCode):
181
        '''
182
        Returns a list of active coupons
183
        '''
184
        try:
185
            delete_coupon(couponCode)
186
        finally:
187
            PromotionDataAccessors.close_session()
188
 
3386 varun.gupt 189
    def getSuccessfulPaymentCountForCoupon(self, couponCode):
190
        '''
191
        Returns the count of successful payments done using this coupon
192
 
193
        Parameters:
194
        - couponCode
195
        '''
196
        try:
197
            return get_successful_payment_count_for_coupon(couponCode)
198
        finally:
199
            PromotionDataAccessors.close_session()
200
 
201
    def getRuleDocString(self, ruleName):
202
        '''
203
        Returns the doc string of the rule module
204
 
205
        Parameters:
206
        - ruleName
207
        '''
208
        try:
209
            return get_rule_doc_string(ruleName)
210
        finally:
211
            PromotionDataAccessors.close_session()
4189 varun.gupt 212
 
213
    def getItemDiscountMap(self, itemIds):
214
        '''
215
        Returns 
216
        '''
217
        try:
218
            return [to_t_item_coupon_discount(item_coupon_discount) for item_coupon_discount in get_item_discount_map(itemIds)]
219
        finally:
220
            PromotionDataAccessors.close_session()
4494 varun.gupt 221
 
222
    def getDiscountsForEntity(self, entityId):
223
        '''
224
        Returns the tuple containing discount coupon and discount value applicable for this entity
4189 varun.gupt 225
 
4494 varun.gupt 226
        Parameters:
227
         - entityId
228
        '''
229
        try:
230
            return get_discounts_for_entity(entityId)
231
        finally:
232
            PromotionDataAccessors.close_session()
5469 rajveer 233
 
6497 amit.gupta 234
    def getActiveCodes(self, promotionId):
235
        '''
236
        Returns the tuple containing Active gift vouchers
237
 
238
        Parameters:
239
         - entityId
240
        '''
241
        try:
242
            return get_active_gvs(promotionId)
243
        finally:
244
            PromotionDataAccessors.close_session()
245
 
5469 rajveer 246
    def addVoucher(self, voucher):
247
        """
248
        Parameters:
249
         - voucher
250
        """
251
        try:
252
            return add_voucher(voucher)
253
        finally:
254
            PromotionDataAccessors.close_session()
255
 
256
    def assignVoucher(self, userId, userEmail, voucherType, amount):
257
        """
258
        Parameters:
259
         - userId
260
         - voucherType
261
         - amount
262
        """
263
        try:
264
            return to_t_voucher(assign_voucher(userId, userEmail, voucherType, amount))
265
        finally:
266
            PromotionDataAccessors.close_session()
267
 
268
    def markVoucherAsRedeemed(self, voucherCode, redeemedOn):
269
        """
270
        Parameters:
271
         - voucherCode
272
         - redeemedOn
273
        """
274
        try:
275
            return mark_voucher_as_redeemed(voucherCode, redeemedOn)
276
        finally:
277
            PromotionDataAccessors.close_session()
7092 amit.gupta 278
 
279
    def getAllCouponsByPromotionId(self, promotionId):
280
        try:
281
            return get_all_coupons_by_promotion_id(promotionId)
282
        finally:
283
            PromotionDataAccessors.close_session()
284
 
5469 rajveer 285
 
7092 amit.gupta 286
 
3386 varun.gupt 287
    def isAlive(self):
3376 rajveer 288
        """
289
        For checking weather service is active alive or not. It also checks connectivity with database
290
        """
291
        try:
292
            return is_alive()
293
        finally:
3386 varun.gupt 294
            PromotionDataAccessors.close_session()