Changeset 2d91474 in OpenWorkouts-current for ow/models


Ignore:
Timestamp:
Jan 15, 2019, 10:13:57 PM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
0c18869
Parents:
9bee49d
Message:

(#23) - Show workouts in the dashboard with date filtering

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/models/user.py

    r9bee49d r2d91474  
    7575        reindex_object(catalog, workout)
    7676
    77     def workouts(self):
     77    def workouts(self, year=None, month=None):
    7878        """
    7979        Return this user workouts, sorted by date, from newer to older
    8080        """
    81         workouts = sorted(self.values(), key=attrgetter('start'))
     81        workouts = self.values()
     82        if year:
     83            workouts = [w for w in workouts if w.start.year == year]
     84        if month:
     85            workouts = [w for w in workouts if w.start.month == month]
     86        workouts = sorted(workouts, key=attrgetter('start'))
    8287        workouts.reverse()
    8388        return workouts
     
    8994    def num_workouts(self):
    9095        return len(self.workout_ids())
     96
     97    @property
     98    def activity_years(self):
     99        return sorted(list(set(w.start.year for w in self.workouts())),
     100                      reverse=True)
     101
     102    def activity_months(self, year):
     103        months = set(
     104            w.start.month for w in self.workouts() if w.start.year == year)
     105        return sorted(list(months))
     106
     107    @property
     108    def activity_dates_tree(self):
     109        """
     110        Return a dict containing information about the activity for this
     111        user.
     112
     113        Example:
     114
     115        {
     116            2019: {
     117                1: {'cycling': 12, 'running': 1}
     118            },
     119            2018: {
     120                1: {'cycling': 10, 'running': 3},
     121                2: {'cycling': 14, 'swimming': 5}
     122            }
     123        }
     124        """
     125        tree = {}
     126        for workout in self.workouts():
     127            year = workout.start.year
     128            month = workout.start.month
     129            sport = workout.sport
     130            if year not in tree:
     131                tree[year] = {}
     132            if month not in tree[year]:
     133                tree[year][month] = {}
     134            if sport not in tree[year][month]:
     135                tree[year][month][sport] = 0
     136            tree[year][month][sport] += 1
     137        return tree
Note: See TracChangeset for help on using the changeset viewer.