bia-fitness/lifting/importers.py

39 lines
1.3 KiB
Python
Raw Normal View History

2015-03-31 21:26:03 +00:00
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
2015-04-01 21:19:28 +00:00
exercises = {}
for e in Exercise.objects.all():
for n in e.names:
exercises[_clean_name(n)] = e.id
2015-03-31 21:26:03 +00:00
# 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)
2015-04-01 18:43:47 +00:00
# map to an Exercise id or str
exercise_id_mapping[fnid] = exercises[cleaned] if cleaned in exercises else cleaned
2015-03-31 21:26:03 +00:00
2015-04-01 21:20:58 +00:00
Set.objects.filter(source='fitnotes').delete()
2015-03-31 21:26:03 +00:00
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
2015-04-01 18:43:47 +00:00
if isinstance(exercise_id_mapping[fnid], str):
2015-04-01 21:19:28 +00:00
exercise_id_mapping[fnid] = Exercise.objects.create(
names=[exercise_id_mapping[fnid]]).id
2015-03-31 21:26:03 +00:00
exercise_id = exercise_id_mapping[fnid]
2015-04-01 21:20:58 +00:00
Set.objects.create(exercise_id=exercise_id, date=date, weight_kg=weight_kg, reps=reps,
source='fitnotes')