Created
August 29, 2012 07:32
-
-
Save tejastank/3507998 to your computer and use it in GitHub Desktop.
OpenERP : Get i18n format address with your form view.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# | |
# | |
# BELOW FUNCTION FEATURE ADD IN `res.country` CLASS, WHICH REFORMAT ADDRESS AS PER COUNTRY ADDRESS FORMAT. | |
def fields_view_address_formate(self, cr, uid, context=None): | |
country_id = self.pool.get('res.users').browse(cr, uid, uid,context).company_id.country_id | |
args = {} | |
if country_id and country_id.address_format: | |
address_format = country_id.address_format | |
else: | |
address_format = '%(street)s\n%(street2)s\n%(city)s, %(state_code)s %(zip)s\n%(country_name)s' | |
address_format_fields = address_format | |
address_format = address_format.replace(',','') | |
for symbol in ['%(', ')s', ',']: | |
address_format_fields = address_format_fields.replace(symbol,' ') | |
address_list = address_format_fields.split() | |
for field in address_list: | |
args[field] = '' | |
args_val = { | |
'street': '<field name="street" placeholder="Street"/>', | |
'street2': '<field name="street2" placeholder="Street2"/>', | |
'city': '<field name="city" placeholder="City"/>', | |
'state_name': '<field name="state_id" placeholder="State"/>', | |
'zip': '<field name="zip" placeholder="Zip"/>', | |
'country_name': '<field name="country_id" placeholder="Country"/>', | |
} | |
args.update(args_val) | |
address_fields = { | |
'zip': {'change_default': True, 'string': 'Zip', 'views': {}, 'selectable': True, 'type': 'char', 'size': 24}, | |
'city': {'selectable': True, 'views': {}, 'type': 'char', 'string': 'City', 'size': 128}, | |
'street': {'selectable': True, 'views': {}, 'type': 'char', 'string': 'Street', 'size': 128}, | |
'street2': {'selectable': True, 'views': {}, 'type': 'char', 'string': 'Street2', 'size': 128}, | |
'country_id': {'domain': [], 'string': 'Country', 'views': {}, 'relation': 'res.country', 'context': {}, 'selectable': True, 'type': 'many2one'}, | |
'state_id': {'domain': [], 'string': 'State', 'views': {}, 'relation': 'res.country.state', 'context': {}, 'selectable': True, 'type': 'many2one'} | |
} | |
arch = address_format % args | |
return {'arch': arch, 'fields': address_fields} | |
# | |
# | |
# | |
# TO USE ABOVE FEATURE, LEARN BELOW EXAMPLE. | |
# In form view of `res.partner` first remove other fields like street, city, state, pincode, country. and add below code. | |
<div class="address_format"/> | |
# | |
# Now changes in res_partner.py file. | |
# | |
def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False): | |
res = super(res_partner,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu) | |
if view_type == 'form': | |
fields_view = self.pool.get('res.country').fields_view_address_formate(cr, uid, context=context) | |
address_fields = fields_view['fields'] | |
address_xml = fields_view['arch'] | |
old = '<div class="address_format"/>' | |
new = '<div class="address_format">' + address_xml + '</div>' | |
res['arch'] = res['arch'].replace(old,new) | |
res['fields'].update(address_fields) | |
return res | |
# This will render user's company's country address_format | |
# So as per localization, address format will come. | |
# res.country hold address format of each country. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment