Changeset 2d2eb0d in OpenWorkouts-current for ow/models/workout.py


Ignore:
Timestamp:
Jan 23, 2019, 2:07:26 PM (5 years ago)
Author:
Segundo Fdez <segun.2@…>
Branches:
current, feature/docs, master
Children:
4dcf28d
Parents:
5cbc4a0 (diff), c6219ed (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feature/ui

# Conflicts:

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/models/workout.py

    r5cbc4a0 r2d2eb0d  
    1 
     1import os
    22from datetime import datetime, timedelta, timezone
    33from decimal import Decimal
     
    1212    copy_blob,
    1313    create_blob,
     14    mps_to_kmph,
     15    save_map_screenshot
    1416)
    1517
     
    5052        self.duration = kw.get('duration', None)  # a timedelta object
    5153        self.distance = kw.get('distance', None)  # kilometers, Decimal
     54        self.speed = kw.get('speed', {})
    5255        self.hr_min = kw.get('hr_min', None)  # bpm, Decimal
    5356        self.hr_max = kw.get('hr_max', None)  # bpm, Decimal
     
    211214        path = None
    212215        if self.tracking_file:
    213             path = self.tracking_file._p_blob_uncommitted
     216            path = self.tracking_file._uncommitted()
    214217            if path is None:
    215                 path = self.tracking_file._p_blob_committed
     218                path = self.tracking_file.committed()
    216219        return path
    217220
     
    228231        path = None
    229232        if self.fit_file:
    230             path = self.fit_file._p_blob_uncommitted
     233            path = self.fit_file._uncommitted()
    231234            if path is None:
    232                 path = self.fit_file._p_blob_committed
     235                path = self.fit_file.committed()
    233236        return path
    234237
     
    348351        4. Grab some basic info from the fit file and store it in the Workout
    349352        """
    350         # backup the fit file
    351         self.fit_file = copy_blob(self.tracking_file)
     353
     354        # we can call load_from_fit afterwards for updates. In such case, check
     355        # if the tracking file is a fit file uploaded to override the previous
     356        # one. If not, just reuse the existing fit file
     357        if self.tracking_filetype == 'fit':
     358            # backup the fit file
     359            self.fit_file = copy_blob(self.tracking_file)
    352360
    353361        # create an instance of our Fit class
     
    378386            self.title = fit.name
    379387
     388        if fit.data['max_speed']:
     389            self.speed['max'] = mps_to_kmph(fit.data['max_speed'])
     390
     391        if fit.data['avg_speed']:
     392            self.speed['avg'] = mps_to_kmph(fit.data['avg_speed'])
     393
    380394        if fit.data['avg_hr']:
    381395            self.hr_avg = Decimal(fit.data['avg_hr'])
     
    406420    def has_fit(self):
    407421        return self.fit_file is not None
     422
     423    @property
     424    def map_screenshot(self):
     425        """
     426        Return the static path to the screenshot image of the map for
     427        this workout (works only for workouts with gps tracking)
     428        """
     429        if not self.has_gpx:
     430            return None
     431
     432        current_path = os.path.abspath(os.path.dirname(__file__))
     433        screenshot_path = os.path.join(
     434            current_path, '../static/maps',
     435            str(self.owner.uid), str(self.workout_id)) + '.png'
     436
     437        if not os.path.exists(screenshot_path):
     438            # screenshot does not exist, generate it
     439            save_map_screenshot(self)
     440
     441        # the value returned is relative to the static files served
     442        # by the app, so we can use request.static_url() with it
     443        static_path = os.path.join('static/maps', str(self.owner.uid),
     444                                   str(self.workout_id))
     445        return 'ow:' + static_path + '.png'
Note: See TracChangeset for help on using the changeset viewer.