forked from openlabs/magento_integration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
country.py
138 lines (119 loc) · 4.19 KB
/
country.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
"""
country
Country
:copyright: (c) 2013 by Openlabs Technologies & Consulting (P) Limited
:license: AGPLv3, see LICENSE for more details.
"""
from openerp.osv import osv
from openerp.tools.translate import _
import pycountry
class Country(osv.osv):
"Country"
_inherit = 'res.country'
def search_using_magento_code(self, cursor, user, code, context):
"""
Searches for country with given magento code.
:param cursor: Database cursor
:param user: ID of current user
:param code: ISO code of country
:param context: Application context
:return: Browse record of country if found else raises error
"""
country_ids = self.search(
cursor, user, [('code', '=', code)], context=context
)
if not country_ids:
raise osv.except_osv(
_('Not Found!'),
_('Country with ISO code %s does not exists.' % code)
)
country = self.browse(
cursor, user, country_ids[0], context=context
)
return country
class CountryState(osv.Model):
"Country State"
_inherit = 'res.country.state'
def find_or_create_using_magento_region(
self, cursor, user, country, region, context
):
"""
Looks for the state whose `region` is sent by magento in `country`
If state already exists, return that else create a new one and
return
:param cursor: Database cursor
:param user: ID of current user
:param country: Browse record of country
:param region: Name of state from magento
:param context: Application context
:return: Browse record of record created/found
"""
state = self.find_using_magento_region(
cursor, user, country, region, context
)
if not state:
state = self.create_using_magento_region(
cursor, user, country, region, context
)
return state
def find_using_magento_region(
self, cursor, user, country, region, context
):
"""
Looks for the state whose `region` is sent by magento
If state already exists, return that
:param cursor: Database cursor
:param user: ID of current user
:param country: Browse record of country
:param region: Name of state from magento
:param context: Application context
:return: Browse record of record found
"""
state_ids = self.search(
cursor, user, [
('name', 'ilike', region),
('country_id', '=', country.id),
], context=context
)
return state_ids and self.browse(
cursor, user, state_ids[0], context=context
) or None
def create_using_magento_region(
self, cursor, user, country, region, context
):
"""
Creates state for the region sent by magento
:param cursor: Database cursor
:param user: ID of current user
:param country: Browse record of country
:param region: Name of state from magento
:param context: Application context
:return: Browse record of record created
"""
code = None
try:
for subdivision in pycountry.subdivisions.get(
country_code=country.code):
if subdivision.name.upper() == region.upper():
code = ''.join(list(region)[:3]).upper()
break
if not code:
if country.code == 'US':
code = 'APO'
else:
code = ''.join(list(region)[:3]).upper()
except KeyError:
raise osv.except_osv(
_('Country Not Found!'),
_('No country found with code %s' % country.code)
)
finally:
state_id = self.create(
cursor, user, {
'name': region,
'country_id': country.id,
'code': code,
}, context=context
)
return self.browse(cursor, user, state_id, context=context)