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.

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