bia-fitness/lifting/views.py

31 lines
836 B
Python
Raw Normal View History

2015-04-02 21:58:26 +00:00
import tempfile
2015-03-31 21:25:19 +00:00
from django.shortcuts import render
2015-04-01 22:19:20 +00:00
from django import forms
2015-04-02 21:58:26 +00:00
from django.contrib.auth.decorators import login_required
import os
from . import importers
2015-03-31 21:25:19 +00:00
2015-04-01 22:19:20 +00:00
class FitnotesUploadForm(forms.Form):
file = forms.FileField()
2015-04-02 21:58:26 +00:00
@login_required
2015-04-01 22:19:20 +00:00
def fitnotes_upload(request):
if request.method == 'POST':
form = FitnotesUploadForm(request.POST, request.FILES)
if form.is_valid():
2015-04-02 21:58:26 +00:00
_, 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)
2015-04-01 22:19:20 +00:00
else:
form = FitnotesUploadForm()
return render(request, 'upload.html', {'form': form})