Subversion Repositories SmartDukaan

Rev

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