<s> from django . shortcuts import redirect <EOL> from django . template . response import TemplateResponse <EOL> from . . forms import AnonymousUserShippingForm , ShippingAddressesForm <EOL> from ... userprofile . forms import AddressForm <EOL> from ... userprofile . models import Address <EOL> def anonymous_user_shipping_address_view ( request , checkout ) : <EOL> data = request . POST or None <EOL> shipping_address = checkout . shipping_address <EOL> if shipping_address is not None : <EOL> address_form = AddressForm ( <EOL> data , instance = checkout . shipping_address , autocomplete_type = '<STR_LIT>' ) <EOL> else : <EOL> address_form = AddressForm ( <EOL> data , autocomplete_type = '<STR_LIT>' , initial = { '<STR_LIT>' : request . country } ) <EOL> user_form = AnonymousUserShippingForm ( data , initial = { '<STR_LIT:email>' : checkout . email } ) <EOL> if user_form . is_valid ( ) and address_form . is_valid ( ) : <EOL> checkout . shipping_address = address_form . instance <EOL> checkout . email = user_form . cleaned_data [ '<STR_LIT:email>' ] <EOL> return redirect ( '<STR_LIT>' ) <EOL> return TemplateResponse ( <EOL> request , '<STR_LIT>' , context = { <EOL> '<STR_LIT>' : address_form , '<STR_LIT>' : user_form , '<STR_LIT>' : checkout } ) <EOL> def user_shipping_address_view ( request , checkout ) : <EOL> data = request . POST or None <EOL> additional_addresses = request . user . addresses . all ( ) <EOL> checkout . email = request . user . email <EOL> shipping_address = checkout . shipping_address <EOL> if shipping_address is not None and shipping_address . id : <EOL> address_form = AddressForm ( <EOL> data , autocomplete_type = '<STR_LIT>' , initial = { '<STR_LIT>' : request . country } ) <EOL> addresses_form = ShippingAddressesForm ( <EOL> data , additional_addresses = additional_addresses , <EOL> initial = { '<STR_LIT:address>' : shipping_address . id } ) <EOL> elif shipping_address : <EOL> address_form = AddressForm ( data , instance = shipping_address ) <EOL> addresses_form = ShippingAddressesForm ( <EOL> data , additional_addresses = additional_addresses ) <EOL> else : <EOL> address_form = AddressForm ( data , initial = { '<STR_LIT>' : request . country } ) <EOL> addresses_form = ShippingAddressesForm ( <EOL> data , additional_addresses = additional_addresses ) <EOL> if addresses_form . is_valid ( ) : <EOL> if addresses_form . cleaned_data [ '<STR_LIT:address>' ] != ShippingAddressesForm . NEW_ADDRESS : <EOL> address_id = addresses_form . cleaned_data [ '<STR_LIT:address>' ] <EOL> checkout . shipping_address = Address . objects . get ( id = address_id ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> elif address_form . is_valid ( ) : <EOL> checkout . shipping_address = address_form . instance <EOL> return redirect ( '<STR_LIT>' ) <EOL> return TemplateResponse ( <EOL> request , '<STR_LIT>' , context = { <EOL> '<STR_LIT>' : address_form , '<STR_LIT>' : addresses_form , <EOL> '<STR_LIT>' : checkout , '<STR_LIT>' : additional_addresses } ) </s>
