|
| 1 | +import logging |
| 2 | + |
| 3 | +import xlrd |
| 4 | + |
| 5 | +from odoo import fields, models |
| 6 | + |
| 7 | +_logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class County(models.Model): |
| 11 | + _name = "res.country.state.county" |
| 12 | + _description = "United States County" |
| 13 | + _sql_constraints = [ |
| 14 | + ( |
| 15 | + "name_uniq", |
| 16 | + "unique(name, state_id)", |
| 17 | + "County name must be unique per state!", |
| 18 | + ), |
| 19 | + ] |
| 20 | + |
| 21 | + name = fields.Char() |
| 22 | + country_id = fields.Many2one( |
| 23 | + "res.country", related="state_id.country_id", readonly=True |
| 24 | + ) |
| 25 | + state_id = fields.Many2one("res.country.state") |
| 26 | + |
| 27 | + def _import_counties(self, file_contents): |
| 28 | + """Import Excel spreadsheet from U.S. Census Bureau. |
| 29 | + See https://www.census.gov/library/publications/2011/compendia/usa-counties-2011.html""" |
| 30 | + self.check_access("create") |
| 31 | + |
| 32 | + country_id = self.env.ref("base.us").id |
| 33 | + |
| 34 | + def get_state_id(code): |
| 35 | + return ( |
| 36 | + self.env["res.country.state"] |
| 37 | + .search( |
| 38 | + [ |
| 39 | + ("country_id", "=", country_id), |
| 40 | + ("code", "=", code), |
| 41 | + ], |
| 42 | + limit=1, |
| 43 | + ) |
| 44 | + .id |
| 45 | + ) |
| 46 | + |
| 47 | + # https://www2.census.gov/library/publications/2011/compendia/usa-counties/excel/LND01.xls |
| 48 | + workbook = xlrd.open_workbook(filename="LND01.xls", file_contents=file_contents) |
| 49 | + sheet = workbook.sheet_by_index(0) |
| 50 | + for i, row in enumerate(sheet.get_rows()): |
| 51 | + if i == 0: |
| 52 | + assert row[0].value == "Areaname", row |
| 53 | + assert row[1].value == "STCOU" |
| 54 | + continue |
| 55 | + |
| 56 | + areaname = row[0].value |
| 57 | + stcou = int(row[1].value) |
| 58 | + if (stcou % 1000) == 0: |
| 59 | + _logger.debug("reading %s", areaname) |
| 60 | + continue |
| 61 | + |
| 62 | + if not areaname: |
| 63 | + continue |
| 64 | + if ", " not in areaname: |
| 65 | + if areaname == "District of Columbia": |
| 66 | + areaname += ", DC" |
| 67 | + else: |
| 68 | + _logger.warning("skipping %s", areaname) |
| 69 | + continue |
| 70 | + |
| 71 | + county_name, state_code = areaname.split(", ") |
| 72 | + state_id = get_state_id(state_code) |
| 73 | + if self.search([("name", "=", county_name), ("state_id", "=", state_id)]): |
| 74 | + _logger.warning("skipping duplicate %s", areaname) |
| 75 | + continue |
| 76 | + |
| 77 | + county = self.create( |
| 78 | + { |
| 79 | + "name": county_name, |
| 80 | + "state_id": get_state_id(state_code), |
| 81 | + } |
| 82 | + ) |
| 83 | + self.env["ir.model.data"].create( |
| 84 | + { |
| 85 | + "module": "l10n_us_base_county", |
| 86 | + "model": self._name, |
| 87 | + "name": f"res_country_state_county_{stcou}", |
| 88 | + "res_id": county.id, |
| 89 | + } |
| 90 | + ) |
| 91 | + _logger.info("created %s", areaname) |
0 commit comments