#
#   Copyright 2013 by Arnold Krille <arnold@arnoldarts.de>
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.


from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth import get_user_model
from django.utils.timezone import get_default_timezone
from fitparse.base import FitFile

from ...models import Workout, Lap, Record
from ...helpers import FullDatetimeProcessor

class Command(BaseCommand):
    args = '<fitfile> ...'
    help = u'Import FIT-files from garmin devices into workout-data'

    def handle(self, *args, **options):
        print "Should import files from %s" % list(args)
        for f in args:
            print "Processing '%s'..." % f
            fitfile = FitFile(
                f,
                data_processor=FullDatetimeProcessor(),
                check_crc=False
            )
            #print fitfile
            workout = Workout()
            print "  Processing session"
            for msg in fitfile.get_messages(name='session', as_dict=True):
                print msg['fields']
                for f in msg['fields']:
                    if f['name'] == 'timestamp':
                        workout.title = f['value'].astimezone(get_default_timezone()).isoformat()
                    if f['name'] == 'start_time':
                        workout.publish_date = f['value']
                    if hasattr(workout, f['name']):
                        print "   Setting '%s' to '%s'" % (f['name'], f['value'])
                        setattr(workout, f['name'], f['value'])

            workout.user = get_user_model().objects.all()[0]
            workout.save()

            print "  Processing laps"
            for msg in fitfile.get_messages(name='lap', as_dict=True):
                print msg['name']
                lap = Lap(workout=workout)
                for f in msg['fields']:
                    if hasattr(lap, f['name']):
                        getattr(lap, f['name'], f['value'])
                lap.save()

            print "  Processing data points"
            for msg in fitfile.get_messages(name='record', as_dict=True):
                record = Record(workout=workout)
                for f in msg['fields']:
                    if hasattr(record, f['name']):
                        setattr(record, f['name'], f['value'])
                record.save()

            print " Finished."

@@ -617,16 +617,31 @@
 cense.%0A%0A
+import logging%0A
 %0Afrom dj
@@ -1062,14 +1062,16 @@
 def 
-handle
+__init__
 (sel
@@ -1086,14 +1086,13 @@
 , **
-option
+kwarg
 s):%0A
@@ -1103,56 +1103,173 @@
     
-print %22Should import files from %25s%22 %25 list(args)
+super(Command, self).__init__(*args, **kwargs)%0A        self.logger = logging.getLogger(__name__+'.'+self.__class__.__name__)%0A%0A    def handle(self, *args, **options):
 %0A   
@@ -1300,22 +1300,34 @@
         
-print 
+self.logger.info(u
 %22Process
@@ -1342,16 +1342,17 @@
 ...%22 %25 f
+)
 %0A       
@@ -1500,35 +1500,8 @@
   )%0A
-            #print fitfile%0A
     
@@ -1536,33 +1536,43 @@
             
-print %22  
+self.logger.info(u%22
 Processing s
@@ -1578,16 +1578,17 @@
 session%22
+)
 %0A       
@@ -1667,30 +1667,70 @@
             
-print 
+self.logger.debug(u%22Session fields: %25s%22 %25 str(
 msg%5B'fields'
@@ -1730,16 +1730,18 @@
 fields'%5D
+))
 %0A       
@@ -2084,90 +2084,8 @@
 %5D):%0A
-                        print %22   Setting '%25s' to '%25s'%22 %25 (f%5B'name'%5D, f%5B'value'%5D)%0A
     
@@ -2242,33 +2242,43 @@
             
-print %22  
+self.logger.info(u%22
 Processing l
@@ -2281,16 +2281,17 @@
 ng laps%22
+)
 %0A       
@@ -2370,21 +2370,47 @@
         
-print
+self.logger.info(%22 Lap: '%25s'%22 %25
  msg%5B'na
@@ -2413,16 +2413,17 @@
 %5B'name'%5D
+)
 %0A       
@@ -2570,17 +2570,17 @@
         
-g
+s
 etattr(l
@@ -2650,17 +2650,27 @@
     
-print %22  
+self.logger.info(u%22
 Proc
@@ -2688,16 +2688,17 @@
  points%22
+)
 %0A       
@@ -3014,16 +3014,26 @@
     
-print %22 
+self.logger.info(%22
 Fini
@@ -3038,9 +3038,10 @@
 nished.%22
+)
 %0A

fix the import