Esempio n. 1
0
    def get_shipping_address_form(self):
        """
        Initializes and handles the form for the shipping address.
        AddressModel is a model of the type defined in settings.SHOP_ADDRESS_MODEL.

        The trick here is that we generate a ModelForm for whatever model was
        passed to us by the SHOP_ADDRESS_MODEL setting, and us this, prefixed, as
        the shipping address form. So this can be as complex or as simple as one wants.

        Subclasses of this view can obviously override this method and return any
        other form instead.
        """
        # Try to get the cached version first.
        form = getattr(self, "_shipping_form", None)
        if not form:
            # Create a dynamic Form class for the model specified as the address model
            form_class = self.get_shipping_form_class()

            # Try to get a shipping address instance from the request (user or session))
            shipping_address = get_shipping_address_from_request(self.request)
            if self.request.method == "POST":
                form = form_class(self.request.POST, prefix="ship", instance=shipping_address)
            else:
                # We should either have an instance, or None
                if not shipping_address:
                    # The user or guest doesn't already have a favorite address.
                    # Instanciate a blank one, and use this as the default value for
                    # the form.
                    shipping_address = AddressModel()

                # Instanciate the form
                form = form_class(instance=shipping_address, prefix="ship")
            setattr(self, "_shipping_form", form)
        return form
Esempio n. 2
0
    def get_shipping_address_form(self):
        """
        Initializes and handles the form for the shipping address.
        AddressModel is a model of the type defined in settings.SHOP_ADDRESS_MODEL.

        The trick here is that we generate a ModelForm for whatever model was
        passed to us by the SHOP_ADDRESS_MODEL setting, and us this, prefixed, as
        the shipping address form. So this can be as complex or as simple as one wants.

        Subclasses of this view can obviously override this method and return any
        other form instead.
        """
        # Try to get the cached version first.
        form = getattr(self, '_shipping_form', None)
        if not form:
            # Create a dynamic Form class for the model specified as the address model
            form_class = self.get_shipping_form_class()

            # Try to get a shipping address instance from the request (user or session))
            shipping_address = get_shipping_address_from_request(self.request)
            if self.request.method == "POST":
                form = form_class(self.request.POST, prefix="ship", instance=shipping_address)
            else:
                # We should either have an instance, or None
                if not shipping_address:
                    # The user or guest doesn't already have a favorite address.
                    # Instanciate a blank one, and use this as the default value for
                    # the form.
                    shipping_address = AddressModel()
                
                # Instanciate the form
                form = form_class(instance=shipping_address, prefix="ship")
            setattr(self, '_shipping_form', form)
        return form
Esempio n. 3
0
    def addresses_form(self):
        """Create an form which handles selecting the two addresses."""
        data = None
        if self.request.method == "POST":
            data = self.request.POST

        return AddressesForm(
            data,
            billing=get_billing_address_from_request(self.request),
            shipping=get_shipping_address_from_request(self.request),
            billing_form_class=self.get_billing_form_class(),
            shipping_form_class=self.get_shipping_form_class(),
        )
Esempio n. 4
0
 def test_get_shipping_address_from_request_with_preset_and_session(self):
     assign_address_to_request(self.request, self.address, shipping=True)
     res = get_shipping_address_from_request(self.request)
     self.assertEqual(res, self.address)
Esempio n. 5
0
 def test_get_shipping_address_from_request_with_preset_and_user(self):
     setattr(self.request, 'user', self.user)
     assign_address_to_request(self.request, self.address, shipping=True)
     res = get_shipping_address_from_request(self.request)
     self.assertEqual(res, self.address)
Esempio n. 6
0
 def test_get_shipping_address_from_request_no_preset(self):
     # Set the user
     setattr(self.request, 'user', self.user)
     res = get_shipping_address_from_request(self.request)
     self.assertEqual(res, None)
Esempio n. 7
0
 def test_get_shipping_address_from_request_with_preset_and_session(self):
     assign_address_to_request(self.request, self.address, shipping=True)
     res = get_shipping_address_from_request(self.request)
     self.assertEqual(res, self.address)
Esempio n. 8
0
 def test_get_shipping_address_from_request_with_preset_and_user(self):
     setattr(self.request, 'user', self.user)
     assign_address_to_request(self.request, self.address, shipping=True)
     res = get_shipping_address_from_request(self.request)
     self.assertEqual(res, self.address)
Esempio n. 9
0
 def test_get_shipping_address_from_request_no_preset(self):
     # Set the user
     setattr(self.request, 'user', self.user)
     res = get_shipping_address_from_request(self.request)
     self.assertEqual(res, None)
Esempio n. 10
0
 def get_zipcode(self, request, shipping_adress_form=False):
     if not shipping_adress_form:
         return get_shipping_address_from_request(request).zip_code
     else:
         return shipping_adress_form.data.get('ship-zip_code', None)