Subversion Repositories SmartDukaan

Rev

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