[715671f] | 1 | from pyramid.httpexceptions import HTTPFound |
---|
[fcf0ef3] | 2 | from pyramid.view import view_config |
---|
| 3 | from pyramid_simpleform import Form |
---|
| 4 | from pyramid_simpleform.renderers import FormRenderer |
---|
| 5 | from pyramid.i18n import TranslationStringFactory |
---|
| 6 | |
---|
| 7 | from ..models.user import User |
---|
| 8 | from ..models.bulk import BulkFile |
---|
| 9 | from ..schemas.bulk import ( |
---|
| 10 | BulkFileSchema, |
---|
| 11 | ) |
---|
| 12 | |
---|
| 13 | _ = TranslationStringFactory('OpenWorkouts') |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | @view_config( |
---|
| 17 | context=User, |
---|
| 18 | permission='edit', |
---|
| 19 | name='add-bulk-file', |
---|
| 20 | renderer='ow:templates/add_bulk_file.pt') |
---|
| 21 | def add_bulk_file(context, request): |
---|
| 22 | """ |
---|
| 23 | Add a compressed file that should contain tracking files, so we can |
---|
| 24 | do a "bulk" upload of tracking files and workouts |
---|
| 25 | """ |
---|
| 26 | # if not given a file there is an empty byte in POST, which breaks |
---|
| 27 | # our blob storage validator. |
---|
| 28 | # dirty fix until formencode fixes its api.is_empty method |
---|
[715671f] | 29 | if isinstance(request.POST.get('compressed_file', None), bytes): |
---|
| 30 | request.POST['compressed_file'] = '' |
---|
[fcf0ef3] | 31 | |
---|
| 32 | form = Form(request, schema=BulkFileSchema()) |
---|
| 33 | |
---|
| 34 | if 'submit' in request.POST and form.validate(): |
---|
| 35 | # get the extension of the compressed file. We use this later to |
---|
| 36 | # know how to decompress it. |
---|
| 37 | file_name = file_ext = request.POST['compressed_file'].filename |
---|
| 38 | file_ext = file_name.split('.')[-1] |
---|
| 39 | # Create a BulkFile instance based on the input from the form |
---|
| 40 | bulk_file = form.bind(BulkFile(uid=str(context.uid))) |
---|
| 41 | # Set the type of compressed file |
---|
| 42 | bulk_file.file_name = file_name |
---|
| 43 | bulk_file.file_type = file_ext |
---|
| 44 | |
---|
| 45 | # save the bulk file |
---|
| 46 | request.root['_bulk_files'].add_bulk_file(bulk_file) |
---|
| 47 | # Send the user to his/her bulk files page |
---|
| 48 | return HTTPFound(location=request.resource_url(context, 'bulk-files')) |
---|
| 49 | |
---|
| 50 | return { |
---|
| 51 | 'form': FormRenderer(form), |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
[8340661] | 55 | @view_config( |
---|
| 56 | context=BulkFile, |
---|
| 57 | permission='delete', |
---|
| 58 | name='delete', |
---|
| 59 | renderer='ow:templates/delete_bulk_file.pt') |
---|
| 60 | def 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 | |
---|
[fcf0ef3] | 73 | @view_config( |
---|
| 74 | context=User, |
---|
| 75 | permission='edit', |
---|
| 76 | name='bulk-files', |
---|
| 77 | renderer='ow:templates/bulk_files.pt') |
---|
| 78 | def bulk_files(context, request): |
---|
| 79 | """ |
---|
| 80 | Render a page where users can see their bulk uploads (finished, |
---|
| 81 | pending, status, etc) |
---|
| 82 | """ |
---|
| 83 | return { |
---|
| 84 | 'bulk_files': request.root['_bulk_files'].get_by_uid(context.uid) |
---|
| 85 | } |
---|