/
project_task_subtask.py
240 lines (215 loc) · 10.8 KB
/
project_task_subtask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Copyright 2017-2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2017-2018 manawi <https://github.com/manawi>
# Copyright 2017-2018 Karamov Ilmir <https://it-projects.info/team/ilmir-k>
# Copyright 2017-2018 iledarn <https://github.com/iledarn>
# Copyright 2017 Nicolas JEUDY <https://github.com/njeudy>
# Copyright 2018 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import models, fields, api
from odoo.tools import html_escape as escape
from odoo.exceptions import Warning as UserError
from odoo.tools.translate import _
SUBTASK_STATES = {'done': 'Done',
'todo': 'TODO',
'waiting': 'In Progress',
'cancelled': 'Cancelled'}
class ProjectTaskSubtask(models.Model):
_name = "project.task.subtask"
_inherit = ['mail.activity.mixin']
state = fields.Selection([(k, v) for k, v in list(SUBTASK_STATES.items())],
'Status', required=True, copy=False, default='todo')
name = fields.Char(required=True, string="Description")
reviewer_id = fields.Many2one('res.users', 'Created by', readonly=True, default=lambda self: self.env.user)
project_id = fields.Many2one("project.project", related='task_id.project_id', store=True)
user_id = fields.Many2one('res.users', 'Assigned to', required=True)
task_id = fields.Many2one('project.task', 'Task', ondelete='cascade', required=True, index="1")
task_state = fields.Char(string='Task state', related='task_id.stage_id.name', readonly=True)
hide_button = fields.Boolean(compute='_compute_hide_button')
recolor = fields.Boolean(compute='_compute_recolor')
deadline = fields.Datetime(string="Deadline")
@api.multi
def _compute_recolor(self):
for record in self:
if self.env.user == record.user_id and record.state == 'todo':
record.recolor = True
@api.multi
def _compute_hide_button(self):
for record in self:
if self.env.user not in [record.reviewer_id, record.user_id]:
record.hide_button = True
@api.multi
def _compute_reviewer_id(self):
for record in self:
record.reviewer_id = record.create_uid
@api.model
def _needaction_domain_get(self):
if self._needaction:
return [('state', '=', 'todo'), ('user_id', '=', self.env.uid)]
return []
@api.multi
def write(self, vals):
old_names = dict(list(zip(self.mapped('id'), self.mapped('name'))))
result = super(ProjectTaskSubtask, self).write(vals)
for r in self:
if vals.get('state'):
r.task_id.send_subtask_email(r.name, r.state, r.reviewer_id.id, r.user_id.id)
if self.env.user != r.reviewer_id and self.env.user != r.user_id:
raise UserError(_('Only users related to that subtask can change state.'))
if vals.get('name'):
r.task_id.send_subtask_email(r.name, r.state, r.reviewer_id.id, r.user_id.id, old_name=old_names[r.id])
if self.env.user != r.reviewer_id and self.env.user != r.user_id:
raise UserError(_('Only users related to that subtask can change state.'))
if vals.get('user_id'):
r.task_id.send_subtask_email(r.name, r.state, r.reviewer_id.id, r.user_id.id)
return result
@api.model
def create(self, vals):
result = super(ProjectTaskSubtask, self).create(vals)
vals = self._add_missing_default_values(vals)
task = self.env['project.task'].browse(vals.get('task_id'))
task.send_subtask_email(vals['name'], vals['state'], vals['reviewer_id'], vals['user_id'])
return result
@api.multi
def change_state_done(self):
for record in self:
record.state = 'done'
@api.multi
def change_state_todo(self):
for record in self:
record.state = 'todo'
@api.multi
def change_state_cancelled(self):
for record in self:
record.state = 'cancelled'
@api.multi
def change_state_waiting(self):
for record in self:
record.state = 'waiting'
class Task(models.Model):
_inherit = "project.task"
subtask_ids = fields.One2many('project.task.subtask', 'task_id', 'Subtask')
kanban_subtasks = fields.Text(compute='_compute_kanban_subtasks')
default_user = fields.Many2one('res.users', compute='_compute_default_user')
completion = fields.Integer('Completion', compute='_compute_completion')
completion_xml = fields.Text(compute='_compute_completion_xml')
@api.multi
def _compute_default_user(self):
for record in self:
if self.env.user != record.user_id and self.env.user != record.create_uid:
record.default_user = record.user_id
else:
if self.env.user != record.user_id:
record.default_user = record.user_id
elif self.env.user != record.create_uid:
record.default_user = record.create_uid
elif self.env.user == record.create_uid and self.env.user == record.user_id:
record.default_user = self.env.user
@api.multi
def _compute_kanban_subtasks(self):
for record in self:
result_string_td = ''
result_string_wt = ''
if record.subtask_ids:
task_todo_ids = record.subtask_ids.filtered(lambda x: x.state == 'todo' and (
x.user_id.id == record.env.user.id))
task_waiting_ids = record.subtask_ids.filtered(lambda x: x.state == 'waiting' and (
x.user_id.id == record.env.user.id))
if task_todo_ids:
tmp_string_td = escape(': {0}'.format(len(task_todo_ids)))
result_string_td += '<li><b>TODO{}</b></li>'.format(tmp_string_td)
if task_waiting_ids:
tmp_string_wt = escape(': {0}'.format(len(task_waiting_ids)))
result_string_wt += '<li><b>In Progress{}</b></li>'.format(tmp_string_wt)
record.kanban_subtasks = '<div class="kanban_subtasks"><ul>' + \
result_string_td + result_string_wt + '</ul></div>'
@api.multi
def _compute_completion(self):
for record in self:
record.completion = record.task_completion()
@api.multi
def _compute_completion_xml(self):
for record in self:
active_subtasks = record.subtask_ids and \
record.subtask_ids.filtered(lambda x: x.user_id.id == record.env.user.id and
x.state != 'cancelled')
if not active_subtasks:
record.completion_xml = """
<div class="task_progress">
</div>
"""
continue
completion = record.task_completion()
color = 'bg-success-full'
if completion < 50:
color = 'bg-danger-full'
record.completion_xml = """
<div class="task_progress">
<div class="progress_info">
Your Checklist:
</div>
<div class ="o_kanban_counter_progress progress task_progress_bar">
<div data-filter="done"
class ="progress-bar {1} o_bar_has_records task_progress_bar_done"
data-original-title="1 done"
style="width: {0}%;">
</div>
<div data-filter="blocked"
class ="progress-bar bg-danger-full"
data-original-title="0 blocked">
</div>
</div>
<div class="task_completion"> {0}% </div>
</div>
""".format(int(completion), color)
def task_completion(self):
user_task_ids = self.subtask_ids.filtered(lambda x: x.user_id.id == self.env.user.id and x.state != 'cancelled')
if not user_task_ids:
return 100
user_done_task_ids = user_task_ids.filtered(lambda x: x.state == 'done')
return (len(user_done_task_ids) / len(user_task_ids)) * 100
@api.multi
def send_subtask_email(self, subtask_name, subtask_state, subtask_reviewer_id, subtask_user_id, old_name=None):
for r in self:
body = ''
reviewer = self.env["res.users"].browse(subtask_reviewer_id)
user = self.env["res.users"].browse(subtask_user_id)
state = SUBTASK_STATES[subtask_state]
if subtask_state == 'done':
state = '<span style="color:#080">' + state + '</span>'
if subtask_state == 'todo':
state = '<span style="color:#A00">' + state + '</span>'
if subtask_state == 'cancelled':
state = '<span style="color:#777">' + state + '</span>'
if subtask_state == 'waiting':
state = '<span style="color:#b818ce">' + state + '</span>'
partner_ids = []
subtype = 'project_task_subtask.subtasks_subtype'
if user == self.env.user and reviewer == self.env.user:
body = '<p>' + '<strong>' + state + '</strong>: ' + escape(subtask_name)
subtype = False
elif self.env.user == reviewer:
body = '<p>' + escape(user.name) + ', <br><strong>' + state + '</strong>: ' + escape(subtask_name)
partner_ids = [user.partner_id.id]
elif self.env.user == user:
body = '<p>' + escape(reviewer.name) + ', <em style="color:#999">I updated checklist item assigned to me:</em> <br><strong>' + state + '</strong>: ' + escape(subtask_name)
partner_ids = [reviewer.partner_id.id]
else:
body = '<p>' + escape(user.name) + ', ' + escape(reviewer.name) + ', <em style="color:#999">I updated checklist item, now its assigned to ' + escape(user.name) + ': </em> <br><strong>' + state + '</strong>: ' + escape(subtask_name)
partner_ids = [user.partner_id.id, reviewer.partner_id.id]
if old_name:
body = body + '<br><em style="color:#999">Updated from</em><br><strong>' + state + '</strong>: ' + escape(old_name) + '</p>'
else:
body = body + '</p>'
r.message_post(message_type='comment',
subtype=subtype,
body=body,
partner_ids=partner_ids)
@api.multi
def copy(self, default=None):
task = super(Task, self).copy(default)
for subtask in self.subtask_ids:
subtask.copy({
'task_id': task.id,
'state': subtask.state,
})
return task