Changeset 02b96c5 in OpenWorkouts-current for ow/views/user.py


Ignore:
Timestamp:
Mar 12, 2019, 12:47:38 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current
Children:
8ba32d9
Parents:
d9453fc
Message:

(#7) Added calendar heatmap chart to the profile page.

The calendar shows the current month, each day without a workout represented
by a light grey square, each day with workout data in a red/pink color, picked
up from a gradient generated based on the app main colors, and calculated based
on the amount of workout time for the day.

A tooltip is shown on hover with more info (only the total workouts time for
now)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    rd9453fc r02b96c5  
    438438        }
    439439        json_stats.append(day_stats)
     440    return Response(content_type='application/json',
     441                    charset='utf-8',
     442                    body=json.dumps(json_stats))
     443
     444
     445@view_config(
     446    context=User,
     447    permission='view',
     448    name='month')
     449def month_stats(context, request):
     450    """
     451    For the given month, return a json-encoded stream containing
     452    per-day workouts information.
     453    """
     454    localizer = get_localizer(request)
     455    now = datetime.now(timezone.utc)
     456    year = int(request.GET.get('year', now.year))
     457    month = int(request.GET.get('month', now.month))
     458    workouts = context.workouts(year, month)
     459    stats = {}
     460
     461    for workout in workouts:
     462        start = workout.start.strftime('%Y-%m-%d')
     463        if start not in stats.keys():
     464            stats[start] = {
     465                'time': 0,  # seconds
     466                'distance': 0,  # kilometers
     467                'elevation': 0,  # meters
     468            }
     469        duration = getattr(workout, 'duration', None) or timedelta(0)
     470        stats[start]['time'] += duration.seconds
     471        distance = getattr(workout, 'distance', None) or 0
     472        stats[start]['distance'] += int(round(distance))
     473        elevation = getattr(workout, 'uphill', None) or 0
     474        stats[start]['elevation'] += int(elevation)
     475
     476    json_stats = []
     477    for day in stats.keys():
     478        hms = timedelta_to_hms(timedelta(seconds=stats[day]['time']))
     479        hours_label = _('hour')
     480        if hms[0] > 1:
     481            hours_label = _('hours')
     482        time_formatted = ' '.join([
     483            str(hms[0]).zfill(2), localizer.translate(hours_label),
     484            str(hms[1]).zfill(2), localizer.translate(_('min.'))
     485        ])
     486        json_stats.append({
     487            'day': day,
     488            'time': stats[day]['time'],
     489            'time_formatted': time_formatted,
     490            'distance': stats[day]['distance'],
     491            'elevation': stats[day]['elevation']
     492        })
     493
    440494    return Response(content_type='application/json',
    441495                    charset='utf-8',
Note: See TracChangeset for help on using the changeset viewer.