Changeset 42baca4 in OpenWorkouts-current for ow/views/workout.py


Ignore:
Timestamp:
Apr 22, 2019, 6:14:53 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current
Children:
0dedfbe
Parents:
e52a502
Message:

(#39) Do not allow duplicated workouts by default when uploading track files.
We still allow users to add duplicates if they want, by checking a checkbox
we show in the upload workout form when we find a possible duplicate.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/workout.py

    re52a502 r42baca4  
    8989    form = Form(request, schema=UploadedWorkoutSchema())
    9090
     91    duplicate = None
     92    allow_duplicates = request.POST.get('allow_duplicates') == 'on'
     93
    9194    if 'submit' in request.POST and form.validate():
    9295        # Grab some information from the tracking file
     
    98101        # Add basic info gathered from the file
    99102        workout.load_from_file()
    100         # Add the workout
    101         context.add_workout(workout)
    102         return HTTPFound(location=request.resource_url(workout))
     103        # Ensure this workout is not a duplicate of an existing workout.
     104        #
     105        # hashed is not "complete" for a workout that has not been added
     106        # yet, as it does not have the owner set, so we have to "build it"
     107        hashed = str(context.uid) + workout.hashed
     108        duplicate = request.root.get_workout_by_hash(hashed)
     109
     110        if duplicate and not allow_duplicates:
     111            form.errors['tracking_file'] = _(
     112                'This workout looks like a duplicate of another workout, '
     113                'please enable workout duplicates below to save it'
     114            )
     115        else:
     116            # Add the workout
     117            context.add_workout(workout)
     118            return HTTPFound(location=request.resource_url(workout))
    103119
    104120    return {
    105         'form': FormRenderer(form)
     121        'form': FormRenderer(form),
     122        'duplicate': duplicate
    106123    }
    107124
Note: See TracChangeset for help on using the changeset viewer.