- Timestamp:
- Jan 15, 2019, 10:13:57 PM (5 years ago)
- Branches:
- current, feature/docs, master
- Children:
- 0c18869
- Parents:
- 9bee49d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ow/models/user.py
r9bee49d r2d91474 75 75 reindex_object(catalog, workout) 76 76 77 def workouts(self ):77 def workouts(self, year=None, month=None): 78 78 """ 79 79 Return this user workouts, sorted by date, from newer to older 80 80 """ 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')) 82 87 workouts.reverse() 83 88 return workouts … … 89 94 def num_workouts(self): 90 95 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.