Adapt module to v13.0 and new usage

pull/2559/head
Akim Juillerat 2020-03-31 17:52:07 +02:00 committed by sbejaoui
parent 7947f599f0
commit 67f2168d2f
11 changed files with 71 additions and 260 deletions

View File

@ -1,2 +1 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from . import models

View File

@ -9,5 +9,9 @@
"license": "AGPL-3",
"website": "https://github.com/OCA/server-tools",
"depends": ["base"],
"data": [
"data/time_weekday.xml",
"security/ir.model.access.xml"
],
"installable": True,
}

View File

@ -4,31 +4,31 @@
<odoo noupdate="1">
<record model="delivery.week.day" id="delivery_weed_day_monday">
<record model="time.weekday" id="time_weekday_monday">
<field name="name">0</field>
</record>
<record model="delivery.week.day" id="delivery_weed_day_tuesday">
<record model="time.weekday" id="time_weekday_tuesday">
<field name="name">1</field>
</record>
<record model="delivery.week.day" id="delivery_weed_day_wednesday">
<record model="time.weekday" id="time_weekday_wednesday">
<field name="name">2</field>
</record>
<record model="delivery.week.day" id="delivery_weed_day_thursday">
<record model="time.weekday" id="time_weekday_thursday">
<field name="name">3</field>
</record>
<record model="delivery.week.day" id="delivery_weed_day_friday">
<record model="time.weekday" id="time_weekday_friday">
<field name="name">4</field>
</record>
<record model="delivery.week.day" id="delivery_weed_day_saturday">
<record model="time.weekday" id="time_weekday_saturday">
<field name="name">5</field>
</record>
<record model="delivery.week.day" id="delivery_weed_day_sunday">
<record model="time.weekday" id="time_weekday_sunday">
<field name="name">6</field>
</record>
</odoo>

View File

@ -1,2 +1,2 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from . import base_time_weekday
from . import base_time_window

View File

