diff --git a/operations/templatetags/operations.py b/operations/templatetags/operations.py index a125ddea..08089de5 100644 --- a/operations/templatetags/operations.py +++ b/operations/templatetags/operations.py @@ -11,6 +11,7 @@ from django.utils.translation import gettext, ngettext, pgettext, pgettext_lazy from operations.jobs import take_remembered from operations.models import Operation +from operations.utils import words as _words register = template.Library() @@ -54,6 +55,12 @@ def state_label(state): return STATE_LABELS[state] +@register.filter +def words(messages): + """A job's stored messages (operations.utils.say) in the reader's language.""" + return _words(messages) + + @register.filter def duration(start, end): """The time from start to end, in seconds under a minute: timesince counts minutes. diff --git a/vmbackups/migrations/0006_run_note_messages.py b/vmbackups/migrations/0006_run_note_messages.py new file mode 100644 index 00000000..7e82f29f --- /dev/null +++ b/vmbackups/migrations/0006_run_note_messages.py @@ -0,0 +1,13 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + """A run's note becomes messages, shown in the reader's language; the + English notes of earlier runs are dropped.""" + + dependencies = [("vmbackups", "0005_owners")] + + operations = [ + migrations.RemoveField(model_name="backuprun", name="note"), + migrations.AddField(model_name="backuprun", name="note", field=models.JSONField(default=list)), + ] diff --git a/vmbackups/models.py b/vmbackups/models.py index f4c4befc..4c61a152 100644 --- a/vmbackups/models.py +++ b/vmbackups/models.py @@ -78,7 +78,7 @@ class BackupRun(models.Model): day = models.DateField() # the local day of the schedule's time state = models.CharField(max_length=16, choices=STATES) operation = models.ForeignKey(Operation, on_delete=models.SET_NULL, null=True) - note = models.CharField(max_length=255, blank=True) + note = models.JSONField(default=list) # messages (operations.utils.say), shown in the reader's language created_at = models.DateTimeField(auto_now_add=True) class Meta: diff --git a/vmbackups/schedule.py b/vmbackups/schedule.py index ce42fa5c..14c58d25 100644 --- a/vmbackups/schedule.py +++ b/vmbackups/schedule.py @@ -17,7 +17,7 @@ from libvirt import libvirtError from computes.models import Compute from operations import claims, jobs from operations.models import Operation -from operations.utils import safe_text +from operations.utils import safe_text, said, say from vrtManager.util import ConnectionFailed from .jobs import beyond_keep @@ -50,7 +50,7 @@ def run_schedules(now=None): continue in_maintenance = Compute.objects.filter(pk=vm.compute_id, maintenance_since__isnull=False).exists() if not open_: - _record(policy, vm, day, BackupRun.SKIPPED, IN_MAINTENANCE if in_maintenance else NOT_STARTED) + _record(policy, vm, day, BackupRun.SKIPPED, say(IN_MAINTENANCE if in_maintenance else NOT_STARTED)) elif not in_maintenance: # deferred while the window lasts due.setdefault(vm.compute_id, []).append((policy.pk, vm.pk, day, start)) for compute_id, items in due.items(): @@ -75,9 +75,9 @@ def occurrences(policy, now): yield day, start -def _record(policy, vm, day, state, note="", operation=None): +def _record(policy, vm, day, state, note=(), operation=None): BackupRun.objects.get_or_create(policy=policy, instance=vm, day=day, - defaults={"state": state, "note": note[:255], "operation": operation}) + defaults={"state": state, "note": list(note), "operation": operation}) def _active_superuser(user): @@ -106,7 +106,7 @@ def _submit(policy_id, vm_id, day, start): if Compute.objects.filter(pk=vm.compute_id, maintenance_since__isnull=False).exists(): return if not _active_superuser(policy.owner): - _record(policy, vm, day, BackupRun.FAILED, NO_OWNER) + _record(policy, vm, day, BackupRun.FAILED, say(NO_OWNER)) return # the same key for the same run: a worker stopped before the row was written gets the same job back key = f"bk-{policy.pk}-{vm.pk}-{day:%Y%m%d}" @@ -117,7 +117,7 @@ def _submit(policy_id, vm_id, day, start): except (claims.Busy, ConnectionFailed): return except (libvirtError, PermissionDenied, jobs.SubmissionConflict) as err: # it would be refused again - _record(policy, vm, day, BackupRun.FAILED, safe_text(str(err) or type(err).__name__)) + _record(policy, vm, day, BackupRun.FAILED, said(err) if str(err) else say(type(err).__name__)) return except Exception as err: logger.error("schedule %s: backup of %s not submitted: %s", policy.name, vm.name, safe_text(err)) diff --git a/vmbackups/templates/vmbackups/policy.html b/vmbackups/templates/vmbackups/policy.html index a73f0271..12593e56 100644 --- a/vmbackups/templates/vmbackups/policy.html +++ b/vmbackups/templates/vmbackups/policy.html @@ -2,6 +2,7 @@ {% load i18n %} {% load static %} {% load bootstrap_icons %} +{% load operations %} {% block title %}{{ policy.name }} - {% trans "Backup schedules" %}{% endblock %} {% block style %}{% endblock %} @@ -36,7 +37,7 @@ {% if run.state == "submitted" %}{% if run.operation_id %}{% trans "Backup job" %}{% else %}{% trans "Started" %}{% endif %} {% elif run.state == "skipped" %}{% trans "Skipped" %} {% else %}{% trans "Failed" %}{% endif %} - {% if run.note %}
{% trans run.note %}
{% endif %} + {% if run.note %}
{{ run.note|words }}
{% endif %} {% endfor %} diff --git a/vmbackups/tests/test_policies.py b/vmbackups/tests/test_policies.py index a43e4bda..fa738d0d 100644 --- a/vmbackups/tests/test_policies.py +++ b/vmbackups/tests/test_policies.py @@ -12,6 +12,7 @@ from computes.models import Compute from instances.models import Instance from instances.tests.test_page_controls import PageControls from logs.models import Logs +from operations.utils import say from vmbackups.models import Backup, BackupPolicy, BackupRun, BackupTarget, BackupTargetPool @@ -85,7 +86,7 @@ class PolicyPageTests(TestCase): def test_edit_and_the_runs(self): policy = self.schedule() BackupRun.objects.create(policy=policy, instance=self.vm1, day=datetime.date.today(), state=BackupRun.SKIPPED, - note="The host was in maintenance") + note=say("The host was in maintenance")) url = reverse("vmbackups:policy", args=[policy.pk]) page = self.client.get(url) self.assertContains(page, "The host was in maintenance") diff --git a/vmbackups/tests/test_schedule.py b/vmbackups/tests/test_schedule.py index fc69cf9e..13619fe6 100644 --- a/vmbackups/tests/test_schedule.py +++ b/vmbackups/tests/test_schedule.py @@ -9,7 +9,7 @@ from unittest.mock import MagicMock, patch import libvirt from django.contrib.auth import get_user_model from django.test import TransactionTestCase, override_settings -from django.utils import timezone +from django.utils import timezone, translation from appsettings.settings import get_settings from computes.models import Compute @@ -17,6 +17,7 @@ from instances.models import Instance from operations import jobs from operations.jobs import Context from operations.models import Operation, acquire +from operations.utils import words from vmbackups import schedule from vmbackups.jobs import CheckTarget, DeleteBackup, _beyond_keep from vmbackups.models import Backup, BackupFile, BackupPolicy, BackupRun, BackupTarget, BackupTargetPool @@ -192,7 +193,7 @@ class ScheduleTests(TransactionTestCase): self.pass_at(at(2026, 11, 17, 8, 1)) run = BackupRun.objects.get() self.assertEqual(run.state, BackupRun.SKIPPED) - self.assertIn("maintenance", run.note) + self.assertIn("maintenance", words(run.note)) self.assertEqual(self.creates(), []) def test_maintenance_ended_inside_the_window(self): @@ -212,7 +213,7 @@ class ScheduleTests(TransactionTestCase): self.pass_at(at(2026, 11, 17, 8, 1)) # the worker was down from before 02:00 run = BackupRun.objects.get() self.assertEqual(run.state, BackupRun.SKIPPED) - self.assertIn("6 hours", run.note) + self.assertIn("6 hours", words(run.note)) self.assertEqual(self.creates(), []) def test_a_new_schedule_does_not_skip_the_times_before_it(self): @@ -270,14 +271,16 @@ class ScheduleTests(TransactionTestCase): self.pass_at(at(2026, 11, 17, 2, 0)) run = BackupRun.objects.get() self.assertEqual(run.state, BackupRun.FAILED) - self.assertIn("owner", run.note) + self.assertIn("owner", words(run.note)) def test_a_vm_that_cannot_be_backed_up_fails_once_with_the_reason(self): - self.proxy.backup_plan.side_effect = OperationError("%(vm)s has TPM state; TPM backups come later", vm="vm1") + self.proxy.backup_plan.side_effect = OperationError("%(vm)s has TPM state; shut it down to back it up", vm="vm1") self.pass_at(at(2026, 11, 17, 2, 0)) self.pass_at(at(2026, 11, 17, 2, 1)) run = BackupRun.objects.get() - self.assertEqual((run.state, run.note), (BackupRun.FAILED, "vm1 has TPM state; TPM backups come later")) + self.assertEqual((run.state, words(run.note)), (BackupRun.FAILED, "vm1 has TPM state; shut it down to back it up")) + with translation.override("tr"): # in the reader's language, its values in place + self.assertEqual(words(run.note), "vm1 TPM durumu taşıyor; yedeklemek için kapatın") def test_an_unreachable_host_is_tried_again(self): from vrtManager.util import ConnectionFailed