start of fitnotes importer
This commit is contained in:
		
							parent
							
								
									bd39151d95
								
							
						
					
					
						commit
						062fa049c4
					
				
					 2 changed files with 58 additions and 0 deletions
				
			
		
							
								
								
									
										32
									
								
								lifting/importers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								lifting/importers.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -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) | ||||||
							
								
								
									
										26
									
								
								lifting/tests.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								lifting/tests.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -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 | ||||||
		Loading…
	
		Reference in a new issue
	
	 James Turk
						James Turk