From bdf535fcbfe66c424e52e86fa554ba567b0ec63b Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Fri, 30 Apr 2021 03:44:53 +0100 Subject: [PATCH] Scan Upload working nicely --- core/forms.py | 3 +- core/views/expo.py | 2 +- core/views/other.py | 60 ++++++++++++++++++++++++++++------- templates/base.html | 1 + templates/scanuploadform.html | 40 +++++++++++++++-------- urls.py | 12 +++---- 6 files changed, 86 insertions(+), 32 deletions(-) diff --git a/core/forms.py b/core/forms.py index 42ed0e0..a7b44db 100644 --- a/core/forms.py +++ b/core/forms.py @@ -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 ''' diff --git a/core/views/expo.py b/core/views/expo.py index a312143..b454df9 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -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}') diff --git a/core/views/other.py b/core/views/other.py index 89fac23..186616a 100644 --- a/core/views/other.py +++ b/core/views/other.py @@ -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. - - print(f'! - FORM scanupload {actual_saved}') - - form = SimpleUploadFileForm() - return render(request, 'scanuploadform.html', {'form': form,'filesaved': True, 'actual_saved': actual_saved}) + 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)') + 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}) diff --git a/templates/base.html b/templates/base.html index c10f2ab..ccc6982 100644 --- a/templates/base.html +++ b/templates/base.html @@ -32,6 +32,7 @@ Survex | All Survex | Scans | + Upload Scans | Drawings | 290 (FGH) | 291 (GSH) | diff --git a/templates/scanuploadform.html b/templates/scanuploadform.html index c08a165..ed16301 100644 --- a/templates/scanuploadform.html +++ b/templates/scanuploadform.html @@ -4,15 +4,27 @@ {% block content %} -

Survey Scan upload into Wallet

+

Scanned notes or survey upload into wallet

+

+{{prevy}} + ...  +{{year}}:{{prev}} +← {{wallet}} → +{{year}}:{{next}} + ...  +{{nexty}} +

- -
-
+
+ {% if filesaved %}

- The file was saved as '{{actual_saved}}'

Upload another?
+ File(s) saved as + {% for f in actual_saved %} + '{{f}}' + {% endfor %} +

Upload more?


{% endif %} @@ -21,22 +33,24 @@
{% csrf_token %}
- +

- + -
-
-
-
+ +

+{% for f in files %} +{{ f}}
+{% endfor %} +

{% endblock %} diff --git a/urls.py b/urls.py index 14aa34a..4b4ccf8 100644 --- a/urls.py +++ b/urls.py @@ -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/', 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/', todos, name="todos"), - path(r'controlpanel', controlpanel, name="controlpanel"), + path('troggle', frontpage, name="frontpage"), # control panel. Shows recent actions. + path('todo/', 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//', singlewallet, name="singlewallet"), - path('survey_scans//', scansingle, name="scansingle"), + path('survey_scans//', singlewallet, name="singlewallet"), + path('survey_scans//', scansingle, name="scansingle"), # The tunnel and therion drawings files pages re_path(r'^dwgdata/$', dwgdata, name="dwgdata"),