Subversion Repositories SmartDukaan

Rev

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