[IMP] mail_activity_team: black, isort

This commit is contained in:
Pedro Gonzalez 2019-10-24 13:47:32 +02:00 committed by Bernat Puig Font
parent f2c27cdb33
commit 108a4e722f
9 changed files with 206 additions and 180 deletions

View File

@ -1,23 +1,21 @@
# Copyright 2018 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': 'Mail Activity Team',
'summary': 'Add Teams to Activities',
'version': '12.0.1.0.0',
'development_status': 'Beta',
'category': 'Social Network',
'website': 'https://github.com/OCA/social',
'author': 'Eficent, Odoo Community Association (OCA)',
'license': 'AGPL-3',
'installable': True,
'depends': [
'mail_activity_board',
],
'data': [
'security/ir.model.access.csv',
'security/mail_activity_team_security.xml',
'views/mail_activity_team_views.xml',
'views/mail_activity_views.xml',
'views/res_users_views.xml',
"name": "Mail Activity Team",
"summary": "Add Teams to Activities",
"version": "13.0.1.0.0",
"development_status": "Beta",
"category": "Social Network",
"website": "https://github.com/OCA/social",
"author": "Eficent, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"depends": ["mail_activity_board"],
"data": [
"security/ir.model.access.csv",
"security/mail_activity_team_security.xml",
"views/mail_activity_team_views.xml",
"views/mail_activity_views.xml",
"views/res_users_views.xml",
],
}

View File

@ -1,6 +1,6 @@
# Copyright 2018 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, models, fields, _, SUPERUSER_ID
from odoo import SUPERUSER_ID, _, api, fields, models
from odoo.exceptions import ValidationError
@ -10,53 +10,52 @@ class MailActivity(models.Model):
def _get_default_team_id(self, user_id=None):
if not user_id:
user_id = self.env.uid
res_model = self.env.context.get('default_res_model')
model = self.env['ir.model'].search(
[('model', '=', res_model)], limit=1)
domain = [('member_ids', 'in', [user_id])]
res_model = self.env.context.get("default_res_model")
model = self.env["ir.model"].search([("model", "=", res_model)], limit=1)
domain = [("member_ids", "in", [user_id])]
if res_model:
domain.extend(['|', ('res_model_ids', '=', False),
('res_model_ids', 'in', model.ids)])
return self.env['mail.activity.team'].search(domain, limit=1)
domain.extend(
["|", ("res_model_ids", "=", False), ("res_model_ids", "in", model.ids)]
)
return self.env["mail.activity.team"].search(domain, limit=1)
team_id = fields.Many2one(
comodel_name='mail.activity.team',
default=lambda s: s._get_default_team_id(),
comodel_name="mail.activity.team", default=lambda s: s._get_default_team_id()
)
@api.onchange('user_id')
@api.onchange("user_id")
def _onchange_user_id(self):
res = {'domain': {'team_id': []}}
res = {"domain": {"team_id": []}}
if not self.user_id:
return res
res['domain']['team_id'] = [
'|',
('res_model_ids', '=', False),
('res_model_ids', 'in', self.res_model_id.ids)]
res["domain"]["team_id"] = [
"|",
("res_model_ids", "=", False),
("res_model_ids", "in", self.res_model_id.ids),
]
if self.team_id and self.user_id in self.team_id.member_ids:
return res
self.team_id = self.with_context(
default_res_model=self.res_model_id.id).\
_get_default_team_id(user_id=self.user_id.id)
default_res_model=self.res_model_id.id
)._get_default_team_id(user_id=self.user_id.id)
return res
@api.onchange('team_id')
@api.onchange("team_id")
def _onchange_team_id(self):
res = {'domain': {'user_id': []}}
res = {"domain": {"user_id": []}}
if not self.team_id:
return res
res['domain']['user_id'] = [('id', 'in', self.team_id.member_ids.ids)]
res["domain"]["user_id"] = [("id", "in", self.team_id.member_ids.ids)]
if self.user_id not in self.team_id.member_ids:
if self.team_id.user_id:
self.user_id = self.team_id.user_id
elif len(self.team_id.member_ids) == 1:
self.user_id = self.team_id.member_ids
else:
self.user_id = self.env['res.users']
self.user_id = self.env["res.users"]
return res
@api.multi
@api.constrains('team_id', 'user_id')
@api.constrains("team_id", "user_id")
def _check_team_and_user(self):
for activity in self:
# SUPERUSER is used to put mail.activity on some objects
@ -65,8 +64,12 @@ class MailActivity(models.Model):
# SUPERUSER is inactive and then even if you add it
# to member_ids it's not taken account
# To not be blocked we must add it to constraint condition
if activity.user_id.id != SUPERUSER_ID and activity.team_id and \
activity.user_id and \
activity.user_id not in self.team_id.member_ids:
if (
activity.user_id.id != SUPERUSER_ID
and activity.team_id
and activity.user_id
and activity.user_id not in self.team_id.member_ids
):
raise ValidationError(
_('The assigned user is not member of the team.'))
_("The assigned user is not member of " "the team.")
)

