From d930beaa4eacb46d7467a09bd759392d25d63126 Mon Sep 17 00:00:00 2001 From: James Turk Date: Thu, 2 Apr 2015 17:58:26 -0400 Subject: [PATCH] fix upload --- lifting/views.py | 16 +++++++++++++++- templates/upload.html | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lifting/views.py b/lifting/views.py index 562a813..5ad630a 100644 --- a/lifting/views.py +++ b/lifting/views.py @@ -1,16 +1,30 @@ +import tempfile + from django.shortcuts import render from django import forms +from django.contrib.auth.decorators import login_required +import os + +from . import importers class FitnotesUploadForm(forms.Form): file = forms.FileField() +@login_required def fitnotes_upload(request): if request.method == 'POST': form = FitnotesUploadForm(request.POST, request.FILES) if form.is_valid(): - print(request.FILES['file']) + _, fname = tempfile.mkstemp() + with open(fname, 'wb') as tmp: + for chunk in request.FILES['file'].chunks(): + tmp.write(chunk) + try: + importers.import_fitnotes_db(fname, request.user) + finally: + os.remove(fname) else: form = FitnotesUploadForm() return render(request, 'upload.html', {'form': form}) diff --git a/templates/upload.html b/templates/upload.html index e0d453c..054d990 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -1,5 +1,5 @@ -
+ {% csrf_token %} {{form}} - +