2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-22 07:11:52 +00:00

Scan Upload working nicely

This commit is contained in:
Philip Sargent 2021-04-30 03:44:53 +01:00
parent 03a5f5989e
commit bdf535fcbf
6 changed files with 86 additions and 32 deletions

View File

@ -15,7 +15,8 @@ from troggle.core.models.caves import Cave, LogbookEntry, QM, Entrance, CaveAndE
Some are not used and need renovating or destroying.
'''
todo = '''Fix UploadFileForm - long list of actions
todo = '''Remove UploadFileForm - replace by Simple variant
Re engineer Simple upload to not use a Django form object
'''

View File

@ -89,7 +89,7 @@ def expofilessingle(request, filepath):
def expofilesdir(request, dirpath, filepath):
'''does a directory display. If there is an index.html file we should display that.
- dirpath is a full Path() resolved including lcoal machine /expofiles/
- dirpath is a full Path() resolved including local machine /expofiles/
- filepath is a Path() and it does not have /expofiles/ in it
'''
#print(f' - expofilesdir {dirpath} settings.EXPOFILESREMOTE: {settings.EXPOFILESREMOTE}')

View File

@ -204,25 +204,63 @@ def ajax_test(request):
@login_required_if_public
def scanupload(request, year='2050'):
print(f'! - FORM scanupload - start')
def scanupload(request, wallet=None):
'''Upload one scanned image file into a wallet on /expofiles
'''
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)
if request.method == 'POST':
form = SimpleUploadFileForm(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"]
print(f'! - FORM scanupload uploaded {f.name}')
multiple = request.FILES.getlist('simplefile')
fs = FileSystemStorage(os.path.join(settings.SURVEY_SCANS, year, w))
actual_saved = fs.save(f.name, content=f) # name may chnage to avoid clash
# INSERT check if name is changed, to allow user to abort and rename - or lets do a chaecjk anyway.
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
print(f'! - FORM scanupload {actual_saved}')
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)')
form = SimpleUploadFileForm()
return render(request, 'scanuploadform.html', {'form': form,'filesaved': True, 'actual_saved': actual_saved})
else:
form = SimpleUploadFileForm()
return render(request, 'scanuploadform.html', {'form':form,})
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})

View File

@ -32,6 +32,7 @@
<a href="/survexfile/">Survex</a> |
<a href="{% url "survexcaveslist" %}">All Survex</a> |
<a href="{% url "allwallets" %}">Scans</a> |
<a href="{% url "scanupload" '2021:01' %}">Upload Scans</a> |
<a href="{% url "dwgdata" %}">Drawings</a> |
<a href="/1623/290/290.html">290 (FGH)</a> |
<a href="/1623/291/291">291 (GSH)</a> |

View File

@ -4,15 +4,27 @@
{% block content %}
<h2>Survey Scan upload into Wallet</h2>
<h2>Scanned notes or survey upload into wallet </h2>
<p style="font-family: monospace; font-weight: bold; font-size: 130%; text-align: center">
<a style="font-weight: normal;" href="/scanupload/{{prevy}}:01">{{prevy}}</a>
&nbsp;...&nbsp;
<a href="/scanupload/{{year}}:{{prev}}">{{year}}:{{prev}}</a>
&larr; {{wallet}} &rarr;
<a href="/scanupload/{{year}}:{{next}}">{{year}}:{{next}}</a>
&nbsp;...&nbsp;
<a style="font-weight: normal;" href="/scanupload/{{nexty}}:01">{{nexty}}</a>
</p>
<div style = "max-width:40%;">
<div style="column-count: 2;">
<div style = "max-width:40%px; " >
{% if filesaved %}
<p style="margin-left:20%;">
<b>The file was saved as <em>'{{actual_saved}}'</em> <br><br>Upload another?</b>
<b>File(s) saved as
{% for f in actual_saved %}
<em>'{{f}}'</em>
{% endfor %}
<br><br>Upload more?</b>
</p>
<br>
{% endif %}
@ -21,22 +33,24 @@
<form method ='post' enctype ="multipart/form-data">
{% csrf_token %}
<br>
<input type = "file" style = "margin-left:20%;"
placeholder = "Simplefile" name = "simplefile" id="files"><label for="files">Scan file(s)</label>
<input type = "file" multiple="multiple" style = "margin-left:20%;"
placeholder = "Simplefile" name = "simplefile" id="files">
<br> <br>
<input type = "text" style = "margin-left:20%;"
placeholder = "Wallet id e.g. 2021#23" name = "title" value='2050#99'id="wallet"><label for="wallet"> Wallet id</label>
<input type = "text" size = "8" style = "margin-left:20%;"
placeholder = "Wallet id e.g. 2021#23" name = "title" value='{{wallet}}'id="wallet"><label for="wallet"> Wallet id</label>
</div>
<div style = "max-width:30%;">
<center>
<button style = "color: #fff; border:1px; background-color:#999; margin-top:8%;
height:35px; width:80%; margin-left:19%;" type = "submit" value = "Upload" >
<strong>Upload</strong>
</button>
</center>
</div>
</form>
</div>
</div>
<p style="margin-left:20%;">
{% for f in files %}
<a href="/expofiles/surveyscans/{{year}}/{{wallet|urlencode}}/{{f}}">{{ f}}</a><br />
{% endfor %}
</p>
{% endblock %}

12
urls.py
View File

@ -78,7 +78,7 @@ trogglepatterns = [
re_path(r'^admin/doc/', include('django.contrib.admindocs.urls')), # needs docutils Python module (http://docutils.sf.net/).
re_path(r'^admin/', admin.site.urls), # includes admin login & logout urls
path('scanupload', scanupload, name='scanupload'),
path('scanupload/<wallet>', scanupload, name='scanupload'),
# setting LOGIN_URL = '/accounts/login/' is default
# url ENDS WITH this string
@ -129,9 +129,9 @@ trogglepatterns = [
path('pathsreport', statistics.pathsreport, name="pathsreport"),
path('dataissues', statistics.dataissues, name="dataissues"),
path(r'troggle', frontpage, name="frontpage"), # control panel. Shows recent actions.
path(r'todo/<path:module>', todos, name="todos"),
path(r'controlpanel', controlpanel, name="controlpanel"),
path('troggle', frontpage, name="frontpage"), # control panel. Shows recent actions.
path('todo/<path:module>', todos, name="todos"),
path('controlpanel', controlpanel, name="controlpanel"),
# The survexfile pages
path('survexfile', survex.survexcavesingle, {'survex_cave': ''}, name="survexcavessingle"),
@ -148,8 +148,8 @@ trogglepatterns = [
# The survey scans in the wallets
path('survey_scans/', allwallets, name="allwallets"),
path('survey_scans/<path>/', singlewallet, name="singlewallet"),
path('survey_scans/<path>/<file>', scansingle, name="scansingle"),
path('survey_scans/<path:path>/', singlewallet, name="singlewallet"),
path('survey_scans/<path:path>/<file>', scansingle, name="scansingle"),
# The tunnel and therion drawings files pages
re_path(r'^dwgdata/$', dwgdata, name="dwgdata"),