Ejemplo n.º 1
0
 def get_billing_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
     """
     # Try to get the cached version first.
     form = getattr(self, '_billing_form', None)
     initial = {'name':get_user_name_from_request(self.request)}
     if not form:
         
         # Create a dynamic Form class for the model specified as the address model
         form_class = model_forms.modelform_factory(AddressModel,
                                                    exclude=['user_shipping', 'user_billing'])
         if self.request.method == "POST":
             form = form_class(self.request.POST, prefix="bill")
         else:
             # Try to get a shipping address instance from the request (user or session))
             billing_address = get_billing_address_from_request(self.request)
             # We should either have an instance, or None
             if not billing_address:
                 # The user or guest doesn't already have a favorite address.
                 # Instansiate a blank one, and use this as the default value for
                 # the form.
                 billing_address = AddressModel()
                 # Make our new address the default for the User or Guest.
                 assign_address_to_request(self.request, billing_address, shipping=False)
                 
             form = form_class(instance=billing_address, prefix="bill", initial=initial)
             
         
         setattr(self, '_billing_form', form)
         
     return form
Ejemplo 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)
     initial = {'name':get_user_name_from_request(self.request)}
     if not form:
         # Create a dynamic Form class for the model specified as the address model
         form_class = self.get_shipping_form_class()
         
         if self.request.method == "POST":
             form = form_class(self.request.POST, prefix="ship")
         else:
             # Try to get a shipping address instance from the request (user or session))
             shipping_address = get_shipping_address_from_request(self.request)
             # 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()
                 # Make our new address the default for the User or Guest.
                 assign_address_to_request(self.request, shipping_address, shipping=True)
                 
             form = form_class(instance=shipping_address, prefix="ship", initial=initial)
         setattr(self, '_shipping_form', form)
     return form