Changeset 6993c72 in OpenWorkouts-current for ow/models/user.py


Ignore:
Timestamp:
Feb 4, 2019, 1:10:40 PM (5 years ago)
Author:
Segundo Fdez <segun.2@…>
Branches:
current, feature/docs, master
Children:
d52ba50
Parents:
bf01534 (diff), 3357e47 (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:
# ow/templates/profile.pt

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/models/user.py

    rbf01534 r6993c72  
    99
    1010from ow.catalog import get_catalog, reindex_object
    11 from ow.utilities import get_week_days
     11from ow.utilities import get_week_days, get_month_week_number
    1212
    1313
     
    7777        reindex_object(catalog, workout)
    7878
    79     def workouts(self, year=None, month=None):
     79    def workouts(self, year=None, month=None, week=None):
    8080        """
    8181        Return this user workouts, sorted by date, from newer to older
     
    8484        if year:
    8585            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]
     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]
    8892        workouts = sorted(workouts, key=attrgetter('start'))
    8993        workouts.reverse()
     
    288292
    289293        return stats
     294
     295    @property
     296    def weekly_year_stats(self):
     297        """
     298        Return per-week stats for the last 12 months
     299        """
     300        # set the boundaries for looking for workouts afterwards,
     301        # we need the current date as the "end date" and one year
     302        # ago from that date. Then we set the start at the first
     303        # 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 dict
     310        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 stats
     325        # to the proper place
     326        for workout in self.workouts():
     327            if start.date() <= workout.start.date() <= end.date():
     328                # less typing, avoid long lines
     329                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'] += 1
     338                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'] += 1
     349                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.