Browse Source

[IMP] partner_phonecall_schedule: black, isort

14.0
Sergio Teruel 5 years ago
committed by Víctor Martínez
parent
commit
6fda6c9455
  1. 9
      partner_phonecall_schedule/__manifest__.py
  2. 23
      partner_phonecall_schedule/models/res_partner.py
  3. 170
      partner_phonecall_schedule/tests/test_schedule.py

9
partner_phonecall_schedule/__manifest__.py

@ -9,11 +9,6 @@
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"depends": [
"resource",
],
"data": [
"security/ir.model.access.csv",
"views/res_partner_view.xml",
],
"depends": ["resource"],
"data": ["security/ir.model.access.csv", "views/res_partner_view.xml"],
}

23
partner_phonecall_schedule/models/res_partner.py

@ -2,7 +2,9 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from __future__ import division
from datetime import datetime
from odoo import api, fields, models
@ -45,31 +47,32 @@ class ResPartner(models.Model):
"""Fill attendance aggregation."""
for one in self:
one.phonecall_calendar_attendance_ids = one.mapped(
"phonecall_calendar_ids.attendance_ids")
"phonecall_calendar_ids.attendance_ids"
)
def _search_phonecall_available(self, operator, value):
"""Search quickly if partner is available to call right now."""
Attendance = self.env["resource.calendar.attendance"]
available = Attendance.search(
self._phonecall_available_domain(),
)
available = Attendance.search(self._phonecall_available_domain())
if operator == "!=" or "not" in operator:
value = not value
operator = "in" if value else "not in"
return [("phonecall_calendar_ids.attendance_ids",
operator, available.ids)]
return [("phonecall_calendar_ids.attendance_ids", operator, available.ids)]
def _phonecall_available_domain(self):
"""Get a domain to know if we are available to call a partner."""
now = fields.Datetime.from_string(self.env.context.get(
"now", datetime.now()))
now = fields.Datetime.from_string(self.env.context.get("now", datetime.now()))
date = fields.Date.to_string(now)
now_tz = fields.Datetime.context_timestamp(self, now)
float_time = now_tz.hour + ((now_tz.minute / 60) + now_tz.second) / 60
return [
("dayofweek", "=", str(now.weekday())),
"|", ("date_from", "=", False), ("date_from", "<=", date),
"|", ("date_to", "=", False), ("date_to", ">=", date),
"|",
("date_from", "=", False),
("date_from", "<=", date),
"|",
("date_to", "=", False),
("date_to", ">=", date),
("hour_from", "<=", float_time),
("hour_to", ">=", float_time),
]

170
partner_phonecall_schedule/tests/test_schedule.py