View File

@ -5,60 +5,56 @@ from odoo import api, fields, models
class MailActivityTeam(models.Model):
_name = "mail.activity.team"
_description = 'Mail Activity Team'
_description = "Mail Activity Team"
@api.depends('res_model_ids', 'member_ids')
@api.depends("res_model_ids", "member_ids")
def _compute_missing_activities(self):
activity_model = self.env['mail.activity']
activity_model = self.env["mail.activity"]
for team in self:
domain = [('team_id', '=', False)]
domain = [("team_id", "=", False)]
if team.member_ids:
domain.append(('user_id', 'in', team.member_ids.ids))
domain.append(("user_id", "in", team.member_ids.ids))
if team.res_model_ids:
domain.append(('res_model_id', 'in', team.res_model_ids.ids))
team.count_missing_activities = activity_model.search(
domain, count=True)
domain.append(("res_model_id", "in", team.res_model_ids.ids))
team.count_missing_activities = activity_model.search(domain, count=True)
name = fields.Char(
string='Name',
required=True,
translate=True,
)
active = fields.Boolean(
string='Active',
default=True,
)
name = fields.Char(string="Name", required=True, translate=True)
active = fields.Boolean(string="Active", default=True)
res_model_ids = fields.Many2many(
comodel_name='ir.model',
string='Used models',
comodel_name="ir.model",
string="Used models",
domain=lambda self: [
('model', 'in',
[k for k in self.env.registry if issubclass(
type(self.env[k]), type(self.env['mail.activity.mixin']))
and self.env[k]._auto])
(
"model",
"in",
[
k
for k in self.env.registry
if issubclass(
type(self.env[k]), type(self.env["mail.activity.mixin"])
)
and self.env[k]._auto
],
)
],
)
member_ids = fields.Many2many(
comodel_name='res.users',
relation='mail_activity_team_users_rel',
comodel_name="res.users",
relation="mail_activity_team_users_rel",
string="Team Members",
)
user_id = fields.Many2one(
comodel_name='res.users',
string='Team Leader',
)
user_id = fields.Many2one(comodel_name="res.users", string="Team Leader")
count_missing_activities = fields.Integer(
string="Missing Activities",
compute='_compute_missing_activities',
default=0,
string="Missing Activities", compute="_compute_missing_activities", default=0
)
@api.onchange('member_ids')
@api.onchange("member_ids")
def _onchange_member_ids(self):
"""Remove team leader in case is not a member anymore"""
if self.user_id and self.user_id not in self.member_ids:
self.user_id = False
@api.onchange('user_id')
@api.onchange("user_id")
def _onchange_user_id(self):
if self.user_id and self.user_id not in self.member_ids:
members_ids = self.member_ids.ids
@ -66,13 +62,13 @@ class MailActivityTeam(models.Model):
self.member_ids = [(4, member) for member in members_ids]
def assign_team_to_unassigned_activities(self):
activity_model = self.env['mail.activity']
activity_model = self.env["mail.activity"]
for team in self:
domain = [('team_id', '=', False)]
domain = [("team_id", "=", False)]
if team.member_ids:
domain.append(('user_id', 'in', team.member_ids.ids))
domain.append(("user_id", "in", team.member_ids.ids))
if team.res_model_ids:
domain.append(('res_model_id', 'in', team.res_model_ids.ids))
domain.append(("res_model_id", "in", team.res_model_ids.ids))
missing_activities = activity_model.search(domain)
for missing_activity in missing_activities:
missing_activity.write({'team_id': team.id})
missing_activity.write({"team_id": team.id})

