improve fitnotes importer
This commit is contained in:
parent
9ec636fbef
commit
d70de69f3d
@ -26,30 +26,24 @@ def import_fitnotes_db(filename, user, fitnotes_to_lift=DEFAULT_MAPPING):
|
|||||||
# lift name => id
|
# lift name => id
|
||||||
lift_ids = {_clean_name(l.name): l.id for l in Lift.objects.all()}
|
lift_ids = {_clean_name(l.name): l.id for l in Lift.objects.all()}
|
||||||
|
|
||||||
# build mapping FitNotes exercise id => our lift id
|
|
||||||
lift_id_mapping = {}
|
|
||||||
|
|
||||||
conn = sqlite3.connect(filename)
|
conn = sqlite3.connect(filename)
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
for fnid, ename in cur.execute('SELECT _id, name FROM exercise WHERE exercise_type_id=0'):
|
|
||||||
cleaned = _clean_name(ename)
|
|
||||||
|
|
||||||
if cleaned not in fitnotes_to_lift:
|
|
||||||
lift_id_mapping[fnid] = cleaned
|
|
||||||
else:
|
|
||||||
lift_id_mapping[fnid] = lift_ids[fitnotes_to_lift[cleaned]]
|
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
Set.objects.filter(source='fitnotes').delete()
|
Set.objects.filter(source='fitnotes').delete()
|
||||||
for fnid, date, weight_kg, reps in cur.execute(
|
count = 0
|
||||||
'SELECT exercise_id, date, metric_weight, reps FROM training_log'):
|
for fname, date, weight_kg, reps in cur.execute(
|
||||||
|
'SELECT name, date, metric_weight, reps FROM training_log, exercise '
|
||||||
|
'WHERE exercise_type_id=0 and exercise_id=exercise._id'
|
||||||
|
):
|
||||||
|
|
||||||
# error if mapping wasn't found and there's a workout using it
|
try:
|
||||||
if isinstance(lift_id_mapping[fnid], str):
|
lift_id = lift_ids[fitnotes_to_lift[_clean_name(fname)]]
|
||||||
raise ValueError('no known conversion for fitnotes exercise "{}"'.format(
|
except KeyError:
|
||||||
lift_id_mapping[fnid]))
|
raise ValueError('no known conversion for fitnotes exercise "{}"'.format(fname))
|
||||||
|
|
||||||
lift_id = lift_id_mapping[fnid]
|
|
||||||
|
|
||||||
Set.objects.create(lift_id=lift_id, date=date, weight_kg=weight_kg, reps=reps,
|
Set.objects.create(lift_id=lift_id, date=date, weight_kg=weight_kg, reps=reps,
|
||||||
source='fitnotes', user=user)
|
source='fitnotes', user=user)
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
return count
|
||||||
|
BIN
fitnotes/testdata/baddata.fitnotes
vendored
BIN
fitnotes/testdata/baddata.fitnotes
vendored
Binary file not shown.
@ -31,7 +31,8 @@ class TestFitnotesImport(TestCase):
|
|||||||
|
|
||||||
def test_basic_import(self):
|
def test_basic_import(self):
|
||||||
# ensure that the data comes in
|
# ensure that the data comes in
|
||||||
import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.good_mapping)
|
num = import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.good_mapping)
|
||||||
|
assert num == 9
|
||||||
assert Set.objects.filter(lift=self.bench).count() == 4
|
assert Set.objects.filter(lift=self.bench).count() == 4
|
||||||
assert Set.objects.filter(lift=self.squat).count() == 5
|
assert Set.objects.filter(lift=self.squat).count() == 5
|
||||||
|
|
||||||
@ -53,10 +54,9 @@ class TestFitnotesImport(TestCase):
|
|||||||
import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.bad_mapping)
|
import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.bad_mapping)
|
||||||
assert Set.objects.filter(lift=self.bench).count() == 0
|
assert Set.objects.filter(lift=self.bench).count() == 0
|
||||||
|
|
||||||
def test_bad_data_import(self):
|
def test_bad_data_doesnt_overwrite(self):
|
||||||
# good db then bad db, should fail without screwing up existing data
|
# good db then bad db, should fail without screwing up existing data
|
||||||
import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.good_mapping)
|
import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.good_mapping)
|
||||||
with self.assertRaises(Exception):
|
with self.assertRaises(ValueError):
|
||||||
# baddata.fitnotes has all lift ids set to 9999
|
import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.bad_mapping)
|
||||||
import_fitnotes_db('fitnotes/testdata/baddata.fitnotes', self.user, self.good_mapping)
|
|
||||||
assert Set.objects.count() == 9
|
assert Set.objects.count() == 9
|
||||||
|
@ -3,5 +3,5 @@ from django.conf.urls import url
|
|||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^upload/$', views.fitnotes_upload),
|
url(r'^$', views.fitnotes_upload),
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user