Subversion Repositories SmartDukaan

Rev

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