Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve backfill_changesets command #685

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading