file upload integration test working

This commit is contained in:
Philip Sargent
2021-04-30 18:02:05 +01:00
parent fde30685a8
commit 8f1d6e2cc2
8 changed files with 128 additions and 102 deletions

View File

@@ -2,6 +2,7 @@ import re, os
from pathlib import Path
from django import forms
from django.conf import settings
from django.urls import reverse
from django.db.models import Q
@@ -16,7 +17,6 @@ from troggle.parsers.imports import import_logbooks, import_QMs, import_drawings
from troggle.core.models.troggle import Expedition, Person, PersonExpedition
from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip
from .login import login_required_if_public
from troggle.core.forms import UploadFileForm, SimpleUploadFileForm
'''Utility functions and code to serve the control panel and individual user's
progress and task list (deprecated as we do not have individual user login).
@@ -203,6 +203,11 @@ def ajax_test(request):
content_type="application/json")
class MyForm(forms.Form): # not a model-form
title = forms.CharField(max_length=20)
scanfiles = forms.FileField() # in MEDIA_FILES
@login_required_if_public
def scanupload(request, wallet=None):
'''Upload one scanned image file into a wallet on /expofiles
@@ -228,13 +233,15 @@ def scanupload(request, wallet=None):
wallet = wallet.replace(':','#')
dirpath = Path(settings.SURVEY_SCANS, year, wallet)
form = MyForm()
if request.method == 'POST':
form = SimpleUploadFileForm(request.POST,request.FILES)
form = MyForm(request.POST,request.FILES)
if form.is_valid():
#form.save() # comment out so nothing saved in MEDIA_ROOT/fileuploads
f = request.FILES["simplefile"]
f = request.FILES["scanfiles"]
w = request.POST["title"]
multiple = request.FILES.getlist('simplefile')
multiple = request.FILES.getlist('scanfiles')
fs = FileSystemStorage(os.path.join(settings.SURVEY_SCANS, year, w))
actual_saved = []
@@ -260,7 +267,70 @@ def scanupload(request, wallet=None):
files.append('(no image files in wallet)')
form = SimpleUploadFileForm()
return render(request, 'scanuploadform.html',
{'form': form, 'wallet': wallet, 'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved})
# @login_required_if_public
# def verysimplescanupload(request, wallet=None):
# '''Upload one scanned image file into a wallet on /expofiles
# '''
# print(f'VERY SIMPLE')
# filesaved = False
# actual_saved = []
# print(f'! - FORM scanupload - start {wallet}')
# if wallet is None:
# wallet = "2021#01" # improve this later
# if not re.match('(19|20)\d\d:\d\d', wallet):
# wallet = "2021:01" # improve this later
# year = wallet[:4]
# nexty = f'{int(year)+1}'
# prevy = f'{int(year)-1}'
# wnumber = wallet[5:]
# next = f'{int(wnumber)+1:02d}'
# prev = f'{int(wnumber)-1:02d}'
# if int(wnumber) == 0:
# prev = f'{int(wnumber):02d}'
# wallet = wallet.replace(':','#')
# dirpath = Path(settings.SURVEY_SCANS, year, wallet)
# form = MyForm()
# if request.method == 'POST':
# form = MyForm(request.POST,request.FILES)
# if form.is_valid():
# #form.save() # comment out so nothing saved in MEDIA_ROOT/fileuploads
# f = request.FILES["simplefile"]
# w = request.POST["title"]
# multiple = request.FILES.getlist('simplefile')
# fs = FileSystemStorage(os.path.join(settings.SURVEY_SCANS, year, w))
# actual_saved = []
# if multiple:
# for f in multiple:
# actual_saved.append( fs.save(f.name, content=f) )
# #print(f'! - FORM scanupload multiple {actual_saved}')
# filesaved = True
# files = []
# dirs = []
# #print(f'! - FORM scanupload - start {wallet} {dirpath}')
# try:
# for f in dirpath.iterdir():
# if f.is_dir():
# dirs.append(f.name)
# if f.is_file():
# if f.name != 'contents.json' and f.name != 'walletindex.html':
# files.append(f.name)
# except FileNotFoundError:
# files.append('(no wallet yet - would be created)')
# if len(files) ==0 :
# files.append('(no image files in wallet)')
# return render(request, 'scanuploadform.html',
# {'form': form, 'wallet': wallet, 'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved})