Exemplo n.º 1
0
    def dispatch(self, request, *args, **kwargs):
        # Assign the checkout session manager so it's available in all checkout
        # views.
        self.checkout_session = CheckoutSessionData(request)

        # Enforce any pre-conditions for the view.
        try:
            self.check_pre_conditions(request)
        except FailedPreCondition as e:
            for message in e.messages:
                messages.warning(request, message)
            if request.is_ajax():
                return http.JsonResponse({'url': e.url})
            else:
                return http.HttpResponseRedirect(e.url)

        # Check if this view should be skipped
        try:
            self.check_skip_conditions(request)
        except PassedSkipCondition as e:
            if request.is_ajax():
                return http.JsonResponse({'url': e.url})
            else:
                return http.HttpResponseRedirect(e.url)

        return super(ShippingAddressView,
                     self).dispatch(request, *args, **kwargs)
Exemplo n.º 2
0
def place_order(sender, **kwargs):
    """ collect basket, user, shipping_method and address, order_number, total
    and pass them to handle_order_placement, but first add payment events and
    sources """
    request = kwargs.get('request', None) or HttpRequest()
    basket = sender
    user = basket.owner if basket.owner else AnonymousUser()
    guest_email = None

    strategy = selector.strategy(user=user)
    session_data = shipping_address = shipping_method = None
    log.debug("initialising: \n basket = %s \n usr = %s \n strategy = %s",
            basket, user, strategy)
    basket.strategy = strategy
    amount_allocated = kwargs['OutSum']
    session_key  = kwargs['session_key']
    order_num =    kwargs['order_num']
    if session_key is not None:
        session = SessionStore(session_key = session_key)
        if len(session.items()):
            log.debug("Session %s successfully restored", session)
            request.session = session
            request.user = user
            session_data = CheckoutSessionData(request)
            if isinstance(user, AnonymousUser):
                guest_email = session_data.get_guest_email()

    order_placement = RobokassaOrderPlacement()
    order_placement.request = request
    if session_data is not None:
        order_placement.checkout_session = session_data
        shipping_address = order_placement.get_shipping_address(basket)
        shipping_method = order_placement.get_shipping_method(
                basket, shipping_address)
        total = order_placement.get_order_totals(basket, shipping_method)
    else:  # session not found, lets try to place order anyway
        log.error(("Session was not restored, trying default order for "
                    "basket #%s"), basket.id)
        basket.is_shipping_required = False
        total = prices.Price(
            currency=basket.currency,
            excl_tax=basket.total_excl_tax, incl_tax=basket.total_incl_tax)

    # now create payment source and events
    source_type, is_created = SourceType.objects.get_or_create(
                name=u'Робокасса', code='robokassa')
    source = Source(source_type=source_type, amount_allocated=amount_allocated,
                amount_debited=amount_allocated)
    order_placement.add_payment_source(source)
    order_placement.add_payment_event('allocated', amount_allocated)
    order_placement.add_payment_event('debited', amount_allocated)
    post_payment.send(sender=order_placement, user=user, source=source)

    # all done lets place an order
    order_placement.handle_order_placement(
                order_num, user, basket, shipping_address, shipping_method,
                total, guest_email=guest_email)
def csdf(rf):
    """"""
    request = rf.get('/')
    middleware = SessionMiddleware()
    middleware.process_request(request)
    return CheckoutSessionData(request)
Exemplo n.º 4
0
class TestCheckoutSession(TestCase):
    """
    oscar.apps.checkout.utils.CheckoutSessionData
    """

    def setUp(self):
        request = RequestFactory().get('/')
        SessionMiddleware().process_request(request)
        self.session_data = CheckoutSessionData(request)

    def test_allows_data_to_be_written_and_read_out(self):
        self.session_data._set('namespace', 'key', 'value')
        self.assertEqual('value', self.session_data._get('namespace', 'key'))

    def test_allows_set_data_can_be_unset(self):
        self.session_data._set('namespace', 'key', 'value')
        self.session_data._unset('namespace', 'key')
        self.assertIsNone(self.session_data._get('namespace', 'key'))

    def test_stores_guest_email(self):
        self.session_data.set_guest_email('*****@*****.**')
        self.assertEqual('*****@*****.**', self.session_data.get_guest_email())

    def test_allows_a_namespace_to_be_flushed(self):
        self.session_data._set('ns', 'a', 1)
        self.session_data._set('ns', 'b', 2)
        self.session_data._flush_namespace('ns')
        self.assertIsNone(self.session_data._get('ns', 'a'))
        self.assertIsNone(self.session_data._get('ns', 'b'))

    def test_allows_bill_to_user_address(self):
        address = mock.Mock()
        address.id = 1
        self.session_data.bill_to_user_address(address)
        self.assertEqual(1, self.session_data.billing_user_address_id())
Exemplo n.º 5
0
 def setUp(self):
     request = RequestFactory().get('/')
     SessionMiddleware().process_request(request)
     self.session_data = CheckoutSessionData(request)
Exemplo n.º 6
0
class TestCheckoutSession(TestCase):
    """
    oscar.apps.checkout.utils.CheckoutSessionData
    """

    def setUp(self):
        request = RequestFactory().get('/')
        SessionMiddleware().process_request(request)
        self.session_data = CheckoutSessionData(request)

    def test_allows_data_to_be_written_and_read_out(self):
        self.session_data._set('namespace', 'key', 'value')
        self.assertEqual('value', self.session_data._get('namespace', 'key'))

    def test_allows_set_data_can_be_unset(self):
        self.session_data._set('namespace', 'key', 'value')
        self.session_data._unset('namespace', 'key')
        self.assertIsNone(self.session_data._get('namespace', 'key'))

    def test_stores_guest_email(self):
        self.session_data.set_guest_email('*****@*****.**')
        self.assertEquals('*****@*****.**', self.session_data.get_guest_email())

    def test_allows_a_namespace_to_be_flushed(self):
        self.session_data._set('ns', 'a', 1)
        self.session_data._set('ns', 'b', 2)
        self.session_data._flush_namespace('ns')
        self.assertIsNone(self.session_data._get('ns', 'a'))
        self.assertIsNone(self.session_data._get('ns', 'b'))

    def test_allows_bill_to_user_address(self):
        address = mock.Mock()
        address.id = 1
        self.session_data.bill_to_user_address(address)
        self.assertEqual(1, self.session_data.billing_user_address_id())
