Subversion Repositories SmartDukaan

Rev

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