source: OpenWorkouts-current/ow/views/bulk.py @ 715671f

current
Last change on this file since 715671f was 715671f, checked in by Borja Lopez <borja@…>, 5 years ago

(#77) Bulk workouts upload:

  • Added methods to extract files from compressed bulk files, then load workouts from those files.
  • Added a task to process/load workouts from "not loaded" bulk files
  • Added full tests coverage
  • Property mode set to 100644
File size: 2.1 KB
Line 
1from pyramid.httpexceptions import HTTPFound
2from pyramid.view import view_config
3from pyramid_simpleform import Form
4from pyramid_simpleform.renderers import FormRenderer
5from pyramid.i18n import TranslationStringFactory
6
7from ..models.user import User
8from ..models.bulk import BulkFile
9from ..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')
21def 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
29    if isinstance(request.POST.get('compressed_file', None), bytes):
30        request.POST['compressed_file'] = ''
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
55@view_config(
56    context=User,
57    permission='edit',
58    name='bulk-files',
59    renderer='ow:templates/bulk_files.pt')
60def bulk_files(context, request):
61    """
62    Render a page where users can see their bulk uploads (finished,
63    pending, status, etc)
64    """
65    return {
66        'bulk_files': request.root['_bulk_files'].get_by_uid(context.uid)
67    }
Note: See TracBrowser for help on using the repository browser.