<s> from django import http <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core import urlresolvers <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from django . utils . translation import ugettext <EOL> from satchmo_store . contact import signals , CUSTOMER_ID <EOL> from satchmo_store . contact . forms import ExtendedContactInfoForm , ContactInfoForm , area_choices_for_country <EOL> from satchmo_store . contact . models import Contact <EOL> from satchmo_store . shop . models import Config <EOL> import logging <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def view ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user_data = Contact . objects . get ( user = request . user . id ) <EOL> except Contact . DoesNotExist : <EOL> user_data = None <EOL> contact_dict = { <EOL> '<STR_LIT>' : user_data , <EOL> } <EOL> signals . satchmo_contact_view . send ( user_data , contact = user_data , contact_dict = contact_dict ) <EOL> context = RequestContext ( request , contact_dict ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> context_instance = context ) <EOL> view = login_required ( view ) <EOL> def update ( request ) : <EOL> """<STR_LIT>""" <EOL> init_data = { } <EOL> shop = Config . objects . get_current ( ) <EOL> try : <EOL> contact = Contact . objects . from_request ( request , create = False ) <EOL> except Contact . DoesNotExist : <EOL> contact = None <EOL> if request . method == "<STR_LIT:POST>" : <EOL> new_data = request . POST . copy ( ) <EOL> form = ExtendedContactInfoForm ( data = new_data , shop = shop , contact = contact , shippable = True , <EOL> initial = init_data ) <EOL> if form . is_valid ( ) : <EOL> if contact is None and request . user : <EOL> contact = Contact ( user = request . user ) <EOL> custID = form . save ( contact = contact ) <EOL> request . session [ CUSTOMER_ID ] = custID <EOL> redirect_to = request . REQUEST . get ( REDIRECT_FIELD_NAME , '<STR_LIT>' ) <EOL> if not redirect_to or '<STR_LIT>' in redirect_to or '<STR_LIT:U+0020>' in redirect_to : <EOL> redirect_to = urlresolvers . reverse ( '<STR_LIT>' ) <EOL> return http . HttpResponseRedirect ( redirect_to ) <EOL> else : <EOL> signals . satchmo_contact_view . send ( contact , contact = contact , contact_dict = init_data ) <EOL> else : <EOL> if contact : <EOL> for item in contact . __dict__ . keys ( ) : <EOL> init_data [ item ] = getattr ( contact , item ) <EOL> if contact . shipping_address : <EOL> for item in contact . shipping_address . __dict__ . keys ( ) : <EOL> init_data [ "<STR_LIT>" + item ] = getattr ( contact . shipping_address , item ) <EOL> if contact . billing_address : <EOL> for item in contact . billing_address . __dict__ . keys ( ) : <EOL> init_data [ item ] = getattr ( contact . billing_address , item ) <EOL> if contact . primary_phone : <EOL> init_data [ '<STR_LIT>' ] = contact . primary_phone . phone <EOL> if contact . organization : <EOL> init_data [ '<STR_LIT>' ] = contact . organization . name <EOL> signals . satchmo_contact_view . send ( contact , contact = contact , contact_dict = init_data ) <EOL> form = ExtendedContactInfoForm ( shop = shop , contact = contact , shippable = True , initial = init_data ) <EOL> init_data [ '<STR_LIT>' ] = form <EOL> if shop . in_country_only : <EOL> init_data [ '<STR_LIT>' ] = shop . sales_country <EOL> else : <EOL> countries = shop . countries ( ) <EOL> if countries and countries . count ( ) == <NUM_LIT:1> : <EOL> init_data [ '<STR_LIT>' ] = countries [ <NUM_LIT:0> ] <EOL> init_data [ '<STR_LIT>' ] = request . REQUEST . get ( REDIRECT_FIELD_NAME , '<STR_LIT>' ) <EOL> context = RequestContext ( request , init_data ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> context_instance = context ) <EOL> update = login_required ( update ) <EOL> class AjaxGetStateException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message ) : <EOL> self . message = message <EOL> def ajax_get_state ( request , ** kwargs ) : <EOL> formdata = request . REQUEST . copy ( ) <EOL> try : <EOL> if formdata . has_key ( "<STR_LIT>" ) : <EOL> country_field = '<STR_LIT>' <EOL> elif formdata . has_key ( "<STR_LIT>" ) : <EOL> country_field = '<STR_LIT>' <EOL> else : <EOL> raise AjaxGetStateException ( "<STR_LIT>" ) <EOL> form = ContactInfoForm ( data = formdata ) <EOL> country_data = formdata . get ( country_field ) <EOL> try : <EOL> country_obj = form . fields [ country_field ] . clean ( country_data ) <EOL> except : <EOL> raise AjaxGetStateException ( "<STR_LIT>" ) <EOL> areas = area_choices_for_country ( country_obj , ugettext ) <EOL> context = RequestContext ( request , { <EOL> '<STR_LIT>' : areas , <EOL> } ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> context_instance = context ) <EOL> except AjaxGetStateException , e : <EOL> log . error ( "<STR_LIT>" % e . message ) <EOL> return http . HttpResponseServerError ( ) </s>
