forked from bwaldvogel/openmoves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimports.py
48 lines (36 loc) · 1.42 KB
/
imports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
import gzip
from flask import flash
from sqlalchemy.sql import func
from gpx_import import gpx_import
from model import db, Sample
from old_xml_import import old_xml_import
from sml_import import sml_import
def move_import(xmlfile, filename, user, request_form):
if filename.endswith('.gz'):
xmlfile = gzip.GzipFile(fileobj=xmlfile, mode='rb', filename=filename)
filename = filename[:-len('.gz')]
extension = filename[-4:]
import_functions = {
'.xml': old_xml_import,
'.sml': sml_import,
'.gpx': gpx_import,
}
if extension not in import_functions:
flash("unknown fileformat: '%s'" % xmlfile.name, 'error')
return
import_function = import_functions[extension]
move = import_function(xmlfile, user, request_form)
if move:
move.temperature_avg, = db.session.query(func.avg(Sample.temperature)).filter(Sample.move == move, Sample.temperature > 0).one()
stroke_count = 0
for events, in db.session.query(Sample.events).filter(Sample.move == move, Sample.events != None):
if 'swimming' in events and events['swimming']['type'] == 'Stroke':
stroke_count += 1
if 'swimming' in move.activity:
assert stroke_count > 0
if stroke_count > 0:
move.stroke_count = stroke_count
db.session.commit()
return move