fix upload

This commit is contained in:
James Turk 2015-04-02 17:58:26 -04:00
parent d907467561
commit d930beaa4e
2 changed files with 17 additions and 3 deletions

View File

@ -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})

View File

@ -1,5 +1,5 @@
<form method="post">
<form action="." method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form}}
<input type="submit" />
<input type="submit" value="Upload" />
</form>