Subversion Repositories SmartDukaan

Rev

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