Browse Source

[IMP] date_range: adapt to multicompany

pull/10/head
mreficent 7 years ago
parent
commit
1a67460010
  1. 1
      date_range/README.rst
  2. 2
      date_range/__manifest__.py
  3. 20
      date_range/models/date_range.py
  4. 17
      date_range/models/date_range_type.py
  5. 4
      date_range/security/date_range_security.xml
  6. 22
      date_range/wizard/date_range_generator.py

1
date_range/README.rst

@ -91,6 +91,7 @@ Contributors
------------ ------------
* Laurent Mignon <laurent.mignon@acsone.eu> * Laurent Mignon <laurent.mignon@acsone.eu>
* Miquel Raïch <miquel.raich@eficent.com>
Maintainer Maintainer
---------- ----------

2
date_range/__manifest__.py

@ -3,7 +3,7 @@
{ {
"name": "Date Range", "name": "Date Range",
"summary": "Manage all kind of date range", "summary": "Manage all kind of date range",
"version": "11.0.1.0.1",
"version": "11.0.2.0.0",
"category": "Uncategorized", "category": "Uncategorized",
"website": "https://github.com/oca/server-ux", "website": "https://github.com/oca/server-ux",
"author": "ACSONE SA/NV, Odoo Community Association (OCA)", "author": "ACSONE SA/NV, Odoo Community Association (OCA)",

20
date_range/models/date_range.py

@ -19,7 +19,8 @@ class DateRange(models.Model):
date_end = fields.Date(string='End date', required=True) date_end = fields.Date(string='End date', required=True)
type_id = fields.Many2one( type_id = fields.Many2one(
comodel_name='date.range.type', string='Type', index=1, required=True, comodel_name='date.range.type', string='Type', index=1, required=True,
ondelete='restrict')
ondelete='restrict', domain="['|', ('company_id', '=', company_id), "
"('company_id', '=', False)]")
type_name = fields.Char( type_name = fields.Char(
string='Type', related='type_id.name', readonly=True, store=True) string='Type', related='type_id.name', readonly=True, store=True)
company_id = fields.Many2one( company_id = fields.Many2one(
@ -33,6 +34,23 @@ class DateRange(models.Model):
('date_range_uniq', 'unique (name,type_id, company_id)', ('date_range_uniq', 'unique (name,type_id, company_id)',
'A date range must be unique per company !')] 'A date range must be unique per company !')]
@api.onchange('company_id')
def _onchange_company_id(self):
if self.company_id and self.type_id.company_id and \
self.type_id.company_id != self.company_id:
self._cache.update(
self._convert_to_cache({'type_id': False}, update=True))
@api.multi
@api.constrains('company_id', 'type_id')
def _check_company_id_type_id(self):
for rec in self.sudo():
if rec.company_id and rec.type_id.company_id and\
rec.company_id != rec.type_id.company_id:
raise ValidationError(
_('The Company in the Date Range and in '
'Date Range Type must be the same.'))
@api.constrains('type_id', 'date_start', 'date_end', 'company_id') @api.constrains('type_id', 'date_start', 'date_end', 'company_id')
def _validate_range(self): def _validate_range(self):
for this in self: for this in self:

17
date_range/models/date_range_type.py

@ -2,6 +2,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models from odoo import api, fields, models
from odoo.tools.translate import _
from odoo.exceptions import ValidationError
class DateRangeType(models.Model): class DateRangeType(models.Model):
@ -21,7 +23,22 @@ class DateRangeType(models.Model):
company_id = fields.Many2one( company_id = fields.Many2one(
comodel_name='res.company', string='Company', index=1, comodel_name='res.company', string='Company', index=1,
default=_default_company) default=_default_company)
date_range_ids = fields.One2many('date.range', 'type_id', string='Ranges')
_sql_constraints = [ _sql_constraints = [
('date_range_type_uniq', 'unique (name,company_id)', ('date_range_type_uniq', 'unique (name,company_id)',
'A date range type must be unique per company !')] 'A date range type must be unique per company !')]
@api.constrains('company_id')
def _check_company_id(self):
if not self.env.context.get('bypass_company_validation', False):
for rec in self.sudo():
if not rec.company_id:
continue
if bool(self.date_range_ids.filtered(
lambda r: r.company_id and
r.company_id != rec.company_id)):
raise ValidationError(
_('You cannot change the company, as this '
'Date Range Type is assigned to Date Range '
'(%s).') % (self.date_range_ids.name_get()[0][1]))

4
date_range/security/date_range_security.xml

@ -3,11 +3,11 @@
<record id="date_range_type_comp_rule" model="ir.rule"> <record id="date_range_type_comp_rule" model="ir.rule">
<field name="name">Date Range Type multi-company</field> <field name="name">Date Range Type multi-company</field>
<field name="model_id" ref="model_date_range_type"/> <field name="model_id" ref="model_date_range_type"/>
<field name="domain_force"> ['|',('company_id','=',user.company_id.id),('company_id','=',False)]</field>
<field name="domain_force"> ['|',('company_id','child_of',[user.company_id.id]),('company_id','=',False)]</field>
</record> </record>
<record id="date_range_comp_rule" model="ir.rule"> <record id="date_range_comp_rule" model="ir.rule">
<field name="name">Date Range multi-company</field> <field name="name">Date Range multi-company</field>
<field name="model_id" ref="model_date_range"/> <field name="model_id" ref="model_date_range"/>
<field name="domain_force"> ['|',('company_id','=',user.company_id.id),('company_id','=',False)]</field>
<field name="domain_force"> ['|',('company_id','child_of',[user.company_id.id]),('company_id','=',False)]</field>
</record> </record>
</odoo> </odoo>

22
date_range/wizard/date_range_generator.py

@ -2,6 +2,8 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models from odoo import api, fields, models
from odoo.tools.translate import _
from odoo.exceptions import ValidationError
from dateutil.rrule import (rrule, from dateutil.rrule import (rrule,
YEARLY, YEARLY,
MONTHLY, MONTHLY,
@ -21,7 +23,8 @@ class DateRangeGenerator(models.TransientModel):
date_start = fields.Date(strint='Start date', required=True) date_start = fields.Date(strint='Start date', required=True)
type_id = fields.Many2one( type_id = fields.Many2one(
comodel_name='date.range.type', string='Type', required=True, comodel_name='date.range.type', string='Type', required=True,
ondelete='cascade')
domain="['|', ('company_id', '=', company_id), "
"('company_id', '=', False)]", ondelete='cascade')
company_id = fields.Many2one( company_id = fields.Many2one(
comodel_name='res.company', string='Company', comodel_name='res.company', string='Company',
default=_default_company) default=_default_company)
@ -58,6 +61,23 @@ class DateRangeGenerator(models.TransientModel):
'company_id': self.company_id.id}) 'company_id': self.company_id.id})
return date_ranges return date_ranges
@api.onchange('company_id')
def _onchange_company_id(self):
if self.company_id and self.type_id.company_id and \
self.type_id.company_id != self.company_id:
self._cache.update(
self._convert_to_cache({'type_id': False}, update=True))
@api.multi
@api.constrains('company_id', 'type_id')
def _check_company_id_type_id(self):
for rec in self.sudo():
if rec.company_id and rec.type_id.company_id and\
rec.company_id != rec.type_id.company_id:
raise ValidationError(
_('The Company in the Date Range Generator and in '
'Date Range Type must be the same.'))
@api.multi @api.multi
def action_apply(self): def action_apply(self):
date_ranges = self._compute_date_ranges() date_ranges = self._compute_date_ranges()

Loading…
Cancel
Save