Subversion Repositories SmartDukaan

Rev

Rev 6433 | Rev 6561 | 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, \
15
    get_active_gvs, delete_coupon
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
 
6355 amit.gupta 91
    def createCoupon(self, promotionId, endOn, email, amount, isCod, usage):
6250 amit.gupta 92
        '''
93
        Parameters:
94
        - promotionId
95
        - couponCode
96
        '''
97
        try:
6355 amit.gupta 98
            return create_coupon(promotionId, endOn, email, amount, isCod, usage)
6250 amit.gupta 99
        finally:
100
            PromotionDataAccessors.close_session()
6433 anupam.sin 101
 
102
    def applyRechargeCoupon(self, couponCode, rechargeOrderId, userId):
103
        try:
104
            return apply_recharge_coupon(couponCode, rechargeOrderId, userId)
105
        finally:
106
            PromotionDataAccessors.close_session()
6250 amit.gupta 107
 
1976 varun.gupt 108
    def applyCoupon(self, couponCode, cartId):
109
        '''
110
        Parameters:
111
        - couponCode
112
        - cartId
113
        '''
114
        try:
115
            print 'Calling apply_coupon'
116
            return apply_coupon(couponCode, cartId)
117
        finally:
118
            PromotionDataAccessors.close_session()
119
 
120
    def trackCouponUsage(self, couponCode, transactionId, userId):
121
        '''
122
        Parameters:
123
        - couponCode
124
        - transactionId
125
        - userId
126
        '''
127
        try:
128
            track_coupon_usage(couponCode, transactionId, userId)
129
        finally:
130
            PromotionDataAccessors.close_session()
131
 
132
    def getCouponUsageCountByUser(self, couponCode, userId):
133
        '''
134
        Parameters:
135
        - couponCode
136
        - userId
137
        '''
138
        try:
139
            return get_coupon_usage_count_by_user(couponCode, userId)
140
        finally:
3376 rajveer 141
            PromotionDataAccessors.close_session()
3386 varun.gupt 142
 
143
    def getActiveCoupons(self):
144
        '''
145
        Returns a list of active coupons
146
        '''
147
        try:
148
            return [to_t_coupon(coupon) for coupon in get_active_coupons()]
149
        finally:
150
            PromotionDataAccessors.close_session()
151
 
6497 amit.gupta 152
 
153
    def deleteCoupon(self, couponCode):
154
        '''
155
        Returns a list of active coupons
156
        '''
157
        try:
158
            delete_coupon(couponCode)
159
        finally:
160
            PromotionDataAccessors.close_session()
161
 
3386 varun.gupt 162
    def getSuccessfulPaymentCountForCoupon(self, couponCode):
163
        '''
164
        Returns the count of successful payments done using this coupon
165
 
166
        Parameters:
167
        - couponCode
168
        '''
169
        try:
170
            return get_successful_payment_count_for_coupon(couponCode)
171
        finally:
172
            PromotionDataAccessors.close_session()
173
 
174
    def getRuleDocString(self, ruleName):
175
        '''
176
        Returns the doc string of the rule module
177
 
178
        Parameters:
179
        - ruleName
180
        '''
181
        try:
182
            return get_rule_doc_string(ruleName)
183
        finally:
184
            PromotionDataAccessors.close_session()
4189 varun.gupt 185
 
186
    def getItemDiscountMap(self, itemIds):
187
        '''
188
        Returns 
189
        '''
190
        try:
191
            return [to_t_item_coupon_discount(item_coupon_discount) for item_coupon_discount in get_item_discount_map(itemIds)]
192
        finally:
193
            PromotionDataAccessors.close_session()
4494 varun.gupt 194
 
195
    def getDiscountsForEntity(self, entityId):
196
        '''
197
        Returns the tuple containing discount coupon and discount value applicable for this entity
4189 varun.gupt 198
 
4494 varun.gupt 199
        Parameters:
200
         - entityId
201
        '''
202
        try:
203
            return get_discounts_for_entity(entityId)
204
        finally:
205
            PromotionDataAccessors.close_session()
5469 rajveer 206
 
6497 amit.gupta 207
    def getActiveCodes(self, promotionId):
208
        '''
209
        Returns the tuple containing Active gift vouchers
210
 
211
        Parameters:
212
         - entityId
213
        '''
214
        try:
215
            return get_active_gvs(promotionId)
216
        finally:
217
            PromotionDataAccessors.close_session()
218
 
5469 rajveer 219
    def addVoucher(self, voucher):
220
        """
221
        Parameters:
222
         - voucher
223
        """
224
        try:
225
            return add_voucher(voucher)
226
        finally:
227
            PromotionDataAccessors.close_session()
228
 
229
    def assignVoucher(self, userId, userEmail, voucherType, amount):
230
        """
231
        Parameters:
232
         - userId
233
         - voucherType
234
         - amount
235
        """
236
        try:
237
            return to_t_voucher(assign_voucher(userId, userEmail, voucherType, amount))
238
        finally:
239
            PromotionDataAccessors.close_session()
240
 
241
    def markVoucherAsRedeemed(self, voucherCode, redeemedOn):
242
        """
243
        Parameters:
244
         - voucherCode
245
         - redeemedOn
246
        """
247
        try:
248
            return mark_voucher_as_redeemed(voucherCode, redeemedOn)
249
        finally:
250
            PromotionDataAccessors.close_session()
251
 
3386 varun.gupt 252
    def isAlive(self):
3376 rajveer 253
        """
254
        For checking weather service is active alive or not. It also checks connectivity with database
255
        """
256
        try:
257
            return is_alive()
258
        finally:
3386 varun.gupt 259
            PromotionDataAccessors.close_session()