Changeset 421f05f in OpenWorkouts-current


Ignore:
Timestamp:
Jan 25, 2019, 12:41:26 AM (5 years ago)
Author:
borja <borja@…>
Branches:
current, feature/docs, master
Children:
5bdfbfb
Parents:
2f8a48f
Message:

(#7) Added week_stats view, that returns a json-encoded stream of data

containing information about the current week status for the given user.

Location:
ow
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ow/tests/views/test_user.py

    r2f8a48f r421f05f  
    11import os
     2import json
    23from datetime import datetime, timedelta, timezone
    34from shutil import copyfileobj
     
    583584        assert 'jack.black@example.net' not in request.root.emails
    584585        assert 'JackBlack' not in request.root.all_nicknames
     586
     587    def test_week_stats_no_stats(self, dummy_request, john):
     588        response = user_views.week_stats(john, dummy_request)
     589        assert isinstance(response, Response)
     590        assert response.content_type == 'application/json'
     591        # the body is a valid json-encoded stream
     592        obj = json.loads(response.body)
     593        assert obj == [
     594            {'distance': 0, 'elevation': 0, 'name': 'Mon',
     595             'time': '00', 'workouts': 0},
     596            {'distance': 0, 'elevation': 0, 'name': 'Tue',
     597             'time': '00', 'workouts': 0},
     598            {'distance': 0, 'elevation': 0, 'name': 'Wed',
     599             'time': '00', 'workouts': 0},
     600            {'distance': 0, 'elevation': 0, 'name': 'Thu',
     601             'time': '00', 'workouts': 0},
     602            {'distance': 0, 'elevation': 0, 'name': 'Fri',
     603             'time': '00', 'workouts': 0},
     604            {'distance': 0, 'elevation': 0, 'name': 'Sat',
     605             'time': '00', 'workouts': 0},
     606            {'distance': 0, 'elevation': 0, 'name': 'Sun',
     607             'time': '00', 'workouts': 0}
     608        ]
     609
     610    def test_week_stats(self, dummy_request, john):
     611        workout = Workout(
     612            start=datetime.now(timezone.utc),
     613            duration=timedelta(minutes=60),
     614            distance=30,
     615            elevation=540
     616        )
     617        john.add_workout(workout)
     618        response = user_views.week_stats(john, dummy_request)
     619        assert isinstance(response, Response)
     620        assert response.content_type == 'application/json'
     621        # the body is a valid json-encoded stream
     622        obj = json.loads(response.body)
     623        assert len(obj) == 7
     624        for day in obj:
     625            if datetime.now(timezone.utc).strftime('%a') == day['name']:
     626                day['distance'] == 30
     627                day['elevation'] == 540
     628                day['time'] == '01'
     629                day['workouts'] == 1
     630            else:
     631                day['distance'] == 0
     632                day['elevation'] == 0
     633                day['time'] == '00'
     634                day['workouts'] == 0
  • ow/views/user.py

    r2f8a48f r421f05f  
     1import json
    12from calendar import month_name
     3from datetime import datetime
    24
    35from pyramid.httpexceptions import HTTPFound
     
    1719from ..models.root import OpenWorkouts
    1820from ..views.renderers import OWFormRenderer
     21from ..utilities import timedelta_to_hms
    1922
    2023_ = TranslationStringFactory('OpenWorkouts')
     
    223226        return HTTPFound(location=request.resource_url(context, 'profile'))
    224227    return {'form': OWFormRenderer(form)}
     228
     229
     230@view_config(
     231    context=User,
     232    permission='view',
     233    name='week')
     234def week_stats(context, request):
     235    stats = context.week_stats
     236    json_stats = []
     237    for day in stats:
     238        hms = timedelta_to_hms(stats[day]['time'])
     239        day_stats = {
     240            'name': day.strftime('%a'),
     241            'time': str(hms[0]).zfill(2),
     242            'distance': int(round(stats[day]['distance'])),
     243            'elevation': int(stats[day]['elevation']),
     244            'workouts': stats[day]['workouts']
     245        }
     246        json_stats.append(day_stats)
     247    return Response(content_type='application/json',
     248                    charset='utf-8',
     249                    body=json.dumps(json_stats))
Note: See TracChangeset for help on using the changeset viewer.