Skip to content

Commit 6c6c36c

Browse files
authored
Add backfill_changesets command (OSMCha#675)
1 parent e3a0ea4 commit 6c6c36c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from datetime import date
2+
3+
from django.core.management.base import BaseCommand
4+
5+
from ...models import Changeset
6+
from ...tasks import create_changeset
7+
8+
9+
class Command(BaseCommand):
10+
help = """Backfill missing changesets in a date range.
11+
Start and end dates should be in YYYY-MM-DD format."""
12+
13+
def add_arguments(self, parser):
14+
parser.add_argument("start_date", nargs=1, type=str)
15+
parser.add_argument("end_date", nargs=1, type=str)
16+
17+
def handle(self, *args, **options):
18+
try:
19+
end_date = date.fromisoformat(options["end_date"][0])
20+
except (ValueError, TypeError):
21+
end_date = date.today()
22+
23+
cl = Changeset.objects.filter(
24+
date__gte=date.fromisoformat(options["start_date"][0]),
25+
date__lte=end_date
26+
).values_list('id')
27+
cl = [c[0] for c in cl]
28+
29+
id = cl[len(cl) - 1]
30+
final = cl[0]
31+
while id < final:
32+
if id not in cl:
33+
create_changeset(id)
34+
id = id + 1

osmchadjango/changeset/tests/test_management_commands.py

+14
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,17 @@ def test_error(self):
103103
)
104104
self.assertIn('Verify the SuspicionReasons ids.', self.out.getvalue())
105105
self.assertIn('One or both of them does not exist.', self.out.getvalue())
106+
107+
108+
class TestBackfillChangesets(TestCase):
109+
def setUp(self):
110+
ChangesetFactory(id='1234', date=datetime(2021,1,2))
111+
ChangesetFactory(id='1238', date=datetime(2021,1,3))
112+
113+
def test_backfill(self):
114+
call_command("backfill_changesets", '2021-01-01', '2021-01-04')
115+
cl = [i[0] for i in Changeset.objects.all().values_list('id')]
116+
self.assertEqual(len(cl), 5)
117+
self.assertIn(1235, cl)
118+
self.assertIn(1236, cl)
119+
self.assertIn(1237, cl)

0 commit comments

Comments
 (0)