[IMP]mail_activity_done: black, isort, prettier

This commit is contained in:
manu 2021-08-13 12:50:46 +02:00 committed by Bernat Puig Font
parent 49d8235387
commit 88e375f09a
9 changed files with 212 additions and 152 deletions

View File

@ -3,18 +3,17 @@
{
"name": "Mail Activity Done",
"version": "12.0.2.0.0",
"author": "Eficent,"
"Odoo Community Association (OCA)",
"author": "Eficent," "Odoo Community Association (OCA)",
"license": "LGPL-3",
"category": "Discuss",
"depends": [
'mail',
"mail",
],
"data": [
'views/templates.xml',
'views/mail_activity_views.xml',
"views/templates.xml",
"views/mail_activity_views.xml",
],
"pre_init_hook": "pre_init_hook",
"post_load": "post_load_hook",
'uninstall_hook': 'uninstall_hook',
"uninstall_hook": "uninstall_hook",
}

View File

@ -1,23 +1,27 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# Copyright 2018 Odoo, S.A.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo.addons.mail.models.mail_activity import MailActivity
from odoo import fields
from odoo.addons.mail.models.mail_activity import MailActivity
def pre_init_hook(cr):
"""The objective of this hook is to default to false all values of field
'done' of mail.activity
"""
cr.execute("""SELECT column_name
cr.execute(
"""SELECT column_name
FROM information_schema.columns
WHERE table_name='mail_activity' AND
column_name='done'""")
column_name='done'"""
)
if not cr.fetchone():
cr.execute(
"""
ALTER TABLE mail_activity ADD COLUMN done boolean;
""")
"""
)
cr.execute(
"""
@ -28,12 +32,11 @@ def pre_init_hook(cr):
def post_load_hook():
def new_action_feedback(self, feedback=False):
if 'done' not in self._fields:
if "done" not in self._fields:
return self.action_feedback_original(feedback=feedback)
message = self.env['mail.message']
message = self.env["mail.message"]
if feedback:
self.write(dict(feedback=feedback))
for activity in self:
@ -42,15 +45,15 @@ def post_load_hook():
activity.active = False
activity.date_done = fields.Date.today()
record.message_post_with_view(
'mail.message_activity_done',
values={'activity': activity},
subtype_id=self.env.ref('mail.mt_activities').id,
"mail.message_activity_done",
values={"activity": activity},
subtype_id=self.env.ref("mail.mt_activities").id,
mail_activity_type_id=activity.activity_type_id.id,
)
message |= record.message_ids[0]
return message.ids and message.ids[0] or False
if not hasattr(MailActivity, 'action_feedback_original'):
if not hasattr(MailActivity, "action_feedback_original"):
MailActivity.action_feedback_original = MailActivity.action_feedback
MailActivity.action_feedback = new_action_feedback

View File

@ -5,8 +5,10 @@
def migrate(cr, version):
if not version:
return
cr.execute("""
cr.execute(
"""
UPDATE mail_activity SET
active = False
WHERE done=True;
""")
"""
)

View File

@ -5,26 +5,27 @@ from odoo import api, fields, models
class MailActivity(models.Model):
_inherit = 'mail.activity'
_inherit = "mail.activity"
active = fields.Boolean(default=True)
done = fields.Boolean(default=False)
state = fields.Selection(selection_add=[
('done', 'Done')], compute='_compute_state')
state = fields.Selection(selection_add=[("done", "Done")], compute="_compute_state")
date_done = fields.Date(
'Completed Date', index=True, readonly=True,
"Completed Date",
index=True,
readonly=True,
)
@api.depends('date_deadline', 'done')
@api.depends("date_deadline", "done")
def _compute_state(self):
super(MailActivity, self)._compute_state()
for record in self.filtered(lambda activity: activity.done):
record.state = 'done'
record.state = "done"
class MailActivityMixin(models.AbstractModel):
_inherit = 'mail.activity.mixin'
_inherit = "mail.activity.mixin"
activity_ids = fields.One2many(
domain=lambda self: [('res_model', '=', self._name),
('active', '=', True)])
domain=lambda self: [("res_model", "=", self._name), ("active", "=", True)]
)

