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.

77 lines
3.0 KiB

9.0 add date range Sorrento Delivery * [ADD] Basic structure for the new date range module * [IMP] Add a basic description into the README * [IMP] Basic implementation * [IMP] First working implementation * [IMP] Improve datamodel * [ADD] Add basic tests for date.range * [PEP8] * [PYLINT] * [DEL] Remove unused code * [IMP] Remove unsused dependencies into the JS * [IMP] Better operator label for date range * [DEL] Remove unused file * [IMP] Better user experience by showing the select input only once empty * [FIX]Try to fix tests that fails only on travis by adding an explicit cast on the daterange methods parameters * [FIX]Try to fix tests that fails only on travis by adding an explicit cast on the daterange methods parameters * [FIX]Try to fix tests that fails only on travis by using postgresql 9.4 * [FIX]Try with postgresql 9.2 since the daterange method has appeared in 9.2 * [IMP] Add a limitation into the module description to warm about the minimal version of postgresql to use * [IMP]Add multi-company rules * [IMP]Remove unused files * [FIX] Add missing brackets into JS * [FIX] Overlap detection when company_id is False * [IMP] Add default order for date.range * [IMP] Add date range generator * [FIX] OE compatibility * [FIX] Travis * [IMP] Code cleanup and improves test coverage * [FIX] Add missing dependency on 'web' * [PYLINT] remove unused import * [FIX] Add missing copyright * [FIX] Limits are included into the range * [IMP][date_range] Security * [IMP] Improve module description * [IMP] Spelling
9 years ago
9.0 add date range Sorrento Delivery * [ADD] Basic structure for the new date range module * [IMP] Add a basic description into the README * [IMP] Basic implementation * [IMP] First working implementation * [IMP] Improve datamodel * [ADD] Add basic tests for date.range * [PEP8] * [PYLINT] * [DEL] Remove unused code * [IMP] Remove unsused dependencies into the JS * [IMP] Better operator label for date range * [DEL] Remove unused file * [IMP] Better user experience by showing the select input only once empty * [FIX]Try to fix tests that fails only on travis by adding an explicit cast on the daterange methods parameters * [FIX]Try to fix tests that fails only on travis by adding an explicit cast on the daterange methods parameters * [FIX]Try to fix tests that fails only on travis by using postgresql 9.4 * [FIX]Try with postgresql 9.2 since the daterange method has appeared in 9.2 * [IMP] Add a limitation into the module description to warm about the minimal version of postgresql to use * [IMP]Add multi-company rules * [IMP]Remove unused files * [FIX] Add missing brackets into JS * [FIX] Overlap detection when company_id is False * [IMP] Add default order for date.range * [IMP] Add date range generator * [FIX] OE compatibility * [FIX] Travis * [IMP] Code cleanup and improves test coverage * [FIX] Add missing dependency on 'web' * [PYLINT] remove unused import * [FIX] Add missing copyright * [FIX] Limits are included into the range * [IMP][date_range] Security * [IMP] Improve module description * [IMP] Spelling
9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2016 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import api, fields, models
  5. from odoo.tools.translate import _
  6. from odoo.exceptions import ValidationError
  7. class DateRange(models.Model):
  8. _name = "date.range"
  9. _order = "type_name,date_start"
  10. @api.model
  11. def _default_company(self):
  12. return self.env['res.company']._company_default_get('date.range')
  13. name = fields.Char(required=True, translate=True)
  14. date_start = fields.Date(string='Start date', required=True)
  15. date_end = fields.Date(string='End date', required=True)
  16. type_id = fields.Many2one(
  17. comodel_name='date.range.type', string='Type', select=1, required=True)
  18. type_name = fields.Char(
  19. string='Type', related='type_id.name', readonly=True, store=True)
  20. company_id = fields.Many2one(
  21. comodel_name='res.company', string='Company', select=1,
  22. default=_default_company)
  23. active = fields.Boolean(
  24. help="The active field allows you to hide the date range without "
  25. "removing it.", default=True)
  26. _sql_constraints = [
  27. ('date_range_uniq', 'unique (name,type_id, company_id)',
  28. 'A date range must be unique per company !')]
  29. @api.constrains('type_id', 'date_start', 'date_end', 'company_id')
  30. def _validate_range(self):
  31. for this in self:
  32. start = fields.Date.from_string(this.date_start)
  33. end = fields.Date.from_string(this.date_end)
  34. if start >= end:
  35. raise ValidationError(
  36. _("%s is not a valid range (%s >= %s)") % (
  37. this.name, this.date_start, this.date_end))
  38. if this.type_id.allow_overlap:
  39. continue
  40. # here we use a plain SQL query to benefit of the daterange
  41. # function available in PostgresSQL
  42. # (http://www.postgresql.org/docs/current/static/rangetypes.html)
  43. SQL = """
  44. SELECT
  45. id
  46. FROM
  47. date_range dt
  48. WHERE
  49. DATERANGE(dt.date_start, dt.date_end, '[]') &&
  50. DATERANGE(%s::date, %s::date, '[]')
  51. AND dt.id != %s
  52. AND dt.active
  53. AND dt.company_id = %s
  54. AND dt.type_id=%s;"""
  55. self.env.cr.execute(SQL, (this.date_start,
  56. this.date_end,
  57. this.id,
  58. this.company_id.id or None,
  59. this.type_id.id))
  60. res = self.env.cr.fetchall()
  61. if res:
  62. dt = self.browse(res[0][0])
  63. raise ValidationError(
  64. _("%s overlaps %s") % (this.name, dt.name))
  65. @api.multi
  66. def get_domain(self, field_name):
  67. self.ensure_one()
  68. return [(field_name, '>=', self.date_start),
  69. (field_name, '<=', self.date_end)]