You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.4 KiB

  1. # Copyright 2016 Vauxoo
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import os
  4. from odoo import _, api, models
  5. from odoo.exceptions import ValidationError
  6. class CompanyCountryConfigSettings(models.TransientModel):
  7. _name = 'company.country.config.settings'
  8. _description = 'Company Country Configuration Settings'
  9. @api.model
  10. def load_company_country(self, country_code=None):
  11. if not country_code:
  12. country_code = os.environ.get('COUNTRY')
  13. if country_code == "":
  14. self.env.ref('base.main_company').write({'country_id': None})
  15. return
  16. if not country_code:
  17. l10n_to_install = self.env['ir.module.module'].search([
  18. ('state', '=', 'to install'),
  19. ('name', '=like', 'l10n_%')], limit=1)
  20. if not l10n_to_install:
  21. raise ValidationError(
  22. _('Error COUNTRY environment variable with country code '
  23. 'not defined and no localization found in pool.'))
  24. country_code = l10n_to_install.name.split('l10n_')[1][:2].upper()
  25. country = self.env['res.country'].search([
  26. ('code', 'ilike', country_code)], limit=1)
  27. if not country:
  28. raise ValidationError(
  29. _('Country code %s not found. Use ISO 3166 codes 2 letters'))
  30. self.env.ref('base.main_company').write({'country_id': country.id})