Skip to content

Commit 8a66a11

Browse files
committed
Record user who cancelled a task [RHELDST-460]
When cancelling a task there is no recorded information of who cancelled the task, simply that it was cancelled. This is unhelpful to users if someone else cancels their task or push. This change adds in the components for being able to show which user cancelled the task.
1 parent b4b486f commit 8a66a11

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
from django.conf import settings
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('hub', '0004_alter_task_worker'),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name='task',
16+
name='cancelled_by',
17+
field=models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE),
18+
),
19+
]

kobo/hub/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@ class Task(models.Model):
578578
resubmitted_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name="resubmitted_by1", on_delete=models.CASCADE)
579579
resubmitted_from = models.ForeignKey("self", null=True, blank=True, related_name="resubmitted_from1", on_delete=models.CASCADE)
580580

581+
cancelled_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
582+
581583
subtask_count = models.PositiveIntegerField(default=0, help_text=_("Subtask count.<br />This is a generated field."))
582584

583585
# override default *objects* Manager
@@ -895,12 +897,14 @@ def cancel_task(self, user=None, recursive=True):
895897

896898
try:
897899
self.__lock(self.worker_id, new_state=TASK_STATES["CANCELED"], initial_states=(TASK_STATES["FREE"], TASK_STATES["ASSIGNED"], TASK_STATES["OPEN"], TASK_STATES["CREATED"]))
900+
self.cancelled_by = user
901+
self.save()
898902
except (MultipleObjectsReturned, ObjectDoesNotExist):
899903
raise Exception("Cannot cancel task %d, state is %s" % (self.id, self.get_state_display()))
900904

901905
if recursive:
902906
for task in self.subtasks():
903-
task.cancel_task(recursive=True)
907+
task.cancel_task(user=user, recursive=True)
904908
self.logs.gzip_logs()
905909

906910
def cancel_subtasks(self):

kobo/hub/templates/task/detail.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ <h3>{% trans 'Details' %}</h3>
2727
<td class="{{ task.get_state_display }}">{{ task.get_state_display }}</td>
2828
</tr>
2929

30+
{% if task.state == "CANCELED" %}
31+
<tr>
32+
<th>{% trans "Cancelled by" %}</th>
33+
{% if task.cancelled_by %}
34+
<td>{{ task.cancelled_by }}</td>
35+
{% else %}
36+
<td>Unavailable</td>
37+
{% endif %}
38+
</tr>
39+
{% endif %}
40+
3041
<tr>
3142
<th>{% trans "Worker" %}</th>
3243
<td>{% if task.worker %}{{ task.worker }}{% endif %}</td>

tests/test_models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,10 +1243,11 @@ def test_cancel_task(self):
12431243
state=TASK_STATES['FREE'],
12441244
)
12451245

1246-
task.cancel_task()
1246+
task.cancel_task(self._user)
12471247

12481248
task = Task.objects.get(id=task.id)
12491249
self.assertEqual(task.state, TASK_STATES['CANCELED'])
1250+
self.assertEqual(task.cancelled_by, self._user)
12501251

12511252
def test_interrupt_task(self):
12521253
task = Task.objects.create(

tests/test_xmlrpc_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_get_tasks_filter_by_state(self):
187187

188188
def test_cancel_task(self):
189189
task_id = Task.create_task(self._user.username, 'task', 'method')
190-
ret = client.cancel_task(_make_request(), task_id)
190+
ret = client.cancel_task(_make_request(self._user), task_id)
191191
self.assertIsNone(ret)
192192

193193
t = Task.objects.get(id=task_id)

0 commit comments

Comments
 (0)