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

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

(#77) Bulk workouts upload:

  • Added a new Folder-like object under the root object to store compressed files
  • Added a new Blob-like object to store those compressed files into the database
  • Added the needed view/template code to handle adding/uploading compressed files that may contain workout tracking files + a page to see them per-user
  • Property mode set to 100644
File size: 2.2 KB
Line 
1from pyramid.httpexceptions import HTTPFound, HTTPNotFound
2from pyramid.view import view_config
3from pyramid.response import Response
4from pyramid_simpleform import Form
5from pyramid_simpleform.renderers import FormRenderer
6from pyramid.i18n import TranslationStringFactory
7
8from ..models.user import User
9from ..models.bulk import BulkFile
10from ..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')
22def 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')
62def 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    }
Note: See TracBrowser for help on using the repository browser.