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.

46 lines
1.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import _, api, fields, models
  5. import logging
  6. _logger = logging.getLogger(__name__)
  7. class Partner(models.Model):
  8. """Partner with birth date in date format."""
  9. _inherit = "res.partner"
  10. # New birthdate field in date format
  11. birthdate_date = fields.Date("Birthdate")
  12. # Make the old Char field to reflect the new Date field
  13. birthdate = fields.Char(
  14. compute="_birthdate_compute",
  15. inverse="_birthdate_inverse",
  16. store=True)
  17. @api.multi
  18. @api.depends("birthdate_date")
  19. def _birthdate_compute(self):
  20. """Store a string of the new date in the old field."""
  21. for partner in self:
  22. partner.birthdate = partner.birthdate_date
  23. @api.multi
  24. def _birthdate_inverse(self):
  25. """Convert the old Char date to the new Date format."""
  26. for partner in self:
  27. try:
  28. partner.birthdate_date = partner.birthdate
  29. except ValueError:
  30. _logger.warn(_(
  31. "Could not convert '{0.birthdate}' to date in "
  32. "res.partner {0.id} ({0.name}). Skipping."
  33. ).format(partner))
  34. @api.model
  35. def _birthdate_install(self):
  36. """Export all old birthdates to the new format."""
  37. self.search([('birthdate', "!=", False)])._inverse_birthdate()