Subversion Repositories SmartDukaan

Rev

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

'''
Created on 28-Apr-2010

@author: ashish
'''
from shop2020.utils.Utils import log_entry
from shop2020.model.v1.user.impl.UserDataAccessors import create_user, update_user, get_user_by_id, update_password,\
    set_user_as_logged_out, set_user_as_logged_in, remove_address_for_user,\
    add_address_for_user, delete_user, authenticate_user, user_exists, get_user_state, initialize, set_default_address,\
    create_anonymous_user
from shop2020.model.v1.user.impl.CartDataAccessors import create_cart, get_cart,\
    get_cart_by_id, get_cart_by_user_id_and_status, get_carts_by_status,\
    get_carts_between, change_cart_status, add_item_to_cart,\
    change_item_quantity, change_item_status, add_address_to_cart, commit_cart,\
    validate_cart, merge_cart, delete_item_from_cart
from shop2020.model.v1.user.impl.Converters import to_t_user, to_t_user_state, to_t_cart
from shop2020.clients.TransactionClient import TransactionClient
from shop2020.thriftpy.model.v1.user.ttypes import ShoppingCartException

from shop2020.model.v1.user.impl.WidgetDataAccessor import add_widget,\
    add_items_to_widget, update_my_research, update_widget, get_widget_by_type,\
    get_my_research, get_ratings, update_ratings, get_browse_history,\
    update_browse_history, delete_item_from_my_research, delete_item_from_widget
from shop2020.model.v1.user.impl.WidgetConverters import to_t_widget, to_t_ratings
from shop2020.clients.InventoryClient import InventoryClient

