Changeset d517001 in OpenWorkouts-current


Ignore:
Timestamp:
Feb 15, 2019, 6:09:04 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
d5429c5
Parents:
d459ee2
Message:

(#58) Set a title automatically when adding manually a workout without
providing one.

The title is generated based on the only required data we have (starting
date and time) + sport (if provided).

Location:
ow
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • ow/tests/test_utilities.py

    rd459ee2 rd517001  
    2525    save_map_screenshot,
    2626    timedelta_to_hms,
    27     get_week_days
     27    get_week_days,
     28    part_of_day
    2829)
    2930
     
    235236            assert m[0] == m[1]
    236237
     238    def test_part_of_day(self):
     239        parts = [
     240            ((5, 11), 'Morning'),
     241            ((12, 17), 'Afternoon'),
     242            ((18, 22), 'Evening'),
     243            ((23, 4), 'Night'),
     244        ]
     245        for part in parts:
     246            hours_range = range(*part[0])
     247            expected = part[1]
     248            for hour in hours_range:
     249                dt = datetime(2019, 1, 15, hour, 0)
     250                assert part_of_day(dt) == expected
     251
    237252
    238253class TestGPXParseMinidom(object):
  • ow/tests/views/test_user.py

    rd459ee2 rd517001  
    447447        assert original_image.size == returned_image.size
    448448
    449 
    450449    def test_edit_profile_get(self, dummy_request, john):
    451450        """
  • ow/tests/views/test_workout.py

    rd459ee2 rd517001  
    126126        assert len(response['form'].form.errors) == 6
    127127
    128     def test_add_workout_manually_post_valid(self, valid_post_request):
     128    add_workout_params = [
     129        # no title, no sport, we generate a title based on when the
     130        # workout started
     131        ({'title': None, 'sport': None}, 'Morning workout'),
     132        # no title, sport given, we use the sport too in the automatically
     133        # generated title
     134        ({'title': None, 'sport': 'cycling'}, 'Morning cycling workout'),
     135        # title given, no sport, we use the provided title
     136        ({'title': 'Example workout', 'sport': None}, 'Example workout'),
     137        # title given, sport too, we use the provided title
     138        ({'title': 'Example workout', 'sport': 'cycling'}, 'Example workout'),
     139    ]
     140
     141    @pytest.mark.parametrize(('params', 'expected'), add_workout_params)
     142    def test_add_workout_manually_post_valid(self, params, expected,
     143                                             valid_post_request):
    129144        """
    130145        POST request to add a workout manually, providing the needed data
    131146        """
    132147        request = valid_post_request
     148        if params['title'] is not None:
     149            request.POST['title'] = params['title']
     150        if params['sport'] is not None:
     151            request.POST['sport'] = params['sport']
    133152        user = request.root['john']
    134153        assert len(user.workouts()) == 1
     
    137156        assert response.location.endswith('/2/')
    138157        assert len(user.workouts()) == 2
     158        assert user['2'].title == expected
    139159
    140160    def test_add_workout_get(self, dummy_request):
  • ow/utilities.py

    rd459ee2 rd517001  
    1313from ZODB.blob import Blob
    1414from splinter import Browser
     15
     16from pyramid.i18n import TranslationStringFactory
     17
     18_ = TranslationStringFactory('OpenWorkouts')
    1519
    1620
     
    263267            return weeks.index(week)
    264268    return None
     269
     270
     271def part_of_day(dt):
     272    """
     273    Given a datetime object (dt), return which part of the day was it
     274    (morning, afternoon, evening, night), translated in the proper
     275    """
     276    parts = {
     277        _('Morning'): (5, 11),
     278        _('Afternoon'): (12, 17),
     279        _('Evening'): (18, 22),
     280        _('Night'): (23, 4)
     281    }
     282    for key, value in parts.items():
     283        if value[0] <= dt.hour <= value[1]:
     284            return key
  • ow/views/workout.py

    rd459ee2 rd517001  
    1010from pyramid_simpleform import Form
    1111from pyramid_simpleform.renderers import FormRenderer
     12from pyramid.i18n import TranslationStringFactory
    1213
    1314from ..schemas.workout import (
     
    1819from ..models.workout import Workout
    1920from ..models.user import User
    20 from ..utilities import slugify, save_map_screenshot
     21from ..utilities import slugify, save_map_screenshot, part_of_day
    2122from ..catalog import get_catalog, reindex_object, remove_from_catalog
     23
     24
     25_ = TranslationStringFactory('OpenWorkouts')
    2226
    2327
     
    5155                                 tzinfo=timezone.utc)
    5256        workout.start = start
     57        if not workout.title:
     58            workout.title = part_of_day(start)
     59            if workout.sport:
     60                workout.title += ' ' + workout.sport
     61            workout.title += ' ' + _('workout')
     62
    5363        context.add_workout(workout)
    5464        return HTTPFound(location=request.resource_url(workout))
Note: See TracChangeset for help on using the changeset viewer.