def __init__(self): iso3166_path = path.join(path.dirname(__file__), 'iso3166') self.csparser = CountriesStatesParser(iso3166_path) self.csparser.parse() self.loaded_countries = []
class CountriesStatesFromFile(object): """Countries utility that reads data from a file """ implements(ICountriesStates) _no_values = [(u'(no values)',u'(no values)')] # Be careful, somewhere else, there is code that asumes that state values # length is <= 6. Don't modify this ones that are part of the vocabulary _not_aplicable = [(u'??NA',u'Not Applicable')] _allowed_no_values = [(u'??NV',u'(no values)')] def __init__(self): iso3166_path = path.join(path.dirname(__file__), 'iso3166') self.csparser = CountriesStatesParser(iso3166_path) self.csparser.parse() self.loaded_countries = [] def special_values(self): return [self._no_values[0], self._not_aplicable[0], self._allowed_no_values[0]] special_values = property(special_values) def countries(self): if self.loaded_countries: return self.loaded_countries names = self.csparser.getCountriesNameOrdered() res = [] for n in names: if len(n[1]) < 18: res.append( n ) elif ',' in n: res.append( ( n[0], n[1].split(',')[0] ) ) else: #This may show the countries wrongly abbreviated (in fact i am #almost sure it will, but is better than not showing them at all res.append( ( n[0], n[1][:18] ) ) # need to pick this up some list of strings property in the admin interface def sorter( x, y, order=['UNITED STATES', 'UNITED KINGDOM', 'CANADA']): if x[1] in order and y[1] in order: return cmp( order.index(x[1]), order.index(y[1]) ) if x[1] in order: return -1 if y[1] in order: return 1 return cmp( x[1], y[1] ) res.sort( sorter ) self.loaded_countries = res return res countries = property(countries) def states(self, country=None, allow_no_values=False): if country is None: states = self._not_aplicable + self.allStates() else: states = self.csparser.getStatesOf(country) if len(states) == 0: states = self._not_aplicable elif allow_no_values: states = self._allowed_no_values + states else: states = self._no_values + states return states def allStates(self): return self.csparser.getStatesOfAllCountries() def allStateValues(self): all_states = self.csparser.getStatesOfAllCountries() return self._allowed_no_values + self._not_aplicable + all_states
class CountriesStatesFromFile(object): """Countries utility that reads data from a file """ implements(ICountriesStates) _no_value = [('--',_(u'(no value)'))] def __init__(self): iso3166_path = path.join(path.dirname(__file__), 'iso3166') self.csparser = CountriesStatesParser(iso3166_path) self.csparser.parse() self.loaded_countries = [] def special_values(self): return [self._no_values[0]] special_values = property(special_values) def countries(self): #if self.loaded_countries: # return self.loaded_countries names = self.csparser.getCountriesNameOrdered() res = [] for n in names: if len(n[1]) < 18: res.append( n ) elif ',' in n: res.append( ( n[0], n[1].split(',')[0] ) ) else: #This may show the countries wrongly abbreviated (in fact i am #almost sure it will, but is better than not showing them at all res.append( ( n[0], n[1][:18] ) ) # need to pick this up some list of strings property in the admin interface def sorter( x, y, order=[]): if x[1] in order and y[1] in order: return cmp( order.index(x[1]), order.index(y[1]) ) if x[1] in order: return -1 if y[1] in order: return 1 return cmp( x[1], y[1] ) res.sort( sorter ) res = self._no_value + res self.loaded_countries = res return res countries = property(countries) def states(self, country=None): if country is None: states = self.allStates() else: states = self._no_value + self.csparser.getStatesOf(country) return states def allStates(self): return self._no_value + self.csparser.getStatesOfAllCountries() def allStateValues(self): all_states = self.csparser.getStatesOfAllCountries() return self._no_value + all_states
class CountriesStatesFromFile(object): """Countries utility that reads data from a file """ implements(ICountriesStates) _no_values = [(u'(no values)', u'(no values)')] # Be careful, somewhere else, there is code that asumes that state values # length is <= 6. Don't modify this ones that are part of the vocabulary _not_aplicable = [(u'??NA', u'Not Applicable')] _allowed_no_values = [(u'??NV', u'(no values)')] def __init__(self): iso3166_path = path.join(path.dirname(__file__), 'iso3166') self.csparser = CountriesStatesParser(iso3166_path) self.csparser.parse() self.loaded_countries = [] def special_values(self): return [ self._no_values[0], self._not_aplicable[0], self._allowed_no_values[0] ] special_values = property(special_values) def countries(self): if self.loaded_countries: return self.loaded_countries names = self.csparser.getCountriesNameOrdered() res = [] for n in names: if len(n[1]) < 18: res.append(n) elif ',' in n: res.append((n[0], n[1].split(',')[0])) else: #This may show the countries wrongly abbreviated (in fact i am #almost sure it will, but is better than not showing them at all res.append((n[0], n[1][:18])) # need to pick this up some list of strings property in the admin interface def sorter(x, y, order=['UNITED STATES', 'UNITED KINGDOM', 'CANADA']): if x[1] in order and y[1] in order: return cmp(order.index(x[1]), order.index(y[1])) if x[1] in order: return -1 if y[1] in order: return 1 return cmp(x[1], y[1]) res.sort(sorter) self.loaded_countries = res return res countries = property(countries) def states(self, country=None, allow_no_values=False): if country is None: states = self._not_aplicable + self.allStates() else: states = self.csparser.getStatesOf(country) if len(states) == 0: states = self._not_aplicable elif allow_no_values: states = self._allowed_no_values + states else: states = self._no_values + states return states def allStates(self): return self.csparser.getStatesOfAllCountries() def allStateValues(self): all_states = self.csparser.getStatesOfAllCountries() return self._allowed_no_values + self._not_aplicable + all_states
class CountriesStatesFromFile(object): """Countries utility that reads data from a file """ implements(ICountriesStates) _no_value = [('--',_(u'(no value)'))] def __init__(self): iso3166_path = path.join(path.dirname(__file__), 'iso3166') self.csparser = CountriesStatesParser(iso3166_path) self.csparser.parse() self.loaded_countries = [] def special_values(self): return [self._no_values[0]] special_values = property(special_values) def countries(self): #if self.loaded_countries: # return self.loaded_countries names = self.csparser.getCountriesNameOrdered() res = [] for n in names: if len(n[1]) < 18: res.append( n ) elif ',' in n: res.append( ( n[0], n[1].split(',')[0] ) ) else: #This may show the countries wrongly abbreviated (in fact i am #almost sure it will, but is better than not showing them at all res.append( ( n[0], n[1][:18] ) ) # need to pick this up some list of strings property in the admin interface def sorter( x, y, order=['ARGENTINA']): if x[1] in order and y[1] in order: return cmp( order.index(x[1]), order.index(y[1]) ) if x[1] in order: return -1 if y[1] in order: return 1 return cmp( x[1], y[1] ) res.sort( sorter ) res = self._no_value + res self.loaded_countries = res return res countries = property(countries) def states(self, country=None): if country is None: states = self.allStates() else: states = self._no_value + self.csparser.getStatesOf(country) return states def allStates(self): return self._no_value + self.csparser.getStatesOfAllCountries() def allStateValues(self): all_states = self.csparser.getStatesOfAllCountries() return self._no_value + all_states