@ -2,7 +2,9 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from datetime import datetime, timedelta
from mock import patch
from odoo import fields
from odoo.tests.common import SavepointCase
@ -15,47 +17,65 @@ class CanICallCase(SavepointCase):
super(CanICallCase, cls).setUpClass()
cls.Calendar = cls.env["resource.calendar"].with_context(tz="UTC")
cls.Partner = cls.env["res.partner"].with_context(tz="UTC")
cls.some_mornings = cls.Calendar.create({
"name": "Some mornings",
"attendance_ids": [
(0, 0, {
"name": "Friday morning",
"dayofweek": "4",
"hour_from": 8,
"hour_to": 12,
}),
(0, 0, {
"name": "Next monday morning",
"dayofweek": "0",
"hour_from": 8,
"hour_to": 12,
"date_from": "2017-09-18",
"date_to": "2017-09-18",
}),
],
})
cls.some_evenings = cls.Calendar.create({
"name": "Some evenings",
"attendance_ids": [
(0, 0, {
"name": "Friday evening",
"dayofweek": "4",
"hour_from": 15,
"hour_to": 19,
}),
(0, 0, {
"name": "Next monday evening",
"dayofweek": "0",
"hour_from": 15,
"hour_to": 19,
"date_from": "2017-09-18",
"date_to": "2017-09-18",
}),
],
})
cls.dude = cls.Partner.create({
"name": "Dude",
})
cls.some_mornings = cls.Calendar.create(
{
"name": "Some mornings",
"attendance_ids": [
(
0,
0,
{
"name": "Friday morning",
"dayofweek": "4",
"hour_from": 8,
"hour_to": 12,
},
),
(
0,
0,
{
"name": "Next monday morning",
"dayofweek": "0",
"hour_from": 8,
"hour_to": 12,
"date_from": "2017-09-18",
"date_to": "2017-09-18",
},
),
],
}
)
cls.some_evenings = cls.Calendar.create(
{
"name": "Some evenings",
"attendance_ids": [
(
0,
0,
{
"name": "Friday evening",
"dayofweek": "4",
"hour_from": 15,
"hour_to": 19,
},
),
(
0,
0,
{
"name": "Next monday evening",
"dayofweek": "0",
"hour_from": 15,
"hour_to": 19,
"date_from": "2017-09-18",
"date_to": "2017-09-18",
},
),
],
}
)
cls.dude = cls.Partner.create({"name": "Dude"})
cls.dude.phonecall_calendar_ids = cls.some_mornings
def setUp(self):
@ -69,22 +89,18 @@ class CanICallCase(SavepointCase):
dude = dude.with_context(now=now)
Partner = Partner.with_context(now=now)
self.assertTrue(dude.phonecall_available)
self.assertTrue(Partner.search([
("id", "=", dude.id),
("phonecall_available", "=", True),
]))
self.assertTrue(Partner.search([
("id", "=", dude.id),
("phonecall_available", "!=", False),
]))
self.assertFalse(Partner.search([
("id", "=", dude.id),
("phonecall_available", "=", False),
]))
self.assertFalse(Partner.search([
("id", "=", dude.id),
("phonecall_available", "!=", True),
]))
self.assertTrue(
Partner.search([("id", "=", dude.id), ("phonecall_available", "=", True)])
)
self.assertTrue(
Partner.search([("id", "=", dude.id), ("phonecall_available", "!=", False)])
)
self.assertFalse(
Partner.search([("id", "=", dude.id), ("phonecall_available", "=", False)])
)
self.assertFalse(
Partner.search([("id", "=", dude.id), ("phonecall_available", "!=", True)])
)
def allowed(self):
# Test mocking datetime.now()
@ -95,8 +111,7 @@ class CanICallCase(SavepointCase):
# Test sending a datetime object in the context
self._allowed(self.datetime)
# Test sending a string in the context
self._allowed(
fields.Datetime.to_string(self.datetime))
self._allowed(fields.Datetime.to_string(self.datetime))
def _disallowed(self, now=None):
dude, Partner = self.dude, self.Partner
@ -104,22 +119,18 @@ class CanICallCase(SavepointCase):
dude = dude.with_context(now=now)
Partner = Partner.with_context(now=now)
self.assertFalse(dude.phonecall_available)
self.assertFalse(Partner.search([
("id", "=", dude.id),
("phonecall_available", "=", True),
]))
self.assertFalse(Partner.search([
("id", "=", dude.id),
("phonecall_available", "!=", False),
]))
self.assertTrue(Partner.search([
("id", "=", dude.id),
("phonecall_available", "=", False),
]))
self.assertTrue(Partner.search([
("id", "=", dude.id),
("phonecall_available", "!=", True),
]))
self.assertFalse(
Partner.search([("id", "=", dude.id), ("phonecall_available", "=", True)])
)
self.assertFalse(
Partner.search([("id", "=", dude.id), ("phonecall_available", "!=", False)])
)
self.assertTrue(
Partner.search([("id", "=", dude.id), ("phonecall_available", "=", False)])
)
self.assertTrue(
Partner.search([("id", "=", dude.id), ("phonecall_available", "!=", True)])
)
def disallowed(self):
# Test mocking datetime.now()
@ -130,8 +141,7 @@ class CanICallCase(SavepointCase):
# Test sending a datetime object in the context
self._disallowed(self.datetime)
# Test sending a string in the context
self._disallowed(
fields.Datetime.to_string(self.datetime))
self._disallowed(fields.Datetime.to_string(self.datetime))
def test_friday_morning(self):
"""I can call dude this morning"""
@ -166,6 +176,6 @@ class CanICallCase(SavepointCase):
"""I get aggregated schedules correctly."""
self.dude.phonecall_calendar_ids |= self.some_evenings
all_attendances = (self.some_mornings | self.some_evenings).mapped(
"attendance_ids")
self.assertEqual(
self.dude.phonecall_calendar_attendance_ids, all_attendances)
"attendance_ids"
)
self.assertEqual(self.dude.phonecall_calendar_attendance_ids, all_attendances)
Loading…
Cancel
Save