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


Ignore:
Timestamp:
Jan 29, 2019, 12:41:01 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
1183d5a, 22eb5de
Parents:
26220ba (diff), bd8eeb4 (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:

Merged patches from darcs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    r26220ba red7e9d7  
    11import json
    22from calendar import month_name
    3 from datetime import datetime
     3from datetime import datetime, timezone
    44
    55from pyramid.httpexceptions import HTTPFound
     
    146146
    147147    return {
    148         'current_year': datetime.now().year,
    149         'current_day_name': datetime.now().strftime('%a'),
     148        'current_year': datetime.now(timezone.utc).year,
     149        'current_day_name': datetime.now(timezone.utc).strftime('%a'),
    150150        'month_name': month_name,
    151151        'viewing_year': viewing_year,
     
    165165    basic info, stats, etc
    166166    """
    167     return {}
     167    now = datetime.now(timezone.utc)
     168    return {
     169        'current_month': now.strftime('%Y-%m')
     170    }
    168171
    169172
     
    250253                    charset='utf-8',
    251254                    body=json.dumps(json_stats))
     255
     256
     257@view_config(
     258    context=User,
     259    permission='view',
     260    name='yearly')
     261def last_months_stats(context, request):
     262    """
     263    Return a json-encoded stream with statistics for the last 12 months
     264    """
     265    stats = context.yearly_stats
     266    # this sets which month is 2 times in the stats, once this year, once
     267    # the previous year. We will show it a bit different in the UI (showing
     268    # the year too to prevent confusion)
     269    repeated_month = datetime.now(timezone.utc).date().month
     270    json_stats = []
     271    for month in stats:
     272        hms = timedelta_to_hms(stats[month]['time'])
     273        name = month_name[month[1]][:3]
     274        if month[1] == repeated_month:
     275            name += ' ' + str(month[0])
     276        month_stats = {
     277            'id': str(month[0]) + '-' + str(month[1]).zfill(2),
     278            'name': name,
     279            'time': str(hms[0]).zfill(2),
     280            'distance': int(round(stats[month]['distance'])),
     281            'elevation': int(stats[month]['elevation']),
     282            'workouts': stats[month]['workouts']
     283        }
     284        json_stats.append(month_stats)
     285    return Response(content_type='application/json',
     286                    charset='utf-8',
     287                    body=json.dumps(json_stats))
Note: See TracChangeset for help on using the changeset viewer.