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