Changeset 5cf5787 in OpenWorkouts-current for ow/views/user.py


Ignore:
Timestamp:
Feb 4, 2019, 12:37:35 PM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
3357e47
Parents:
63df989
Message:

Show weekly/monthly versions of the "last 12 months" workout stats chart

in the user profile page.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    r63df989 r5cf5787  
    168168    year = int(request.GET.get('year', now.year))
    169169    month = int(request.GET.get('month', now.month))
     170    week = request.GET.get('week', None)
    170171    return {
    171         'workouts': context.workouts(year, month),
     172        'workouts': context.workouts(year, month, week),
    172173        'current_month': '{year}-{month}'.format(
    173             year=str(year), month=str(month).zfill(2))
     174            year=str(year), month=str(month).zfill(2)),
     175        'current_week': week
    174176    }
    175177
     
    262264    context=User,
    263265    permission='view',
    264     name='yearly')
     266    name='monthly')
    265267def last_months_stats(context, request):
    266268    """
     
    294296                    charset='utf-8',
    295297                    body=json.dumps(json_stats))
     298
     299
     300@view_config(
     301    context=User,
     302    permission='view',
     303    name='weekly')
     304def last_weeks_stats(context, request):
     305    """
     306    Return a json-encoded stream with statistics for the last 12-months, but
     307    in a per-week basis
     308    """
     309    stats = context.weekly_year_stats
     310    # this sets which month is 2 times in the stats, once this year, once
     311    # the previous year. We will show it a bit different in the UI (showing
     312    # the year too to prevent confusion)
     313    repeated_month = datetime.now(timezone.utc).date().month
     314    json_stats = []
     315    for week in stats:
     316        hms = timedelta_to_hms(stats[week]['time'])
     317        name = month_name[week[1]][:3]
     318        if week[1] == repeated_month:
     319            name += ' ' + str(week[0])
     320        week_stats = {
     321            'id': '-'.join(
     322                [str(week[0]), str(week[1]).zfill(2), str(week[2])]),
     323            'week': str(week[3]),  # the number of week in the current month
     324            'name': name,
     325            'time': str(hms[0]).zfill(2),
     326            'distance': int(round(stats[week]['distance'])),
     327            'elevation': int(stats[week]['elevation']),
     328            'workouts': stats[week]['workouts'],
     329            'url': request.resource_url(
     330                context, 'profile',
     331                query={'year': str(week[0]),
     332                       'month': str(week[1]),
     333                       'week': str(week[2])},
     334                anchor='workouts')
     335        }
     336        json_stats.append(week_stats)
     337    return Response(content_type='application/json',
     338                    charset='utf-8',
     339                    body=json.dumps(json_stats))
Note: See TracChangeset for help on using the changeset viewer.