diff --git a/lifting/importers.py b/lifting/importers.py new file mode 100644 index 0000000..05a0e9f --- /dev/null +++ b/lifting/importers.py @@ -0,0 +1,32 @@ +import sqlite3 +from lifting.models import Exercise, Set + + +def _clean_name(name): + return name.lower() + + +def import_fitnotes_db(filename): + # exercise names to db ids + exercises = {_clean_name(e.name): e.id for e in Exercise.objects.all()} + + # build mapping FitNotes exercise id => our exercise id + exercise_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 in exercises: + exercise_id_mapping[fnid] = exercises[cleaned] + + for fnid, date, weight_kg, reps in cur.execute( + 'SELECT exercise_id, date, metric_weight, reps FROM training_log'): + + # create Exercise if it wasn't found and there's a workout using it + if fnid not in exercise_id_mapping: + exercise_id_mapping[fnid] = Exercise.objects.create(name=cleaned).id + + exercise_id = exercise_id_mapping[fnid] + + Set.objects.create(exercise_id=exercise_id, date=date, weight_kg=weight_kg, reps=reps) diff --git a/lifting/tests.py b/lifting/tests.py new file mode 100644 index 0000000..d9df877 --- /dev/null +++ b/lifting/tests.py @@ -0,0 +1,26 @@ +from django.test import TestCase +from lifting.models import Exercise, Set +from lifting.importers import import_fitnotes_db + + +class TestFitnotesImport(TestCase): + # fitnotes.db has: + # April 1 + # bench press 10 @ 45 + # bench press 5 @ 95 + # bench press 3 @ 135 + # bench press 5 @ 155 + # April 3 + # squat 10 @ 45 + # squat 5 @ 95 + # squat 3 @ 135 + # squat 2 @ 185 + # squat 5 @ 225 + import_fitnotes_db('example.fitnotes') + + #assert Exercise.objects.count() == 2 + + bp = Exercise.objects.get(name="bench press") + squat = Exercise.objects.get(name="barbell squat") + + assert Set.objects.count() == 9