From 1c1d09007ecfd95aca96765846caf217e9fce2b3 Mon Sep 17 00:00:00 2001 From: Michael Wedl Date: Thu, 5 Dec 2024 09:31:40 +0100 Subject: [PATCH] Plugin projectnumber: Add command to reset projectnumber --- CHANGELOG.md | 4 ++++ plugins/projectnumber/management/__init__.py | 0 .../management/commands/__init__.py | 0 .../management/commands/resetprojectnumber.py | 23 +++++++++++++++++++ .../projectnumber/tests/test_plugin_api.py | 13 +++++++++++ 5 files changed, 40 insertions(+) create mode 100644 plugins/projectnumber/management/__init__.py create mode 100644 plugins/projectnumber/management/commands/__init__.py create mode 100644 plugins/projectnumber/management/commands/resetprojectnumber.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 539abc45b..30b8659f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Upcoming +* Plugin projectnumber: Add manage.py command to reset projectnumber + + ## v2024.96 - 2024-12-04 * Plugin: webhooks at certain events * Plugin: automatically assign project numbers diff --git a/plugins/projectnumber/management/__init__.py b/plugins/projectnumber/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/projectnumber/management/commands/__init__.py b/plugins/projectnumber/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plugins/projectnumber/management/commands/resetprojectnumber.py b/plugins/projectnumber/management/commands/resetprojectnumber.py new file mode 100644 index 000000000..2e289be21 --- /dev/null +++ b/plugins/projectnumber/management/commands/resetprojectnumber.py @@ -0,0 +1,23 @@ +from typing import Any + +from django.core.management.base import BaseCommand, CommandError, CommandParser + +from ...models import ProjectNumber + + +class Command(BaseCommand): + help = 'Reset the project number counter' + + def add_arguments(self, parser: CommandParser) -> None: + parser.add_argument('value', nargs='?', type=int, default=0, help='New project number counter value') + + def handle(self, *args: Any, value=0, **options: Any) -> str | None: + if value < 0: + raise CommandError('Project number counter must be a non-negative integer') + + counter, _ = ProjectNumber.objects.update_or_create( + pk=1, + defaults={'current_id': value}, + create_defaults={'current_id': value}, + ) + print(f'Project number was reset to {counter.current_id}') diff --git a/plugins/projectnumber/tests/test_plugin_api.py b/plugins/projectnumber/tests/test_plugin_api.py index 3d74deedd..e5c36ca5d 100644 --- a/plugins/projectnumber/tests/test_plugin_api.py +++ b/plugins/projectnumber/tests/test_plugin_api.py @@ -3,6 +3,7 @@ import pytest from django.apps import apps +from django.core.management import call_command from django.template.defaultfilters import date from django.template.defaulttags import NowNode from django.utils import dateparse @@ -19,6 +20,7 @@ ) from ..app import ProjectNumberPluginConfig +from ..management.commands import resetprojectnumber from ..models import ProjectNumber @@ -99,3 +101,14 @@ def test_on_project_saved(self, template, expected): # Check project counter increment counter.refresh_from_db() assert counter.current_id == 1 + + def test_command_resetprojectnumber(self): + # Reset project counter to a specific value + call_command(resetprojectnumber.Command(), '10') + counter = ProjectNumber.objects.get(pk=1) + assert counter.current_id == 10 + + # Reset to 0 + call_command(resetprojectnumber.Command()) + counter.refresh_from_db() + assert counter.current_id == 0