Changeset 778d53d in OpenWorkouts-current for ow/models/user.py


Ignore:
Timestamp:
Mar 5, 2019, 11:45:32 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current
Children:
aa6dcaf
Parents:
35953eb
Message:

(#7) Show per-sport stats in the profile page:

  • Show a dropdown list of sports for which the user has activities. By default we choose the sport with most activities.
  • Show a dropdown list of years for which the user has activities. By default we show stats for the current year. If the user picks up a different year, we show the totals (distance, time, elevation, number of workouts) for that year.
  • Show the totals of all time for the chosen sport
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/models/user.py

    r35953eb r778d53d  
    113113    def num_workouts(self):
    114114        return len(self.workout_ids())
     115
     116    @property
     117    def favorite_sport(self):
     118        """
     119        Return which sport is the one with most workouts for this user.
     120        In case of more than one sport with the maximum number of workouts,
     121        return the first in reversed alphabetical ordering
     122        """
     123        sports = {}
     124        for w in self.workouts():
     125            if w.sport not in sports.keys():
     126                sports[w.sport] = 0
     127            sports[w.sport] += 1
     128        _sports = sorted(sports.items(), reverse=True,
     129                         key=lambda x: (x[1], x[0]))
     130        if _sports:
     131            return _sports[0][0]
     132        return None
     133
     134    @property
     135    def activity_sports(self):
     136        return sorted(list(set(w.sport for w in self.workouts())))
    115137
    116138    @property
     
    368390
    369391        return stats
     392
     393    def sport_totals(self, sport=None, year=None):
     394        """
     395        Return totals for this user, filtered by sport.
     396
     397        If no sport is passed, the favorite sport is picked up
     398
     399        If the additional parameter year is passed, show stats only
     400        for that year
     401        """
     402        totals = {
     403            'workouts': 0,
     404            'time': timedelta(0),
     405            'distance': Decimal(0),
     406            'elevation': Decimal(0),
     407        }
     408        if self.activity_sports:
     409            sport = sport or self.favorite_sport
     410            for workout in self.workouts():
     411                if workout.sport == sport:
     412                    if year is None or workout.start.year == year:
     413                        totals['workouts'] += 1
     414                        totals['time'] += workout.duration or timedelta(0)
     415                        totals['distance'] += workout.distance or Decimal(0)
     416                        totals['elevation'] += workout.uphill or Decimal(0)
     417        return totals
Note: See TracChangeset for help on using the changeset viewer.