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.

58 lines
2.0 KiB

9 years ago
  1. # -*- coding: utf-8 -*-
  2. # Odoo, Open Source Management Solution
  3. # Copyright (C) 2014-2015 Grupo ESOC <www.grupoesoc.es>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. from openerp import _, api, fields, models
  18. import logging
  19. _logger = logging.getLogger(__name__)
  20. class Partner(models.Model):
  21. """Partner with birth date in date format."""
  22. _inherit = "res.partner"
  23. # New birthdate field in date format
  24. birthdate_date = fields.Date("Birthdate")
  25. # Make the old Char field to reflect the new Date field
  26. birthdate = fields.Char(
  27. compute="_birthdate_compute",
  28. inverse="_birthdate_inverse",
  29. store=True)
  30. @api.one
  31. @api.depends("birthdate_date")
  32. def _birthdate_compute(self):
  33. """Store a string of the new date in the old field."""
  34. self.birthdate = self.birthdate_date
  35. @api.one
  36. def _birthdate_inverse(self):
  37. """Convert the old Char date to the new Date format."""
  38. try:
  39. self.birthdate_date = self.birthdate
  40. except ValueError:
  41. _logger.warn(
  42. _("Could not convert '{0.birthdate}' to date in "
  43. "res.partner {0.id} ({0.name}). Skipping.").format(self))
  44. @api.model
  45. def _birthdate_install(self):
  46. """Export all old birthdates to the new format."""
  47. self.search([('birthdate', "!=", False)])._inverse_birthdate()