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_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
|
||||
|
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):
|
||||
# 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
|
||||
|
@ -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),
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user