Changeset 8340661 in OpenWorkouts-current


Ignore:
Timestamp:
Oct 13, 2019, 8:12:17 PM (3 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current
Children:
9cc65af
Parents:
6211162
Message:

(#77) Bulk workouts upload:

  • added option to delete existing bulk files
Location:
ow
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • ow/templates/bulk_files.pt

    r6211162 r8340661  
    6060          </p>
    6161
     62          <p>
     63            <a href="" tal:attributes="href request.resource_url(bulk_file, 'delete')"
     64               i18n:translate="">delete</a>
     65          </p>
     66
    6267          <!--!
    6368               <tal:workouts tal:condition="bulk_file.loaded and bulk_file.workout_ids">
  • ow/tests/views/test_bulk.py

    r6211162 r8340661  
    216216        assert response == {'bulk_files': [bulk_file]}
    217217        assert response['bulk_files'][0].uid == str(user.uid)
     218
     219    def test_delete_bulk_file_get(self, dummy_request):
     220        request = dummy_request
     221        user = request.root.users[0]
     222        # let's add one bulk file
     223        uid = str(user.uid)
     224        bulk_file = BulkFile(uid=uid)
     225        bulk_file_path = os.path.join(
     226            os.path.dirname(os.path.dirname(__file__)),
     227            'fixtures/bulk-fit.zip')
     228        with open(bulk_file_path, 'rb') as _bulk_file:
     229            bulk_file.compressed_file = create_blob(
     230                _bulk_file.read(), file_extension='zip', binary=True)
     231        bulk_file.file_name = 'bulk-fit.zip'
     232        bulk_file.file_type = 'zip'
     233        request.root['_bulk_files'].add_bulk_file(bulk_file)
     234        assert len(request.root['_bulk_files']) == 1
     235        response = bulk_views.delete_bulk_file(bulk_file, request)
     236        assert response == {'user': user}
     237        assert len(request.root['_bulk_files']) == 1
     238
     239    def test_delete_bulk_file_post_invalid(self, dummy_request):
     240        request = dummy_request
     241        request.method = 'POST'
     242        # invalid, missing confirmation delete hidden value
     243        request.POST = MultiDict({'submit': True})
     244        user = request.root.users[0]
     245        # let's add one bulk file
     246        uid = str(user.uid)
     247        bulk_file = BulkFile(uid=uid)
     248        bulk_file_path = os.path.join(
     249            os.path.dirname(os.path.dirname(__file__)),
     250            'fixtures/bulk-fit.zip')
     251        with open(bulk_file_path, 'rb') as _bulk_file:
     252            bulk_file.compressed_file = create_blob(
     253                _bulk_file.read(), file_extension='zip', binary=True)
     254        bulk_file.file_name = 'bulk-fit.zip'
     255        bulk_file.file_type = 'zip'
     256        request.root['_bulk_files'].add_bulk_file(bulk_file)
     257        assert len(request.root['_bulk_files']) == 1
     258        response = bulk_views.delete_bulk_file(bulk_file, request)
     259        assert response == {'user': user}
     260        assert len(request.root['_bulk_files']) == 1
     261
     262    def test_delete_bulk_file_post_valid(self, dummy_request):
     263        request = dummy_request
     264        request.method = 'POST'
     265        # invalid, missing confirmation delete hidden value
     266        request.POST = MultiDict({'submit': True, 'delete': 'yes'})
     267        user = request.root.users[0]
     268        # let's add one bulk file
     269        uid = str(user.uid)
     270        bulk_file = BulkFile(uid=uid)
     271        bulk_file_path = os.path.join(
     272            os.path.dirname(os.path.dirname(__file__)),
     273            'fixtures/bulk-fit.zip')
     274        with open(bulk_file_path, 'rb') as _bulk_file:
     275            bulk_file.compressed_file = create_blob(
     276                _bulk_file.read(), file_extension='zip', binary=True)
     277        bulk_file.file_name = 'bulk-fit.zip'
     278        bulk_file.file_type = 'zip'
     279        request.root['_bulk_files'].add_bulk_file(bulk_file)
     280        assert len(request.root['_bulk_files']) == 1
     281        response = bulk_views.delete_bulk_file(bulk_file, request)
     282        # after a successful delete, we send the user back to his dashboard
     283        assert isinstance(response, HTTPFound)
     284        assert response.location == request.resource_url(user, 'bulk-files')
     285        assert len(request.root['_bulk_files']) == 0
  • ow/views/bulk.py

    r6211162 r8340661  
    5454
    5555@view_config(
     56    context=BulkFile,
     57    permission='delete',
     58    name='delete',
     59    renderer='ow:templates/delete_bulk_file.pt')
     60def delete_bulk_file(context, request):
     61    """
     62    Delete a bulk file
     63    """
     64    uid = context.uid
     65    user = request.root.get_user_by_uid(uid)
     66    if 'submit' in request.POST:
     67        if request.POST.get('delete', None) == 'yes':
     68            del request.root['_bulk_files'][str(context.bfid)]
     69            return HTTPFound(location=request.resource_url(user, 'bulk-files'))
     70    return {'user': user}
     71
     72
     73@view_config(
    5674    context=User,
    5775    permission='edit',
Note: See TracChangeset for help on using the changeset viewer.