View File

@ -4,7 +4,7 @@ from odoo import api, fields, models, modules
class ResUsers(models.Model):
_inherit = 'res.users'
_inherit = "res.users"
@api.model
def systray_get_activities(self):
@ -25,31 +25,38 @@ class ResUsers(models.Model):
AND act.done = False
GROUP BY m.id, states, act.res_model;
"""
self.env.cr.execute(query, {
'today': fields.Date.context_today(self),
'user_id': self.env.uid,
})
self.env.cr.execute(
query,
{
"today": fields.Date.context_today(self),
"user_id": self.env.uid,
},
)
activity_data = self.env.cr.dictfetchall()
model_ids = [a['id'] for a in activity_data]
model_names = {n[0]: n[1] for n in self.env['ir.model'].browse(
model_ids).name_get()}
model_ids = [a["id"] for a in activity_data]
model_names = {
n[0]: n[1] for n in self.env["ir.model"].browse(model_ids).name_get()
}
user_activities = {}
for activity in activity_data:
if not user_activities.get(activity['model']):
user_activities[activity['model']] = {
'name': model_names[activity['id']],
'model': activity['model'],
'icon': modules.module.get_module_icon(
self.env[activity['model']]._original_module),
'total_count': 0, 'today_count': 0,
'overdue_count': 0, 'planned_count': 0,
'type': 'activity',
if not user_activities.get(activity["model"]):
user_activities[activity["model"]] = {
"name": model_names[activity["id"]],
"model": activity["model"],
"icon": modules.module.get_module_icon(
self.env[activity["model"]]._original_module
),
"total_count": 0,
"today_count": 0,
"overdue_count": 0,
"planned_count": 0,
"type": "activity",
}
user_activities[activity['model']][
'%s_count' % activity['states']] += activity['count']
if activity['states'] in ('today', 'overdue'):
user_activities[activity['model']][
'total_count'] += activity['count']
user_activities[activity["model"]][
"%s_count" % activity["states"]
] += activity["count"]
if activity["states"] in ("today", "overdue"):
user_activities[activity["model"]]["total_count"] += activity["count"]
return list(user_activities.values())

View File

@ -1,14 +1,14 @@
// Copyright2018 Eficent <http://www.eficent.com>
// License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
odoo.define('mail.Activity.done', function(require) {
odoo.define("mail.Activity.done", function (require) {
"use strict";
var mailUtils = require('mail.utils');
var core = require('web.core');
var utils = require('mail.utils');
var time = require('web.time');
var mail_activity = require('mail.Activity');
var mailUtils = require("mail.utils");
var core = require("web.core");
var utils = require("mail.utils");
var time = require("web.time");
var mail_activity = require("mail.Activity");
var QWeb = core.qweb;
var _t = core._t;
@ -16,33 +16,33 @@ odoo.define('mail.Activity.done', function(require) {
// We are forced here to override the method, as there is no possibility
// to inherit it.
var setDelayLabel = function (activities) {
var today = moment().startOf('day');
var today = moment().startOf("day");
_.each(activities, function (activity) {
var to_display = '';
var deadline = moment(activity.date_deadline).startOf('day');
var diff = deadline.diff(today, 'days', true); // true means no rounding
var to_display = "";
var deadline = moment(activity.date_deadline).startOf("day");
var diff = deadline.diff(today, "days", true); // True means no rounding
if (diff === 0) {
to_display = _t('Today');
}else{
if(diff < 0){ // overdue
to_display = _t("Today");
} else if (diff < 0) {
// Overdue
if (diff === -1) {
to_display = _t('Yesterday');
to_display = _t("Yesterday");
} else {
to_display = _.str.sprintf(_t('%d days overdue'), Math.abs(diff));
to_display = _.str.sprintf(_t("%d days overdue"), Math.abs(diff));
}
}else{ // due
} else {
// Due
if (diff === 1) {
to_display = _t('Tomorrow');
to_display = _t("Tomorrow");
} else {
to_display = _.str.sprintf(_t('Due in %d days'), Math.abs(diff));
}
to_display = _.str.sprintf(_t("Due in %d days"), Math.abs(diff));
}
}
activity.label_delay = to_display;
});
// We do not want to show the activities that have been completed.
var open_activities = _.filter(activities, function (activity) {
return activity.done !== true
return activity.done !== true;
});
return open_activities;
};
@ -54,25 +54,33 @@ odoo.define('mail.Activity.done', function(require) {
*/
_render: function () {
_.each(this._activities, function (activity) {
var note = mailUtils.parseAndTransform(activity.note || '', mailUtils.inline);
var is_blank = (/^\s*$/).test(note);
var note = mailUtils.parseAndTransform(
activity.note || "",
mailUtils.inline
);
var is_blank = /^\s*$/.test(note);
if (!is_blank) {
activity.note = mailUtils.parseAndTransform(activity.note, mailUtils.addLink);
activity.note = mailUtils.parseAndTransform(
activity.note,
mailUtils.addLink
);
} else {
activity.note = '';
activity.note = "";
}
});
var activities = setDelayLabel(this._activities);
if (activities.length) {
var nbActivities = _.countBy(activities, 'state');
this.$el.html(QWeb.render('mail.activity_items', {
var nbActivities = _.countBy(activities, "state");
this.$el.html(
QWeb.render("mail.activity_items", {
activities: activities,
nbPlannedActivities: nbActivities.planned,
nbTodayActivities: nbActivities.today,
nbOverdueActivities: nbActivities.overdue,
dateFormat: time.getLangDateFormat(),
datetimeFormat: time.getLangDatetimeFormat(),
}));
})
);
} else {
this.$el.empty();
}

View File

@ -1,35 +1,41 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
from datetime import date
from odoo.tests.common import TransactionCase
class TestMailActivityDoneMethods(TransactionCase):
def setUp(self):
super(TestMailActivityDoneMethods, self).setUp()
self.employee = self.env['res.users'].create({
'company_id': self.env.ref("base.main_company").id,
'name': "Test User",
'login': "testuser",
'groups_id': [(6, 0, [self.env.ref('base.group_user').id])]
})
activity_type = self.env['mail.activity.type'].search(
[('name', '=', 'Meeting')], limit=1)
self.act1 = self.env['mail.activity'].create({
'activity_type_id': activity_type.id,
'res_id': self.env.ref("base.res_partner_1").id,
'res_model_id': self.env['ir.model']._get('res.partner').id,
'user_id': self.employee.id,
'date_deadline': date.today(),
})
self.employee = self.env["res.users"].create(
{
"company_id": self.env.ref("base.main_company").id,
"name": "Test User",
"login": "testuser",
"groups_id": [(6, 0, [self.env.ref("base.group_user").id])],
}
)
activity_type = self.env["mail.activity.type"].search(
[("name", "=", "Meeting")], limit=1
)
self.act1 = self.env["mail.activity"].create(
{
"activity_type_id": activity_type.id,
"res_id": self.env.ref("base.res_partner_1").id,
"res_model_id": self.env["ir.model"]._get("res.partner").id,
"user_id": self.employee.id,
"date_deadline": date.today(),
}
)
def test_mail_activity_done(self):
self.act1.done = True
self.assertEquals(self.act1.state, 'done')
self.assertEquals(self.act1.state, "done")
def test_systray_get_activities(self):
act_count = self.employee.sudo(self.employee).systray_get_activities()
self.assertEqual(len(act_count), 1,
"Number of activities should be equal to one")
self.assertEqual(
len(act_count), 1, "Number of activities should be equal to one"
)

View File

@ -15,7 +15,9 @@
<field name="date_done" />
</field>
<button name="action_done" position="attributes">
<attribute name="attrs">{'invisible':[('state', '=', 'done')]}</attribute>
<attribute
name="attrs"
>{'invisible':[('state', '=', 'done')]}</attribute>
</button>
</field>
</record>
@ -28,17 +30,26 @@
<field name="res_model_id" position="after">
<field name="done" />
<field name="active" />
<filter string="Completed Activities" name="activities_completed"
domain="[('active', '=', False), ('state', '=', 'done')]"/>
<filter
string="Completed Activities"
name="activities_completed"
domain="[('active', '=', False), ('state', '=', 'done')]"
/>
</field>
<filter name="activities_overdue" position="attributes">
<attribute name="domain">[('date_deadline', '&lt;', context_today().strftime('%Y-%m-%d'))]</attribute>
<attribute
name="domain"
>[('date_deadline', '&lt;', context_today().strftime('%Y-%m-%d'))]</attribute>
</filter>
<filter name="activities_today" position="attributes">
<attribute name="domain">[('date_deadline', '=', context_today().strftime('%Y-%m-%d'))]</attribute>
<attribute
name="domain"
>[('date_deadline', '=', context_today().strftime('%Y-%m-%d'))]</attribute>
</filter>
<filter name="activities_upcoming_all" position="attributes">
<attribute name="domain">[('date_deadline', '&gt;', context_today().strftime('%Y-%m-%d'))]</attribute>
<attribute
name="domain"
>[('date_deadline', '&gt;', context_today().strftime('%Y-%m-%d'))]</attribute>
</filter>
</field>
</record>
@ -49,19 +60,31 @@
<field name="inherit_id" ref="mail.res_partner_view_search_inherit_mail" />
<field name="arch" type="xml">
<filter name="activities_my" position="after">
<filter string="Open Activities" name="activities_open"
domain="[('activity_ids.active', '=', True)]"/>
<filter string="Completed Activities" name="activities_completed"
domain="[('activity_ids.active', '=', False), ('activity_ids.state', '=', 'done')]"/>
<filter
string="Open Activities"
name="activities_open"
domain="[('activity_ids.active', '=', True)]"
/>
<filter
string="Completed Activities"
name="activities_completed"
domain="[('activity_ids.active', '=', False), ('activity_ids.state', '=', 'done')]"
/>
</filter>
<filter name="activities_overdue" position="attributes">
<attribute name="domain">[('activity_ids.date_deadline', '&lt;', context_today().strftime('%Y-%m-%d'))]</attribute>
<attribute
name="domain"
>[('activity_ids.date_deadline', '&lt;', context_today().strftime('%Y-%m-%d'))]</attribute>
</filter>
<filter name="activities_today" position="attributes">
<attribute name="domain">[('activity_ids.date_deadline', '=', context_today().strftime('%Y-%m-%d'))]</attribute>
<attribute
name="domain"
>[('activity_ids.date_deadline', '=', context_today().strftime('%Y-%m-%d'))]</attribute>
</filter>
<filter name="activities_upcoming_all" position="attributes">
<attribute name="domain">[('activity_ids.date_deadline', '&gt;', context_today().strftime('%Y-%m-%d'))]</attribute>
<attribute
name="domain"
>[('activity_ids.date_deadline', '&gt;', context_today().strftime('%Y-%m-%d'))]</attribute>
</filter>
</field>
</record>
@ -77,8 +100,12 @@
</field>
<tree position="attributes">
<attribute name="decoration-muted">state == 'done'</attribute>
<attribute name="decoration-danger">date_deadline &lt; current_date and state != 'done'</attribute>
<attribute name="decoration-success">date_deadline == current_date and state != 'done'</attribute>
<attribute
name="decoration-danger"
>date_deadline &lt; current_date and state != 'done'</attribute>
<attribute
name="decoration-success"
>date_deadline == current_date and state != 'done'</attribute>
</tree>
</field>
</record>

View File

@ -4,9 +4,16 @@
Copyright 2018 Eficent <http://www.eficent.com>
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
-->
<template id="assets_backend" name="mail_activity_done assets" inherit_id="web.assets_backend">
<template
id="assets_backend"
name="mail_activity_done assets"
inherit_id="web.assets_backend"
>
<xpath expr="." position="inside">
<script type="text/javascript" src="/mail_activity_done/static/src/js/mail_activity.js"/>
<script
type="text/javascript"
src="/mail_activity_done/static/src/js/mail_activity.js"
/>
</xpath>
</template>
</odoo>