Skip to content

Commit

Permalink
Merge branch 'projectnumber-resetcounter' into 'main'
Browse files Browse the repository at this point in the history
Plugin projectnumber: Add command to reset projectnumber

See merge request reportcreator/reportcreator!792
  • Loading branch information
MWedl committed Dec 5, 2024
2 parents 403c71b + 1c1d090 commit 108c0d1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions plugins/projectnumber/management/commands/resetprojectnumber.py
Original file line number Diff line number Diff line change
@@ -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}')
13 changes: 13 additions & 0 deletions plugins/projectnumber/tests/test_plugin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +20,7 @@
)

from ..app import ProjectNumberPluginConfig
from ..management.commands import resetprojectnumber
from ..models import ProjectNumber


Expand Down Expand Up @@ -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

0 comments on commit 108c0d1

Please sign in to comment.