Subversion Repositories SmartDukaan

Rev

Rev 11819 | Rev 11821 | Go to most recent revision | 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
11820 amit.gupta 68
        coupon_code = cart.coupon_code
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():
11819 amit.gupta 83
                        print `privateDeal.isCod`
11741 amit.gupta 84
                        if not privateDeal.isCod:
85
                            return False
6355 amit.gupta 86
        finally:
87
            PromotionDataAccessors.close_session()
88
            return isCod
89
 
1976 varun.gupt 90
    def getPromotionById(self, promotionId):
91
        '''
92
        Parameters:
93
        - promotionId
94
        '''
95
        try:
96
            pass
97
        finally:
98
            PromotionDataAccessors.close_session()
99
 
100
    def generateCouponsForPromotion(self, promotionId, couponCode):
101
        '''
102
        Parameters:
103
        - promotionId
104
        - couponCode
105
        '''
106
        try:
107
            generate_coupons_for_promotion(promotionId, couponCode)
108
        finally:
109
            PromotionDataAccessors.close_session()
110
 
8707 manish.sha 111
    def createCoupon(self, promotionId, couponCode, couponCategory, arguments, isCod, prefix):
6250 amit.gupta 112
        try:
8707 manish.sha 113
            return create_coupon(promotionId, couponCategory, couponCode, arguments, isCod, prefix)
6250 amit.gupta 114
        finally:
115
            PromotionDataAccessors.close_session()
6433 anupam.sin 116
 
6561 amit.gupta 117
 
6433 anupam.sin 118
    def applyRechargeCoupon(self, couponCode, rechargeOrderId, userId):
119
        try:
120
            return apply_recharge_coupon(couponCode, rechargeOrderId, userId)
121
        finally:
122
            PromotionDataAccessors.close_session()
6250 amit.gupta 123
 
1976 varun.gupt 124
    def applyCoupon(self, couponCode, cartId):
125
        '''
126
        Parameters:
127
        - couponCode
128
        - cartId
129
        '''
130
        try:
131
            print 'Calling apply_coupon'
132
            return apply_coupon(couponCode, cartId)
133
        finally:
134
            PromotionDataAccessors.close_session()
6736 amit.gupta 135
 
136
    def getEmiDiscount(self, cartId):
137
        '''
138
        Parameters:
139
        - cartId
140
        '''
141
        try:
142
            print 'Calling get_emi_discount'
143
            return get_emi_discount(cartId)
144
        finally:
145
            PromotionDataAccessors.close_session()
146
 
1976 varun.gupt 147
 
148
    def trackCouponUsage(self, couponCode, transactionId, userId):
149
        '''
150
        Parameters:
151
        - couponCode
152
        - transactionId
153
        - userId
154
        '''
155
        try:
156
            track_coupon_usage(couponCode, transactionId, userId)
157
        finally:
158
            PromotionDataAccessors.close_session()
159
 
160
    def getCouponUsageCountByUser(self, couponCode, userId):
161
        '''
162
        Parameters:
163
        - couponCode
164
        - userId
165
        '''
166
        try:
167
            return get_coupon_usage_count_by_user(couponCode, userId)
168
        finally:
3376 rajveer 169
            PromotionDataAccessors.close_session()
3386 varun.gupt 170
 
171
    def getActiveCoupons(self):
172
        '''
173
        Returns a list of active coupons
174
        '''
175
        try:
176
            return [to_t_coupon(coupon) for coupon in get_active_coupons()]
177
        finally:
178
            PromotionDataAccessors.close_session()
179
 
6497 amit.gupta 180
 
181
    def deleteCoupon(self, couponCode):
182
        '''
183
        Returns a list of active coupons
184
        '''
185
        try:
186
            delete_coupon(couponCode)
187
        finally:
188
            PromotionDataAccessors.close_session()
189
 
3386 varun.gupt 190
    def getSuccessfulPaymentCountForCoupon(self, couponCode):
191
        '''
192
        Returns the count of successful payments done using this coupon
193
 
194
        Parameters:
195
        - couponCode
196
        '''
197
        try:
198
            return get_successful_payment_count_for_coupon(couponCode)
199
        finally:
200
            PromotionDataAccessors.close_session()
201
 
202
    def getRuleDocString(self, ruleName):
203
        '''
204
        Returns the doc string of the rule module
205
 
206
        Parameters:
207
        - ruleName
208
        '''
209
        try:
210
            return get_rule_doc_string(ruleName)
211
        finally:
212
            PromotionDataAccessors.close_session()
4189 varun.gupt 213
 
214
    def getItemDiscountMap(self, itemIds):
215
        '''
216
        Returns 
217
        '''
218
        try:
219
            return [to_t_item_coupon_discount(item_coupon_discount) for item_coupon_discount in get_item_discount_map(itemIds)]
220
        finally:
221
            PromotionDataAccessors.close_session()
4494 varun.gupt 222
 
223
    def getDiscountsForEntity(self, entityId):
224
        '''
225
        Returns the tuple containing discount coupon and discount value applicable for this entity
4189 varun.gupt 226
 
4494 varun.gupt 227
        Parameters:
228
         - entityId
229
        '''
230
        try:
231
            return get_discounts_for_entity(entityId)
232
        finally:
233
            PromotionDataAccessors.close_session()
5469 rajveer 234
 
6497 amit.gupta 235
    def getActiveCodes(self, promotionId):
236
        '''
237
        Returns the tuple containing Active gift vouchers
238
 
239
        Parameters:
240
         - entityId
241
        '''
242
        try:
243
            return get_active_gvs(promotionId)
244
        finally:
245
            PromotionDataAccessors.close_session()
246
 
5469 rajveer 247
    def addVoucher(self, voucher):
248
        """
249
        Parameters:
250
         - voucher
251
        """
252
        try:
253
            return add_voucher(voucher)
254
        finally:
255
            PromotionDataAccessors.close_session()
256
 
257
    def assignVoucher(self, userId, userEmail, voucherType, amount):
258
        """
259
        Parameters:
260
         - userId
261
         - voucherType
262
         - amount
263
        """
264
        try:
265
            return to_t_voucher(assign_voucher(userId, userEmail, voucherType, amount))
266
        finally:
267
            PromotionDataAccessors.close_session()
268
 
269
    def markVoucherAsRedeemed(self, voucherCode, redeemedOn):
270
        """
271
        Parameters:
272
         - voucherCode
273
         - redeemedOn
274
        """
275
        try:
276
            return mark_voucher_as_redeemed(voucherCode, redeemedOn)
277
        finally:
278
            PromotionDataAccessors.close_session()
7092 amit.gupta 279
 
280
    def getAllCouponsByPromotionId(self, promotionId):
281
        try:
282
            return get_all_coupons_by_promotion_id(promotionId)
283
        finally:
284
            PromotionDataAccessors.close_session()
285
 
5469 rajveer 286
 
7092 amit.gupta 287
 
3386 varun.gupt 288
    def isAlive(self):
3376 rajveer 289
        """
290
        For checking weather service is active alive or not. It also checks connectivity with database
291
        """
292
        try:
293
            return is_alive()
294
        finally:
3386 varun.gupt 295
            PromotionDataAccessors.close_session()