Skip to content

Commit 7d075da

Browse files
authored
Merge pull request #117 from jedie/dev
Update/fix "fill_basedata" command
2 parents 6e26b7c + a08c481 commit 7d075da

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ Because this is a project and not really a reuse-able-app ;)
221221

222222
[comment]: <> (✂✂✂ auto generated history start ✂✂✂)
223223

224+
* [v0.18.1](https://github.com/jedie/django-for-runners/compare/v0.18.0...v0.18.1)
225+
* 2024-08-02 - Update/fix "fill_basedata" command
224226
* [v0.18.0](https://github.com/jedie/django-for-runners/compare/v0.17.4...v0.18.0)
225227
* 2024-08-02 - Update README.md
226228
* 2024-08-02 - Expand test matrix with 3.12 and remove 3.9 support
@@ -248,12 +250,12 @@ Because this is a project and not really a reuse-able-app ;)
248250
* 2023-04-04 - Switch to "managed-django-project" template:
249251
* [v0.17.3](https://github.com/jedie/django-for-runners/compare/v0.17.2...v0.17.3)
250252
* 2023-04-03 - Bugfix check if Postgres is used and migration not done
251-
* [v0.17.2](https://github.com/jedie/django-for-runners/compare/v0.17.1...v0.17.2)
252-
* 2023-04-02 - release 0.17.2
253-
* 2023-04-02 - Bugfix adding all template files
254253

255254
<details><summary>Expand older history entries ...</summary>
256255

256+
* [v0.17.2](https://github.com/jedie/django-for-runners/compare/v0.17.1...v0.17.2)
257+
* 2023-04-02 - release 0.17.2
258+
* 2023-04-02 - Bugfix adding all template files
257259
* [v0.17.1](https://github.com/jedie/django-for-runners/compare/v0.17.0...v0.17.1)
258260
* 2023-04-02 - Bugfix packaging and missing files
259261
* 2023-04-02 - update link to https://github.com/kbr/autotask

for_runners/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
Store your GPX tracks of your running (or other sports activity) in django.
44
"""
55

6-
__version__ = '0.18.0'
6+
__version__ = '0.18.1'
77
__author__ = 'Jens Diemer <[email protected]>'

for_runners/management/commands/fill_basedata.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,32 @@
55
"""
66

77

8-
from django import forms
98
from django.conf import settings
109

1110
# https://github.com/jedie/django-for-runners
1211
from for_runners.management.commands.base import BaseCommand
1312
from for_runners.models import DistanceModel, GpxModel
1413

1514

16-
class DistanceForm(forms.ModelForm):
17-
class Meta:
18-
model = DistanceModel
19-
fields = ['distance_km']
20-
21-
2215
class Command(BaseCommand):
2316
help = "Create some base data"
2417

2518
def handle(self, *args, **options):
26-
2719
updated_needed = False
28-
2920
for distance_km in settings.BASE_IDEAL_TRACK_LENGTHS:
30-
form = DistanceForm(data={'distance_km': distance_km})
31-
if not form.is_valid():
32-
raise AssertionError(
33-
f'settings.BASE_IDEAL_TRACK_LENGTHS {distance_km!r} not valid: {form.errors["distance_km"]}'
34-
)
35-
36-
distance_km = form.cleaned_data['distance_km']
3721
obj, created = DistanceModel.objects.get_or_create(distance_km=distance_km)
3822
if created:
3923
updated_needed = True
4024
self.stdout.write(f"Create: {repr(obj)}")
4125
else:
26+
obj.full_clean()
4227
self.stdout.write(f"{repr(obj)} already exists, ok.")
4328

4429
if not updated_needed:
45-
print("No track updated needed, ok.")
30+
self.stdout.write("No track updated needed, ok.")
4631
else:
32+
print("Update existing tracks", end="")
4733
for track in GpxModel.objects.all():
4834
track.save()
4935
print(".", end="", flush=True)
50-
print()
36+
self.stdout.write('done.')

for_runners/tests/test_fill_basedata.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import io
2+
13
from django.conf import settings
24
from django.core.management import call_command
35
from django.test import TestCase
@@ -12,6 +14,23 @@ def test_import_no_username_given(self):
1214
assert len(settings.BASE_IDEAL_TRACK_LENGTHS) > 0
1315
assert DistanceModel.objects.count() == 0
1416

15-
call_command(FillBasedataCommand())
17+
out = io.StringIO()
18+
call_command(FillBasedataCommand(), stdout=out)
19+
20+
distances = sorted(DistanceModel.objects.values_list('distance_km', flat=True))
21+
self.assertEqual(distances, sorted(settings.BASE_IDEAL_TRACK_LENGTHS))
22+
23+
output = out.getvalue()
24+
self.assertIn('Create: <DistanceModel: 21.0975 km>', output)
25+
self.assertIn('Create: <DistanceModel: 42.195 km>', output)
26+
27+
# Run again will not change anything:
28+
out = io.StringIO()
29+
call_command(FillBasedataCommand(), stdout=out)
30+
31+
self.assertEqual(sorted(DistanceModel.objects.values_list('distance_km', flat=True)), distances)
1632

17-
assert DistanceModel.objects.count() == len(settings.BASE_IDEAL_TRACK_LENGTHS)
33+
output = out.getvalue()
34+
self.assertIn('<DistanceModel: 21.0975 km> already exists, ok.', output)
35+
self.assertIn('<DistanceModel: 42.195 km> already exists, ok.', output)
36+
self.assertIn('No track updated needed, ok.', output)

0 commit comments

Comments
 (0)