@ -5,10 +5,10 @@
from odoo import _, api, fields, models, tools
class DeliveryWeekDay(models.Model):
class TimeWeekday(models.Model):
_name = "delivery.week.day"
_description = "Delivery Week Day"
_name = "time.weekday"
_description = "Time Week Day"
name = fields.Selection(
selection=[
@ -38,7 +38,6 @@ class DeliveryWeekDay(models.Model):
for record in self:
record.display_name = translated_values[record.name]
@api.multi
def name_get(self):
"""
WORKAROUND since Odoo doesn't handle properly records where name is
@ -53,18 +52,16 @@ class DeliveryWeekDay(models.Model):
@api.model
def create(self, vals):
result = super(DeliveryWeekDay, self).create(vals)
result = super().create(vals)
self._get_id_by_name.clear_cache(self)
return result
@api.multi
def write(self, vals):
result = super(DeliveryWeekDay, self).write(vals)
result = super().write(vals)
self._get_id_by_name.clear_cache(self)
return result
@api.multi
def unlink(self):
result = super(DeliveryWeekDay, self).unlink()
result = super().unlink()
self._get_id_by_name.clear_cache(self)
return result

View File

@ -3,6 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import math
from datetime import time
from psycopg2.extensions import AsIs
@ -10,24 +11,24 @@ from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
class DeliveryWindow(models.Model):
class TimeWindowMixin(models.AbstractModel):
_name = "delivery.window"
_description = "Delivery Window"
_order = "partner_id, start"
_name = "time.window.mixin"
_description = "Time Window"
_order = "start"
# TODO patch api.constrains with field here?
_overlap_check_field = False
start = fields.Float("From", required=True)
end = fields.Float("To", required=True)
week_day_ids = fields.Many2many(
comodel_name="alc.delivery.week.day", required=True
)
partner_id = fields.Many2one(
"res.partner", required=True, index=True, ondelete='cascade'
weekday_ids = fields.Many2many(
comodel_name="time.weekday", required=True
)
@api.constrains("start", "end", "week_day_ids")
def check_window_no_onverlaps(self):
week_days_field = self._fields["week_day_ids"]
@api.constrains("start", "end", "weekday_ids")
def check_window_no_overlaps(self):
weekdays_field = self._fields["weekday_ids"]
for record in self:
if record.start > record.end:
raise ValidationError(
@ -51,20 +52,21 @@ class DeliveryWindow(models.Model):
NUMRANGE(w.start::numeric, w.end::numeric) &&
NUMRANGE(%(start)s::numeric, %(end)s::numeric)
AND w.id != %(window_id)s
AND d.%(relation_week_day_fkey)s in %(week_day_ids)s
AND w.partner_id = %(partner_id)s"""
AND d.%(relation_week_day_fkey)s in %(weekday_ids)s
AND w.%(check_field)s = %(check_field_id)s;"""
self.env.cr.execute(
SQL,
dict(
table=AsIs(self._table),
relation=AsIs(week_days_field.relation),
relation_window_fkey=AsIs(week_days_field.column1),
relation_week_day_fkey=AsIs(week_days_field.column2),
relation=AsIs(weekdays_field.relation),
relation_window_fkey=AsIs(weekdays_field.column1),
relation_week_day_fkey=AsIs(weekdays_field.column2),
start=record.start,
end=record.end,
window_id=record.id,
week_day_ids=tuple(record.week_day_ids.ids),
partner_id=record.partner_id.id,
weekday_ids=tuple(record.weekday_ids.ids),
check_field=AsIs(self._overlap_check_field),
check_field_id=record[self._overlap_check_field].id,
),
)
res = self.env.cr.fetchall()
@ -75,22 +77,37 @@ class DeliveryWindow(models.Model):
% (record.display_name, other.display_name)
)
@api.depends("start", "end", "week_day_ids")
@api.depends("start", "end", "weekday_ids")
def _compute_display_name(self):
for record in self:
"{days}: From {start} to {end}".format(
days=", ".join(record.week_day_ids.mapped("display_name")),
record.display_name = _("{days}: From {start} to {end}").format(
days=", ".join(record.weekday_ids.mapped("display_name")),
start=self.float_to_time_repr(record.start),
end=self.float_to_time_repr(record.end),
)
@api.model
def _get_hour_min_from_value(self, value):
hour = math.floor(value)
minute = round((value % 1) * 60)
if minute == 60:
minute = 0
hour += 1
return hour, minute
@api.model
def float_to_time_repr(self, value):
pattern = "%02d:%02d"
hour = math.floor(value)
min = round((value % 1) * 60)
if min == 60:
min = 0
hour += 1
hour, minute = self._get_hour_min_from_value(value)
return pattern % (hour, minute)
return pattern % (hour, min)
@api.model
def float_to_time(self, value):
hour, minute = self._get_hour_min_from_value(value)
return time(hour=hour, minute=minute)
def get_start_time(self):
return self.float_to_time(self.start)
def get_end_time(self):
return self.float_to_time(self.end)

View File

@ -0,0 +1,2 @@
* Laurent Mignon <laurent.mignon@acsone.eu>
* Akim Juillerat <akim.juillerat@camptocamp.com>

View File

@ -0,0 +1 @@
This module provides base classes to manage time windows.

View File

@ -3,33 +3,13 @@
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record model="ir.model.access" id="delivery_week_day_access_read">
<field name="name">delivery.window.day access read</field>
<field name="model_id" ref="model_delivery_week_day"/>
<record model="ir.model.access" id="time_weekday_access_read">
<field name="name">time.weekday access read</field>
<field name="model_id" ref="model_time_weekday"/>
<field name="group_id" ref="base.group_user"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>
<record model="ir.model.access" id="delivery_window_access_read">
<field name="name">delivery.window access read</field>
<field name="model_id" ref="model_delivery_window"/>
<field name="group_id" ref="base.group_user"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_write" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>
<record model="ir.model.access" id="delivery_window_access_manage">
<field name="name">delivery.window access read</field>
<field name="model_id" ref="model_delivery_window"/>
<field name="group_id" ref="sales_team.group_sale_manager"/>
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="1"/>
<field name="perm_write" eval="1"/>
<field name="perm_unlink" eval="1"/>
</record>
</odoo>

View File

@ -1,2 +0,0 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)

View File

@ -1,187 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright 2020 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.exceptions import ValidationError
from odoo.tests import SavepointCase
class TestDeliveryWindow(SavepointCase):
@classmethod
def setUpClass(cls):
super(TestDeliveryWindow, cls).setUpClass()
cls.partner_1 = cls.env['res.partner'].create({'name': 'partner 1'})
cls.partner_2 = cls.env['res.partner'].create({'name': 'patner 2'})
cls.DeliveryWindow = cls.env["delivery.window"]
cls.monday = cls.env.ref(
"partner_delivery_window.delivery_weed_day_monday"
)
cls.sunday = cls.env.ref(
"partner_delivery_window.delivery_weed_day_sunday"
)
def test_00(self):
"""
Data:
A partner without delivery window
Test Case:
Add a delivery window
Expected result:
A delivery window is created for the partner
"""
self.assertFalse(self.partner_1.delivery_window_ids)
self.DeliveryWindow.create(
{
"partner_id": self.partner_1.id,
"start": 10.0,
"end": 12.0,
"week_day_ids": [(4, self.monday.id)],
}
)
self.assertTrue(self.partner_1.delivery_window_ids)
delivery_window = self.partner_1.delivery_window_ids
self.assertEqual(delivery_window.start, 10.0)
self.assertEqual(delivery_window.end, 12.0)
self.assertEqual(delivery_window.week_day_ids, self.monday)
def test_01(self):
"""
Data:
A partner without delivery window
Test Case:
1 Add a delivery window
2 unlink the partner
Expected result:
1 A delivery window is created for the partner
2 The delivery window is removed
"""
partner_id = self.partner_1.id
self.assertFalse(self.partner_1.delivery_window_ids)
self.DeliveryWindow.create(
{
"partner_id": self.partner_1.id,
"start": 10.0,
"end": 12.0,
"week_day_ids": [(4, self.monday.id)],
}
)
self.assertTrue(self.partner_1.delivery_window_ids)
delivery_window = self.DeliveryWindow.search(
[("partner_id", "=", partner_id)]
)
self.assertTrue(delivery_window)
self.partner_1.unlink()
self.assertFalse(delivery_window.exists())
def test_02(self):
"""
Data:
A partner without delivery window
Test Case:
1 Add a delivery window
2 Add a second delivery window that overlaps the first one (same day)
Expected result:
1 A delivery window is created for the partner
2 ValidationError is raised
"""
self.DeliveryWindow.create(
{
"partner_id": self.partner_1.id,
"start": 10.0,
"end": 12.0,
"week_day_ids": [(4, self.monday.id)],
}
)
with self.assertRaises(ValidationError):
self.DeliveryWindow.create(
{
"partner_id": self.partner_1.id,
"start": 11.0,
"end": 13.0,
"week_day_ids": [(4, self.monday.id), (4, self.sunday.id)],
}
)
def test_03(self):
"""
Data:
A partner without delivery window
Test Case:
1 Add a delivery window
2 Add a second delivery window that overlaps the first one (another day)
Expected result:
1 A delivery window is created for the partner
2 A second delivery window is created for the partner
"""
self.assertFalse(self.partner_1.delivery_window_ids)
self.DeliveryWindow.create(
{
"partner_id": self.partner_1.id,
"start": 10.0,
"end": 12.0,
"week_day_ids": [(4, self.monday.id)],
}
)
self.assertTrue(self.partner_1.delivery_window_ids)
self.DeliveryWindow.create(
{
"partner_id": self.partner_1.id,
"start": 11.0,
"end": 13.0,
"week_day_ids": [(4, self.sunday.id)],
}
)
self.assertEquals(len(self.partner_1.delivery_window_ids), 2)
def test_04(self):
"""
Data:
Partner 1 without delivery window
Partner 2 without delivery window
Test Case:
1 Add a delivery window to partner 1
2 Add the same delivery window to partner 2
Expected result:
1 A delivery window is created for the partner 1
1 A delivery window is created for the partner 2
"""
self.assertFalse(self.partner_1.delivery_window_ids)
self.DeliveryWindow.create(
{
"partner_id": self.partner_1.id,
"start": 10.0,
"end": 12.0,
"week_day_ids": [(4, self.monday.id)],
}
)
self.assertTrue(self.partner_1.delivery_window_ids)
self.assertFalse(self.partner_2.delivery_window_ids)
self.DeliveryWindow.create(
{
"partner_id": self.partner_2.id,
"start": 10.0,
"end": 12.0,
"week_day_ids": [(4, self.monday.id)],
}
)
self.assertTrue(self.partner_2.delivery_window_ids)
def test_05(self):
"""""
Data:
Partner 1 without delivery window
Test Case:
Add a delivery window to partner 1 with end > start
Expected result:
ValidationError is raised
"""
with self.assertRaises(ValidationError):
self.DeliveryWindow.create(
{
"partner_id": self.partner_1.id,
"start": 14.0,
"end": 12.0,
"week_day_ids": [(4, self.monday.id)],
}
)