Esempio n. 1
0
def login_event_handler(website_obj=None):
    """This method is triggered when a login event occurs.

    When a user logs in, all items in his guest cart should be added to his
    logged in or registered cart. If there is no such cart, it should be
    created.

    .. versionchanged:: 2.4.0.1
        website_obj was previously a mandatory argument because the pool
        object in the class was required to load other objects from the pool.
        Since pool object is a singleton, this object is not required.
    """

    if website_obj is not None:
        warnings.warn(
            "login_event_handler will not accept arguments from "
            "Version 2.5 +", DeprecationWarning, stacklevel=2
        )

    try:
        Cart = Pool().get('nereid.cart')
    except KeyError:
        # Just return silently. This KeyError is cause if the module is not
        # installed for a specific database but exists in the python path
        # and is loaded by the tryton module loader
        current_app.logger.warning(
            "nereid-cart-b2c module installed but not in database"
        )
        return

    # Find the guest cart
    try:
        guest_cart, = Cart.search([
            ('sessionid', '=', session.sid),
            ('user', '=', request.nereid_website.guest_user.id),
            ('website', '=', request.nereid_website.id)
        ], limit=1)
    except ValueError:
        return

    # There is a cart
    if guest_cart.sale and guest_cart.sale.lines:
        to_cart = Cart.open_cart(True)
        # Transfer lines from one cart to another
        for from_line in guest_cart.sale.lines:
            Cart._add_or_update(
                to_cart.sale.id, from_line.product.id, from_line.quantity
            )

    # Clear and delete the old cart
    guest_cart._clear_cart()