class UserContextServiceHandler:
    
    
    def __init__(self):
        initialize()
        self.transaction_client = TransactionClient()
        self.inventory_client = InventoryClient()

    """
    service
    """
    def createAnonymousUser(self, jsessionId):
        cart = create_cart(-1)
        return to_t_user(create_anonymous_user(jsessionId, cart))
    
    def getUserById(self, userId):
        """
        Parameters:
        - userId
        """
        return to_t_user(get_user_by_id(userId))
    
    def createUser(self, user):
        """
        Parameters:
        - user
        """
        cart = create_cart(-1)
        return to_t_user(create_user(user, cart))
    
    def updateUser(self, user):
        """
        Parameters:
        - user
        """
        return to_t_user(update_user(user))
    
    def deleteUser(self, userId):
        """
        Parameters:
        - userId
        """
        return delete_user(userId)
    
    def getUserState(self, userId):
        """
        Parameters:
        - userId
        """
        log_entry(self, "get State")
        return to_t_user_state(get_user_state(userId))
    
    def authenticateUser(self, email, password):
        """
        Parameters:
        - email
        - password
        """
        log_entry(self, "authenticate user")
        return to_t_user(authenticate_user(email, password))
    
    def userExists(self, email):
        """
        Parameters:
        - email
        """
        log_entry(self, "user exists")
        return user_exists(email)
    
    def addAddressForUser(self, userId, address, timestamp, setDefault):
        """
        Parameters:
        - userId
        - address
        - timestamp
        - setDefault
        """
        log_entry(self, "add address for user")
        return add_address_for_user(address, userId, timestamp, setDefault)
    
    def removeAddressForUser(self, userid, addressId):
        """
        Parameters:
        - userid
        - addressId
        """
        log_entry(self, "removing address")
        return remove_address_for_user(userid, addressId)
    
    def setUserAsLoggedIn(self, userId, timestamp):
        """
        Parameters:
        - userId
        - timestamp
        """
        log_entry(self, "set_user_as_logged_in")
        return set_user_as_logged_in(userId, timestamp)
    
    def setUserAsLoggedOut(self, userId, timestamp):
        """
        Parameters:
        - userId
        - timestamp
        """
        log_entry(self, "set user as logged out")
        return set_user_as_logged_out(userId, timestamp)
    
    def setDefaultAddress(self, userId, addressId):
        """
        Parameters:
        - userId
        - addressId
        """
        return set_default_address(userId, addressId)
    
    def updatePassword(self, userId, password):
        """
        Parameters:
        - userId
        - password
        """
        log_entry(self, "updating password")
        return update_password(userId, password)


    def createCart(self, userId):
        """
        Parameters:
        - userId
        """
        return create_cart(userId).id

    def getCurrentCart(self, userId):
        """
        Parameters:
         - userId
        """
        cart = get_cart(userId)
        if cart:
            return to_t_cart(cart)
        else:
            raise ShoppingCartException(101, "not cart attached to this user")

    def getCart(self, cartId):
        """
        Parameters:
         - cartId
        """
        cart = get_cart_by_id(cartId)
        if cart:
            return to_t_cart(cart)
        else:
            raise ShoppingCartException(101, "not cart attached to this id")
    
    def getCartsForUser(self, userId, status):
        """
        Parameters:
         - userId
         - status
        """
        if not userId:
            raise ShoppingCartException(101, "user_id is not provided")
        
        carts = get_cart_by_user_id_and_status(userId, status)
        t_carts = []
        if carts:
            for cart in carts:
                t_carts.append(to_t_cart(cart))
        return t_carts
    
    def getCartsByStatus(self, status):
        """
        Parameters:
         - status
        """
        carts = get_carts_by_status(status)
        t_carts = []
        if carts:
            for cart in carts:
                t_carts.append(to_t_cart(cart))
        return t_carts
    
    def getCartsByTime(self, from_time, to_time, status):
        """
        Parameters:
         - from_time
         - to_time
         - status
        """
        carts = get_carts_between(from_time, to_time, status)
        t_carts = []
        if carts:
            for cart in carts:
                t_carts.append(to_t_cart(cart))
        return t_carts
    
    def changeCartStatus(self, cartId, status):
        """
        Parameters:
         - cartId
         - status
        """
        change_cart_status(cartId, status)
    
    def addItemToCart(self, cartId, itemId, quantity):
        """
        Parameters:
         - cartId
         - itemId
         - quantity
        """
        return add_item_to_cart(cartId, itemId, quantity)
    
    def deleteItemFromCart(self, cartId, itemId):
        """
        Parameters:
         - cartId
         - itemId
        """
        delete_item_from_cart(cartId, itemId)

    def changeQuantity(self, cartId, itemId, quantity):
        """
        Parameters:
         - cartId
         - itemId
         - quantity
        """
        return change_item_quantity(cartId, itemId, quantity)
    
    def changeItemStatus(self, cartId, itemId, status):
        """
        Parameters:
         - cartId
         - itemId
        """
        return change_item_status(cartId, itemId, status)
    
    def addAddressToCart(self, cartId, addressId):
        """
        Parameters:
         - cartId
         - addressId
        """
        return add_address_to_cart(cartId, addressId)
    
    def commitCart(self, cartId):
        """
        Parameters:
         - cartId
        """
        return commit_cart(cartId, self.transaction_client)
    
    def validateCart(self, cartId):
        """
        Parameters:
         - cartId
        """
        return validate_cart(cartId, self.inventory_client)
    
    def mergeCart(self, fromCartId, toCartId):
        """
        Parameters:
         - fromCartId
         - toCartId
        """
        merge_cart(fromCartId, toCartId)

    def addWidget(self, widget):
        """
        Parameters:
         - widget
        """
        add_widget(widget)
    
    def addItemToWidget(self, widget_id, items):
        """
        Parameters:
         - widget_id
         - items
        """
        add_items_to_widget(widget_id, items)
    
    def updateMyResearch(self, user_id, item_id):
        """
        Parameters:
         - user_id
         - item_id
        """
        return update_my_research(user_id, item_id)
    
    def updateWidget(self, widgetId, enable):
        """
        Parameters:
         - widgetId
         - enable
        """
        update_widget(widgetId, enable)
    
    def updateWidgetItem(self, widgetId, enable):
        """
        Parameters:
         - widgetId
         - enable
        """
        update_widget(widgetId, enable)
        
    def deleteItemFromWidget(self, widget_id, item_id):
        """
        Parameters:
         - widget_id
         - item_id
        """
        return delete_item_from_widget(widget_id, item_id)
    
    def deleteItemFromMyResearch(self, user_id, item_id):
        """
        Parameters:
         - user_id
         - item_id
        """
        return delete_item_from_my_research(user_id, item_id)
    
    def getWidget(self, type, userId, onlyEnabled):
        """
        Parameters:
         - type
         - userId
         - onlyEnabled
        """
        widget = get_widget_by_type(type, userId, onlyEnabled)
        return to_t_widget(widget)
    
    def getMyResearch(self, user_id):
        """
        Parameters:
         - user_id
        """
        return to_t_widget(get_my_research(user_id))
    
    def updateRatings(self, item_id, type, rating, user_id):
        """
        Parameters:
        - item_id
        - type
        - rating
        - user_id
        """
        update_ratings(user_id, item_id, rating, type)
        
            
    def getRatings(self, item_id, user_id):
        """
        Parameters:
        - item_id
        - user_id
        """
        all_ratings, user_ratings = get_ratings(user_id, item_id)
        return to_t_ratings(user_ratings, all_ratings, item_id, user_id)

    def updateBrowseHistory(self, user_id, item_id, is_session_id):
        """
        Parameters: 
        - user_id
        - item_id
        - isSessionId
        """
        update_browse_history(user_id, item_id, is_session_id)
        
    def getBrowseHistory(self, user_id, is_session_id):
        """
        Parameters:
         - user_id
         - is_session_id
        """
        return to_t_widget(get_browse_history(user_id, is_session_id))