Subversion Repositories SmartDukaan

Rev

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