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.

167 lines
6.9 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
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
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
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
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. # © 2016 ACSONE SA/NV (<http://acsone.eu>)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. from odoo.tools.translate import _
  5. from odoo.exceptions import ValidationError
  6. class DateRange(models.Model):
  7. _name = "date.range"
  8. _description = "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', index=1, required=True,
  18. ondelete='restrict', domain="['|', ('company_id', '=', company_id), "
  19. "('company_id', '=', False)]")
  20. type_name = fields.Char(
  21. string='Type', related='type_id.name', readonly=True, store=True)
  22. company_id = fields.Many2one(
  23. comodel_name='res.company', string='Company', index=1,
  24. default=_default_company)
  25. active = fields.Boolean(
  26. help="The active field allows you to hide the date range without "
  27. "removing it.", default=True)
  28. parent_type_id = fields.Many2one(
  29. related='type_id.parent_type_id',
  30. store=True,
  31. readonly=True)
  32. parent_id = fields.Many2one(
  33. comodel_name='date.range', string="Parent", index=1)
  34. _sql_constraints = [
  35. ('date_range_uniq', 'unique (name,type_id, company_id)',
  36. 'A date range must be unique per company !')]
  37. @api.onchange('company_id', 'type_id')
  38. def _onchange_company_id(self):
  39. if self.company_id and self.type_id.company_id and \
  40. self.type_id.company_id != self.company_id:
  41. self._cache.update(
  42. self._convert_to_cache({'type_id': False}, update=True))
  43. @api.multi
  44. @api.constrains('company_id', 'type_id')
  45. def _check_company_id_type_id(self):
  46. for rec in self.sudo():
  47. if rec.company_id and rec.type_id.company_id and\
  48. rec.company_id != rec.type_id.company_id:
  49. raise ValidationError(
  50. _('The Company in the Date Range and in '
  51. 'Date Range Type must be the same.'))
  52. @api.constrains('parent_id', 'date_start', 'date_end')
  53. def _validate_child_range(self):
  54. for this in self:
  55. if not this.parent_id:
  56. continue
  57. start = this.parent_id.date_start <= this.date_start
  58. end = this.parent_id.date_end >= this.date_end
  59. child_range = start and end
  60. if not child_range:
  61. text_dict = {
  62. 'name': this.name,
  63. 'start': this.date_start,
  64. 'end': this.date_end,
  65. 'parent_name': this.parent_id.name,
  66. 'parent_start': this.parent_id.date_start,
  67. 'parent_end': this.parent_id.date_end,
  68. }
  69. if (not start) and end:
  70. text = _(
  71. "Start date %(start)s of %(name)s must be greater than"
  72. " or equal to "
  73. "start date %(parent_start)s of %(parent_name)s"
  74. ) % text_dict
  75. elif (not end) and start:
  76. text = _(
  77. "End date %(end)s of %(name)s must be smaller than"
  78. " or equal to "
  79. "end date %(parent_end)s of %(parent_name)s"
  80. ) % text_dict
  81. else:
  82. text = _(
  83. "%(name)s range not in "
  84. "%(parent_start)s - %(parent_end)s"
  85. ) % text_dict
  86. raise ValidationError(
  87. _("%(name)s not a subrange of"
  88. " %(parent_name)s: " % text_dict) + text
  89. )
  90. @api.constrains('type_id', 'date_start', 'date_end', 'company_id')
  91. def _validate_range(self):
  92. for this in self:
  93. start = fields.Date.from_string(this.date_start)
  94. end = fields.Date.from_string(this.date_end)
  95. if start > end:
  96. raise ValidationError(
  97. _("%s is not a valid range (%s > %s)") % (
  98. this.name, this.date_start, this.date_end))
  99. if this.type_id.allow_overlap:
  100. continue
  101. # here we use a plain SQL query to benefit of the daterange
  102. # function available in PostgresSQL
  103. # (http://www.postgresql.org/docs/current/static/rangetypes.html)
  104. SQL = """
  105. SELECT
  106. id
  107. FROM
  108. date_range dt
  109. WHERE
  110. DATERANGE(dt.date_start, dt.date_end, '[]') &&
  111. DATERANGE(%s::date, %s::date, '[]')
  112. AND dt.id != %s
  113. AND dt.active
  114. AND dt.company_id = %s
  115. AND dt.type_id=%s;"""
  116. self.env.cr.execute(SQL, (this.date_start,
  117. this.date_end,
  118. this.id,
  119. this.company_id.id or None,
  120. this.type_id.id))
  121. res = self.env.cr.fetchall()
  122. if res:
  123. dt = self.browse(res[0][0])
  124. raise ValidationError(
  125. _("%s overlaps %s") % (this.name, dt.name))
  126. @api.multi
  127. def get_domain(self, field_name):
  128. self.ensure_one()
  129. return [(field_name, '>=', self.date_start),
  130. (field_name, '<=', self.date_end)]
  131. @api.multi
  132. @api.onchange('company_id', 'type_id', 'date_start', 'date_end')
  133. def onchange_type_id(self):
  134. """The type_id and the dates determine the choices for parent."""
  135. domain = []
  136. if self.company_id:
  137. domain.append(('company_id', '=', self.company_id.id))
  138. if self.parent_type_id:
  139. domain.append(('type_id', '=', self.parent_type_id.id))
  140. if self.date_start:
  141. domain.append('|')
  142. domain.append(('date_start', '<=', self.date_start))
  143. domain.append(('date_start', '=', False))
  144. if self.date_end:
  145. domain.append('|')
  146. domain.append(('date_end', '>=', self.date_end))
  147. domain.append(('date_end', '=', False))
  148. if domain:
  149. # If user did not select a parent already, autoselect the last
  150. # (ordered by date_start) or only parent that applies.
  151. if self.type_id and self.date_start and not self.parent_id:
  152. possible_parent = self.search(
  153. domain, limit=1, order='date_start desc')
  154. self.parent_id = possible_parent # can be empty!
  155. return {'domain': {'parent_id': domain}}