View File

@ -1,13 +1,13 @@
# Copyright 2018 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models, fields
from odoo import fields, models
class ResUsers(models.Model):
_inherit = "res.users"
activity_team_ids = fields.Many2many(
comodel_name='mail.activity.team',
relation='mail_activity_team_users_rel',
comodel_name="mail.activity.team",
relation="mail_activity_team_users_rel",
string="Activity Teams",
)

View File

@ -2,3 +2,4 @@
* Jordi Ballester Alomar (jordi.ballester@eficent.com)
* Miquel Raïch (miquel.raich@eficent.com)
* Pedro Gonzalez (pedro.gonzalez@pesol.es)

View File

@ -1,3 +1 @@
This module adds the possibility to assign teams to activities.

View File

@ -1,94 +1,141 @@
# Copyright 2018 Eficent Business and IT Consulting Services, S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase
class TestMailActivityTeam(TransactionCase):
def setUp(self):
super(TestMailActivityTeam, self).setUp()
self.env["mail.activity.team"].search([]).unlink()
self.employee = self.env['res.users'].create({
'company_id': self.env.ref("base.main_company").id,
'name': "Employee",
'login': "csu",
'email': "crmuser@yourcompany.com",
'groups_id': [(6, 0, [
self.env.ref('base.group_user').id,
self.env.ref('base.group_partner_manager').id])]
})
self.employee = self.env["res.users"].create(
{
"company_id": self.env.ref("base.main_company").id,
"name": "Employee",
"login": "csu",
"email": "crmuser@yourcompany.com",
"groups_id": [
(
6,
0,
[
self.env.ref("base.group_user").id,
self.env.ref("base.group_partner_manager").id,
],
)
],
}
)
self.employee2 = self.env['res.users'].create({
'company_id': self.env.ref("base.main_company").id,
'name': "Employee 2",
'login': "csu2",
'email': "crmuser2@yourcompany.com",
'groups_id': [(6, 0, [self.env.ref('base.group_user').id])]
})
self.employee2 = self.env["res.users"].create(
{
"company_id": self.env.ref("base.main_company").id,
"name": "Employee 2",
"login": "csu2",
"email": "crmuser2@yourcompany.com",
"groups_id": [(6, 0, [self.env.ref("base.group_user").id])],
}
)
self.partner_ir_model = self.env['ir.model']._get('res.partner')
self.partner_ir_model = self.env["ir.model"]._get("res.partner")
activity_type_model = self.env['mail.activity.type']
self.activity1 = activity_type_model.create({
'name': 'Initial Contact',
'days': 5,
'summary': 'ACT 1 : Presentation, barbecue, ... ',
'res_model_id': self.partner_ir_model.id,
})
self.activity2 = activity_type_model.create({
'name': 'Call for Demo',
'days': 6,
'summary': 'ACT 2 : I want to show you my ERP !',
'res_model_id': self.partner_ir_model.id,
})
activity_type_model = self.env["mail.activity.type"]
self.activity1 = activity_type_model.create(
{
"name": "Initial Contact",
"delay_count": 5,
"delay_unit": "days",
"summary": "ACT 1 : Presentation, barbecue, ... ",
"res_model_id": self.partner_ir_model.id,
}
)
self.activity2 = activity_type_model.create(
{
"name": "Call for Demo",
"delay_count": 6,
"delay_unit": "days",
"summary": "ACT 2 : I want to show you my ERP !",
"res_model_id": self.partner_ir_model.id,
}
)
self.partner_client = self.env.ref("base.res_partner_1")
self.act1 = self.env['mail.activity'].sudo(self.employee).create({
'activity_type_id': self.activity1.id,
'note': 'Partner activity 1.',
'res_id': self.partner_client.id,
'res_model_id': self.partner_ir_model.id,
'user_id': self.employee.id,
})
self.act1 = (
self.env["mail.activity"]
.with_user(self.employee)
.create(
{
"activity_type_id": self.activity1.id,
"note": "Partner activity 1.",
"res_id": self.partner_client.id,
"res_model_id": self.partner_ir_model.id,
"user_id": self.employee.id,
}
)
)
self.team1 = self.env['mail.activity.team'].sudo().create({
'name': 'Team 1',
'res_model_ids': [(6, 0, [self.partner_ir_model.id])],
'member_ids': [(6, 0, [self.employee.id])],
})
self.team1 = (
self.env["mail.activity.team"]
.sudo()
.create(
{
"name": "Team 1",
"res_model_ids": [(6, 0, [self.partner_ir_model.id])],
"member_ids": [(6, 0, [self.employee.id])],
}
)
)
self.team2 = self.env['mail.activity.team'].sudo().create({
'name': 'Team 2',
'res_model_ids': [(6, 0, [self.partner_ir_model.id])],
'member_ids': [(6, 0, [self.employee.id, self.employee2.id])],
})
self.team2 = (
self.env["mail.activity.team"]
.sudo()
.create(
{
"name": "Team 2",
"res_model_ids": [(6, 0, [self.partner_ir_model.id])],
"member_ids": [(6, 0, [self.employee.id, self.employee2.id])],
}
)
)
self.act2 = self.env['mail.activity'].sudo(self.employee).create({
'activity_type_id': self.activity2.id,
'note': 'Partner activity 2.',
'res_id': self.partner_client.id,
'res_model_id': self.partner_ir_model.id,
'user_id': self.employee.id,
})
self.act2 = (
self.env["mail.activity"]
.with_user(self.employee)
.create(
{
"activity_type_id": self.activity2.id,
"note": "Partner activity 2.",
"res_id": self.partner_client.id,
"res_model_id": self.partner_ir_model.id,
"user_id": self.employee.id,
}
)
)
def test_missing_activities(self):
self.assertFalse(
self.act1.team_id, 'Error: Activity 1 should not have a team.')
self.assertFalse(self.act1.team_id, "Error: Activity 1 should not have a team.")
self.assertEqual(self.team1.count_missing_activities, 1)
self.team1.assign_team_to_unassigned_activities()
self.team1._compute_missing_activities()
self.assertEqual(self.team1.count_missing_activities, 0)
self.assertEqual(self.act1.team_id, self.team1)
def test_team_onchanges(self):
self.assertFalse(
self.team2.user_id, "Error: Team 2 should not have a Team Leader yet."
)
self.team2.user_id = self.employee
self.team2.member_ids = [(3, self.employee.id)]
self.team2._onchange_member_ids()
self.assertFalse(self.team2.user_id)
def test_activity_onchanges(self):
self.assertEqual(
self.act2.team_id, self.team1,
'Error: Activity 2 should have Team 1.')
with self.env.do_in_onchange():
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
)
self.act2.team_id = False
self.act2._onchange_team_id()
self.assertEqual(self.act2.user_id, self.employee)
@ -98,21 +145,5 @@ class TestMailActivityTeam(TransactionCase):
self.act2.user_id = self.employee2
self.act2._onchange_user_id()
self.assertEqual(self.act2.team_id, self.team2)
self.act2.team_id = self.team1
self.act2._onchange_team_id()
self.assertEqual(self.act2.user_id, self.team1.member_ids)
with self.assertRaises(ValidationError):
self.act2.write({
'user_id': self.employee2.id,
'team_id': self.team1.id,
})
def test_team_onchanges(self):
self.assertFalse(
self.team2.user_id,
'Error: Team 2 should not have a Team Leader yet.')
with self.env.do_in_onchange():
self.team2.user_id = self.employee
self.team2.member_ids = [(3, self.employee.id)]
self.team2._onchange_member_ids()
self.assertFalse(self.team2.user_id)
self.act2.write({"user_id": self.employee2.id, "team_id": self.team1.id})

View File

@ -78,7 +78,6 @@
<record model="ir.actions.act_window" id="mail_activity_team_action">
<field name="name">Activity Teams</field>
<field name="res_model">mail.activity.team</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="domain">[]</field>
<field name="context">{}</field>

View File

@ -61,7 +61,7 @@
<xpath expr='//field[@name="user_id"]' position='before'>
<field name="team_id"/>
</xpath>
<xpath expr='//filter[@name="activities_my"]' position='after'>
<xpath expr='//filter[@name="activities_6_month"]' position='after'>
<filter name="my_team_activities" string="My Team Activities" domain="[('team_id.member_ids', '=', uid)]"/>
</xpath>
<group position='inside'>