Subversion Repositories SmartDukaan

Rev

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