Subversion Repositories SmartDukaan

Rev

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