diff --git a/fitnotes/importer.py b/fitnotes/importer.py index b5cf32d..2c681ff 100644 --- a/fitnotes/importer.py +++ b/fitnotes/importer.py @@ -26,30 +26,24 @@ def import_fitnotes_db(filename, user, fitnotes_to_lift=DEFAULT_MAPPING): # lift name => id 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) 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(): Set.objects.filter(source='fitnotes').delete() - for fnid, date, weight_kg, reps in cur.execute( - 'SELECT exercise_id, date, metric_weight, reps FROM training_log'): + count = 0 + 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 - if isinstance(lift_id_mapping[fnid], str): - raise ValueError('no known conversion for fitnotes exercise "{}"'.format( - lift_id_mapping[fnid])) - - lift_id = lift_id_mapping[fnid] + try: + lift_id = lift_ids[fitnotes_to_lift[_clean_name(fname)]] + except KeyError: + raise ValueError('no known conversion for fitnotes exercise "{}"'.format(fname)) Set.objects.create(lift_id=lift_id, date=date, weight_kg=weight_kg, reps=reps, source='fitnotes', user=user) + count += 1 + + return count diff --git a/fitnotes/testdata/baddata.fitnotes b/fitnotes/testdata/baddata.fitnotes deleted file mode 100644 index a0de8a8..0000000 Binary files a/fitnotes/testdata/baddata.fitnotes and /dev/null differ diff --git a/fitnotes/tests.py b/fitnotes/tests.py index 0a38095..6e1cb25 100644 --- a/fitnotes/tests.py +++ b/fitnotes/tests.py @@ -31,7 +31,8 @@ class TestFitnotesImport(TestCase): def test_basic_import(self): # 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.squat).count() == 5 @@ -53,10 +54,9 @@ class TestFitnotesImport(TestCase): import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.bad_mapping) 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 import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.good_mapping) - with self.assertRaises(Exception): - # baddata.fitnotes has all lift ids set to 9999 - import_fitnotes_db('fitnotes/testdata/baddata.fitnotes', self.user, self.good_mapping) + with self.assertRaises(ValueError): + import_fitnotes_db('fitnotes/testdata/example.fitnotes', self.user, self.bad_mapping) assert Set.objects.count() == 9 diff --git a/fitnotes/urls.py b/fitnotes/urls.py index 663ba1c..a4e9046 100644 --- a/fitnotes/urls.py +++ b/fitnotes/urls.py @@ -3,5 +3,5 @@ from django.conf.urls import url from . import views urlpatterns = [ - url(r'^upload/$', views.fitnotes_upload), + url(r'^$', views.fitnotes_upload), ]