Subversion Repositories SmartDukaan

Rev

Rev 4189 | Rev 5469 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on: May 24, 2011
@author: Varun Gupta
'''

from shop2020.model.v1.user.impl.PromotionDataAccessors import initialize, create_promotion,\
    generate_coupons_for_promotion, apply_coupon, track_coupon_usage, is_alive,\
    get_active_coupons, get_successful_payment_count_for_coupon, get_rule_doc_string, \
    get_item_discount_map, get_discounts_for_entity
from shop2020.model.v1.user.impl.PromotionRuleDataUtilities import get_coupon_usage_count_by_user
from shop2020.model.v1.user.impl import PromotionDataAccessors
from shop2020.model.v1.user.impl.Converters import to_t_coupon, to_t_item_coupon_discount

class PromotionServiceHandler:
    
    def __init__(self, dbname='user', db_hostname='localhost'):
        initialize(dbname, db_hostname)
    
    def createPromotion(self, name, ruleExecutionSrc, startOn, endOn):
        '''
        Parameters:
        - name
        - ruleExecutionSrc
        - startOn
        - endOn
        '''
        try:
            create_promotion(name, ruleExecutionSrc, startOn, endOn)
        finally:
            PromotionDataAccessors.close_session()
    
    def getAllPromotions(self):
        try:
            pass
        finally:
            PromotionDataAccessors.close_session()
    
    def getPromotionById(self, promotionId):
        '''
        Parameters:
        - promotionId
        '''
        try:
            pass
        finally:
            PromotionDataAccessors.close_session()
    
    def generateCouponsForPromotion(self, promotionId, couponCode):
        '''
        Parameters:
        - promotionId
        - couponCode
        '''
        try:
            generate_coupons_for_promotion(promotionId, couponCode)
        finally:
            PromotionDataAccessors.close_session()
    
    def applyCoupon(self, couponCode, cartId):
        '''
        Parameters:
        - couponCode
        - cartId
        '''
        try:
            print 'Calling apply_coupon'
            return apply_coupon(couponCode, cartId)
        finally:
            PromotionDataAccessors.close_session()

    def trackCouponUsage(self, couponCode, transactionId, userId):
        '''
        Parameters:
        - couponCode
        - transactionId
        - userId
        '''
        try:
            track_coupon_usage(couponCode, transactionId, userId)
        finally:
            PromotionDataAccessors.close_session()
    
    def getCouponUsageCountByUser(self, couponCode, userId):
        '''
        Parameters:
        - couponCode
        - userId
        '''
        try:
            return get_coupon_usage_count_by_user(couponCode, userId)
        finally:
            PromotionDataAccessors.close_session()
    
    def getActiveCoupons(self):
        '''
        Returns a list of active coupons
        '''
        try:
            return [to_t_coupon(coupon) for coupon in get_active_coupons()]
        finally:
            PromotionDataAccessors.close_session()
    
    def getSuccessfulPaymentCountForCoupon(self, couponCode):
        '''
        Returns the count of successful payments done using this coupon
        
        Parameters:
        - couponCode
        '''
        try:
            return get_successful_payment_count_for_coupon(couponCode)
        finally:
            PromotionDataAccessors.close_session()
    
    def getRuleDocString(self, ruleName):
        '''
        Returns the doc string of the rule module
        
        Parameters:
        - ruleName
        '''
        try:
            return get_rule_doc_string(ruleName)
        finally:
            PromotionDataAccessors.close_session()

    def getItemDiscountMap(self, itemIds):
        '''
        Returns 
        '''
        try:
            return [to_t_item_coupon_discount(item_coupon_discount) for item_coupon_discount in get_item_discount_map(itemIds)]
        finally:
            PromotionDataAccessors.close_session()
    
    def getDiscountsForEntity(self, entityId):
        '''
        Returns the tuple containing discount coupon and discount value applicable for this entity
        
        Parameters:
         - entityId
        '''
        try:
            return get_discounts_for_entity(entityId)
        finally:
            PromotionDataAccessors.close_session()
    
    def isAlive(self):
        """
        For checking weather service is active alive or not. It also checks connectivity with database
        """
        try:
            return is_alive()
        finally:
            PromotionDataAccessors.close_session()