Skip to content

Commit

Permalink
Improve backfill_changesets command (#685)
Browse files Browse the repository at this point in the history
* Set backfill_changesets default behaviour to backfill the last day changesets

* Fix test command arguments

* Fix typo

* Fix start_date and end_date access
  • Loading branch information
willemarcel authored Apr 15, 2024
1 parent d9b1beb commit f40ce72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
21 changes: 13 additions & 8 deletions osmchadjango/changeset/management/commands/backfill_changesets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import date
from datetime import date, datetime, timedelta

from django.core.management.base import BaseCommand

Expand All @@ -11,19 +11,24 @@ class Command(BaseCommand):
Start and end dates should be in YYYY-MM-DD format."""

def add_arguments(self, parser):
parser.add_argument("start_date", nargs=1, type=str)
parser.add_argument("end_date", nargs=1, type=str)
parser.add_argument("--start_date", type=str)
parser.add_argument("--end_date", type=str)

def handle(self, *args, **options):
# if start_date is not defined, set it as yesterday
try:
end_date = date.fromisoformat(options["end_date"][0])
start_date = date.fromisoformat(options["start_date"])
except (ValueError, TypeError):
end_date = date.today()
start_date = date.today() - timedelta(days=1)
# if end_date is not defined, set it as today
try:
end_date = date.fromisoformat(options["end_date"])
except (ValueError, TypeError):
end_date = datetime.now()

cl = Changeset.objects.filter(
date__gte=date.fromisoformat(options["start_date"][0]),
date__lte=end_date
).values_list('id')
date__gte=start_date, date__lte=end_date
).values_list("id")
cl = [c[0] for c in cl]

id = cl[len(cl) - 1]
Expand Down
4 changes: 3 additions & 1 deletion osmchadjango/changeset/tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ def setUp(self):
ChangesetFactory(id='1238', date=datetime(2021,1,3))

def test_backfill(self):
call_command("backfill_changesets", '2021-01-01', '2021-01-04')
call_command(
"backfill_changesets", "--start_date=2021-01-01", "--end_date=2021-01-04"
)
cl = [i[0] for i in Changeset.objects.all().values_list('id')]
self.assertEqual(len(cl), 5)
self.assertIn(1235, cl)
Expand Down

0 comments on commit f40ce72

Please sign in to comment.