Changes in ow/models/user.py [63df989:bed4f06] in OpenWorkouts-current
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/models/user.py
r63df989 rbed4f06 9 9 10 10 from ow.catalog import get_catalog, reindex_object 11 from ow.utilities import get_week_days , get_month_week_number11 from ow.utilities import get_week_days 12 12 13 13 … … 77 77 reindex_object(catalog, workout) 78 78 79 def workouts(self, year=None, month=None , week=None):79 def workouts(self, year=None, month=None): 80 80 """ 81 81 Return this user workouts, sorted by date, from newer to older … … 84 84 if year: 85 85 workouts = [w for w in workouts if w.start.year == year] 86 if month: 87 workouts = [w for w in workouts if w.start.month == month] 88 if week: 89 week = int(week) 90 workouts = [ 91 w for w in workouts if w.start.isocalendar()[1] == week] 86 if month: 87 workouts = [w for w in workouts if w.start.month == month] 92 88 workouts = sorted(workouts, key=attrgetter('start')) 93 89 workouts.reverse() … … 292 288 293 289 return stats 294 295 @property296 def weekly_year_stats(self):297 """298 Return per-week stats for the last 12 months299 """300 # set the boundaries for looking for workouts afterwards,301 # we need the current date as the "end date" and one year302 # ago from that date. Then we set the start at the first303 # day of that month.304 end = datetime.now(timezone.utc)305 start = (end - timedelta(days=365)).replace(day=1)306 307 stats = {}308 309 # first initialize the stats dict310 for days in range((end - start).days):311 day = (start + timedelta(days=days)).date()312 week = day.isocalendar()[1]313 month_week = get_month_week_number(day)314 key = (day.year, day.month, week, month_week)315 if key not in stats.keys():316 stats[key] = {317 'workouts': 0,318 'time': timedelta(0),319 'distance': Decimal(0),320 'elevation': Decimal(0),321 'sports': {}322 }323 324 # now loop over the workouts, filtering and then adding stats325 # to the proper place326 for workout in self.workouts():327 if start.date() <= workout.start.date() <= end.date():328 # less typing, avoid long lines329 start_date = workout.start.date()330 week = start_date.isocalendar()[1]331 month_week = get_month_week_number(start_date)332 week = stats[(start_date.year,333 start_date.month,334 week,335 month_week)]336 337 week['workouts'] += 1338 week['time'] += workout.duration or timedelta(seconds=0)339 week['distance'] += workout.distance or Decimal(0)340 week['elevation'] += workout.uphill or Decimal(0)341 if workout.sport not in week['sports']:342 week['sports'][workout.sport] = {343 'workouts': 0,344 'time': timedelta(seconds=0),345 'distance': Decimal(0),346 'elevation': Decimal(0),347 }348 week['sports'][workout.sport]['workouts'] += 1349 week['sports'][workout.sport]['time'] += (350 workout.duration or timedelta(0))351 week['sports'][workout.sport]['distance'] += (352 workout.distance or Decimal(0))353 week['sports'][workout.sport]['elevation'] += (354 workout.uphill or Decimal(0))355 356 return stats
Note: See TracChangeset
for help on using the changeset viewer.