From d70de69f3d4382ab5a2b2771bb46bc4333948bfc Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 12 Apr 2015 23:06:52 -0400 Subject: [PATCH] improve fitnotes importer --- fitnotes/importer.py | 30 ++++++++++++----------------- fitnotes/testdata/baddata.fitnotes | Bin 73728 -> 0 bytes fitnotes/tests.py | 10 +++++----- fitnotes/urls.py | 2 +- 4 files changed, 18 insertions(+), 24 deletions(-) delete mode 100644 fitnotes/testdata/baddata.fitnotes 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 a0de8a85a6343ec84153f3b230c65c2c11aa4306..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 73728 zcmeI5e{38_7035{zMVb$?$-I?TpZiU9!Z)Uv6~-F+NQr^`+RXtY{&6ANmFRAoAdg- z^4_hpx0l3%KS_^GoV%O%X6C)mym>Q|lP8%|W2S3pv$j*vUF{C3Lka|>0Zo%6X`}d| z7w=K&?^f}i%L4Qh7N1vo*(mkjkfS8+2#ibWH`TjiH)5ekZ~NEVu804qyrFDlQ}QQ6 zpJ{zK_y8%~>d&=e2aEUi2AgO$^WVo(b%$NMvjj5^h;B;D>9vm7=YksM=H*3yl zqZ8BVk@S={IW;;yICV}tkv^vlo}NBAIzg4j(-YHuTETD~GgsA|I619NoE{s~hSSFf zPmfJ&2l}+4e$mY9u4xyu3wFumZ&ua6WSIFmH)|Gij!`g*?yAb??U}{wN;UeldU4*? zEx)bh646L=h0>gTi44kKvYmOm>}D-HUsE(nu1Rrf%w!Kbg=|5;oOO(aQr5M(3s!l? zSWQ)5Sg;nkZr;%s=4zU*qvz(c3x-p&i@HTE=4@xCRFfBUXFf}#Rm$ef^y5(+^SDxw?}#-0Tct7bPXzakIq8aF(@L3&!NtBQX{~dNlBO z$YYZh=8tgr$MuVL*`dVKytJc%Uvr(c&YHYqmltk^s23&KmBypyD5q60c>I`lZ65pN z>-E;g?aHMniyt`>xVFLLcD6DPMz|Y3iDqSL1G8otYb9o^8feuxsEZ=9jS`(flO*Tm zp1OSIbLi9j3BBTgB(J0*EWUep;Ogiy&+Gk(t?22RXIW7%RD!wAiDr)FY>VP_S(Nu_ zD|?*0a(_FEClZ0HY0rxx%9>}rG+TQYZSbI0zGnNI*;=1}#Zp1b%~+j>p2jRK^GnJp zt?}ev@}@W0-^SwI-GQs8Jo{4>p?HZjSS||RpV`BF39N;MeW_?TD^9aMts-w4QN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2!H?xfB*=900@8p2&{hs zIRCHzF@}dA00JNY0w4eaAOHd&00JNY0w93%KVkp`KmY_l00ck)1V8`;KmY_l00h=Q z0i6HW{}{tV5C8!X009sH0T2KI5C8!X009ud`5!R=0w4eaAOHd&00JNY0w4eaAOHgE zp8(GP>wk>lAqao~2!H?xfB*=900@8p2!H?x@biB_eM_Q0ct8LIKmY_l00ck)1V8`; gKmY_l00eGv0$T!6Nl^k!;ZIlz9sBMV`18eo0cPVX4*&oF 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), ]