Subversion Repositories SmartDukaan

Rev

Rev 4189 | Rev 5469 | 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.PromotionDataAccessors import initialize, create_promotion,\
3386 varun.gupt 7
    generate_coupons_for_promotion, apply_coupon, track_coupon_usage, is_alive,\
4189 varun.gupt 8
    get_active_coupons, get_successful_payment_count_for_coupon, get_rule_doc_string, \
4494 varun.gupt 9
    get_item_discount_map, get_discounts_for_entity
2121 varun.gupt 10
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
1976 varun.gupt 11
from shop2020.model.v1.user.impl import PromotionDataAccessors
4189 varun.gupt 12
from shop2020.model.v1.user.impl.Converters import to_t_coupon, to_t_item_coupon_discount
1976 varun.gupt 13
 
14
class PromotionServiceHandler:
15
 
3187 rajveer 16
    def __init__(self, dbname='user', db_hostname='localhost'):
17
        initialize(dbname, db_hostname)
1976 varun.gupt 18
 
19
    def createPromotion(self, name, ruleExecutionSrc, startOn, endOn):
20
        '''
21
        Parameters:
22
        - name
23
        - ruleExecutionSrc
24
        - startOn
25
        - endOn
26
        '''
27
        try:
28
            create_promotion(name, ruleExecutionSrc, startOn, endOn)
29
        finally:
30
            PromotionDataAccessors.close_session()
31
 
32
    def getAllPromotions(self):
33
        try:
34
            pass
35
        finally:
36
            PromotionDataAccessors.close_session()
37
 
38
    def getPromotionById(self, promotionId):
39
        '''
40
        Parameters:
41
        - promotionId
42
        '''
43
        try:
44
            pass
45
        finally:
46
            PromotionDataAccessors.close_session()
47
 
48
    def generateCouponsForPromotion(self, promotionId, couponCode):
49
        '''
50
        Parameters:
51
        - promotionId
52
        - couponCode
53
        '''
54
        try:
55
            generate_coupons_for_promotion(promotionId, couponCode)
56
        finally:
57
            PromotionDataAccessors.close_session()
58
 
59
    def applyCoupon(self, couponCode, cartId):
60
        '''
61
        Parameters:
62
        - couponCode
63
        - cartId
64
        '''
65
        try:
66
            print 'Calling apply_coupon'
67
            return apply_coupon(couponCode, cartId)
68
        finally:
69
            PromotionDataAccessors.close_session()
70
 
71
    def trackCouponUsage(self, couponCode, transactionId, userId):
72
        '''
73
        Parameters:
74
        - couponCode
75
        - transactionId
76
        - userId
77
        '''
78
        try:
79
            track_coupon_usage(couponCode, transactionId, userId)
80
        finally:
81
            PromotionDataAccessors.close_session()
82
 
83
    def getCouponUsageCountByUser(self, couponCode, userId):
84
        '''
85
        Parameters:
86
        - couponCode
87
        - userId
88
        '''
89
        try:
90
            return get_coupon_usage_count_by_user(couponCode, userId)
91
        finally:
3376 rajveer 92
            PromotionDataAccessors.close_session()
3386 varun.gupt 93
 
94
    def getActiveCoupons(self):
95
        '''
96
        Returns a list of active coupons
97
        '''
98
        try:
99
            return [to_t_coupon(coupon) for coupon in get_active_coupons()]
100
        finally:
101
            PromotionDataAccessors.close_session()
102
 
103
    def getSuccessfulPaymentCountForCoupon(self, couponCode):
104
        '''
105
        Returns the count of successful payments done using this coupon
106
 
107
        Parameters:
108
        - couponCode
109
        '''
110
        try:
111
            return get_successful_payment_count_for_coupon(couponCode)
112
        finally:
113
            PromotionDataAccessors.close_session()
114
 
115
    def getRuleDocString(self, ruleName):
116
        '''
117
        Returns the doc string of the rule module
118
 
119
        Parameters:
120
        - ruleName
121
        '''
122
        try:
123
            return get_rule_doc_string(ruleName)
124
        finally:
125
            PromotionDataAccessors.close_session()
4189 varun.gupt 126
 
127
    def getItemDiscountMap(self, itemIds):
128
        '''
129
        Returns 
130
        '''
131
        try:
132
            return [to_t_item_coupon_discount(item_coupon_discount) for item_coupon_discount in get_item_discount_map(itemIds)]
133
        finally:
134
            PromotionDataAccessors.close_session()
4494 varun.gupt 135
 
136
    def getDiscountsForEntity(self, entityId):
137
        '''
138
        Returns the tuple containing discount coupon and discount value applicable for this entity
4189 varun.gupt 139
 
4494 varun.gupt 140
        Parameters:
141
         - entityId
142
        '''
143
        try:
144
            return get_discounts_for_entity(entityId)
145
        finally:
146
            PromotionDataAccessors.close_session()
147
 
3386 varun.gupt 148
    def isAlive(self):
3376 rajveer 149
        """
150
        For checking weather service is active alive or not. It also checks connectivity with database
151
        """
152
        try:
153
            return is_alive()
154
        finally:
3386 varun.gupt 155
            PromotionDataAccessors.close_session()