Exemplo n.º 7
0
 def setUp(self):
     request = RequestFactory().get('/')
     SessionMiddleware().process_request(request)
     self.session_data = CheckoutSessionData(request)
Exemplo n.º 8
0
def place_order(sender, **kwargs):
    """ collect basket, user, shipping_method and address, order_number, total
    and pass them to handle_order_placement, but first add payment events and
    sources """
    request = kwargs.get('request', None) or HttpRequest()
    basket = sender
    user = basket.owner if basket.owner else AnonymousUser()
    guest_email = None

    strategy = selector.strategy(user=user)
    session_data = shipping_address = shipping_method = None
    log.debug("initialising: \n basket = %s \n usr = %s \n strategy = %s",
              basket, user, strategy)
    basket.strategy = strategy
    amount_allocated = kwargs['OutSum']
    session_key = kwargs['session_key']
    order_num = kwargs['order_num']
    if session_key is not None:
        session = SessionStore(session_key=session_key)
        if len(session.items()):
            log.debug("Session %s successfully restored", session)
            request.session = session
            request.user = user
            session_data = CheckoutSessionData(request)
            if isinstance(user, AnonymousUser):
                guest_email = session_data.get_guest_email()

    order_placement = RobokassaOrderPlacement()
    order_placement.request = request
    if session_data is not None:
        order_placement.checkout_session = session_data
        shipping_address = order_placement.get_shipping_address(basket)
        shipping_method = order_placement.get_shipping_method(
            basket, shipping_address)
        total = order_placement.get_order_totals(basket, shipping_method)
    else:  # session not found, lets try to place order anyway
        log.error(("Session was not restored, trying default order for "
                   "basket #%s"), basket.id)
        basket.is_shipping_required = False
        total = prices.Price(currency=basket.currency,
                             excl_tax=basket.total_excl_tax,
                             incl_tax=basket.total_incl_tax)

    # now create payment source and events
    source_type, is_created = SourceType.objects.get_or_create(
        name=u'Робокасса', code='robokassa')
    source = Source(source_type=source_type,
                    amount_allocated=amount_allocated,
                    amount_debited=amount_allocated)
    order_placement.add_payment_source(source)
    order_placement.add_payment_event('allocated', amount_allocated)
    order_placement.add_payment_event('debited', amount_allocated)
    post_payment.send(sender=order_placement, user=user, source=source)

    # all done lets place an order
    order_placement.handle_order_placement(order_num,
                                           user,
                                           basket,
                                           shipping_address,
                                           shipping_method,
                                           total,
                                           guest_email=guest_email)
Exemplo n.º 9
0
class ShippingAddressView(checkout_views.ShippingAddressView):
    def dispatch(self, request, *args, **kwargs):
        # Assign the checkout session manager so it's available in all checkout
        # views.
        self.checkout_session = CheckoutSessionData(request)

        # Enforce any pre-conditions for the view.
        try:
            self.check_pre_conditions(request)
        except FailedPreCondition as e:
            for message in e.messages:
                messages.warning(request, message)
            if request.is_ajax():
                return http.JsonResponse({'url': e.url})
            else:
                return http.HttpResponseRedirect(e.url)

        # Check if this view should be skipped
        try:
            self.check_skip_conditions(request)
        except PassedSkipCondition as e:
            if request.is_ajax():
                return http.JsonResponse({'url': e.url})
            else:
                return http.HttpResponseRedirect(e.url)

        return super(ShippingAddressView,
                     self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        address_fields = dict((k, v)
                              for (k, v) in form.instance.__dict__.items()
                              if not k.startswith('_'))
        self.checkout_session.ship_to_new_address(address_fields)
        url = self.get_success_url()
        if self.request.is_ajax():
            return http.JsonResponse({'url': url})
        else:
            return redirect(url)

    def get_template_names(self):
        if self.request.is_ajax():
            template_name = 'checkout/shipping_address_ajax.html'
        else:
            template_name = 'checkout/shipping_address.html'
        return [template_name]

    def get_context_data(self, **kwargs):

        context = super(ShippingAddressView, self).get_context_data(**kwargs)
        method = self.get_default_shipping_method(self.request.basket)
        shipping_charge = method.calculate(self.request.basket)
        context['shipping_charge'] = shipping_charge
        context['order_total'] = OrderTotalCalculator().calculate(
            self.request.basket, shipping_charge)
        return context

    def get_default_shipping_method(self, basket):
        return Repository().get_default_shipping_method(
            basket=self.request.basket,
            user=self.request.user,
            request=self.request)

    def get_initial(self):

        initial = self.checkout_session.new_shipping_address_fields()
        if initial:
            initial = initial.copy()
            # Convert the primary key stored in the session into a Country
            # instance
            try:
                initial['country'] = Country.objects.get(
                    iso_3166_1_a2=initial.pop('country_id'))
            except Country.DoesNotExist:
                # Hmm, the previously selected Country no longer exists. We
                # ignore this.
                pass
        if not initial:
            address = self.get_available_addresses().first()
            if address:
                initial = model_to_dict(address)

        return initial