summaryrefslogtreecommitdiffstats
path: root/core/views/uploads.py
blob: 82e34c14f3b2508c9d90557f05828ba8bb1b621e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
import re, os
import subprocess
import json
import settings
import urllib
import operator

from pathlib import Path
from functools import reduce

from django import forms

from django.conf import settings
from django.urls import reverse
from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.template import Context, loader
from django.core.files.storage import FileSystemStorage, default_storage
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned


#from troggle import settings
from troggle.parsers.imports import import_caves, import_people, import_surveyscans
from troggle.parsers.imports import import_logbooks, import_QMs, import_drawingsfiles, import_survex
from troggle.parsers.scans import wallet_blank_json, contentsjson
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
from troggle.core.models.troggle import DataIssue
from troggle.core.models.troggle import Expedition, Person, PersonExpedition
from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip
from troggle.core.models.survex import DrawingFile, Wallet
from troggle.core.views.scans import oldwallet, walletindex
from troggle.core.views.caves import getCave

from .auth import login_required_if_public
#from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt

'''File upload 'views' 
'''

todo = '''
- Register uploaded filenames in the Django db without needing to wait for a reset & bulk file import

- Refactor scanupload() as it contains all the wallets 'complaints' code from the old script wallets.py

- Need to validate uploaded file as being a valid image file, not a dubious script or hack

- Write equivalent GPX upload form system, similar to scanupload() but in expofiles/gpslogs/
  Need to validate it as being a valid GPX file using an XML parser, not a dubious script or hack
  
- Validate Tunnel & Therion files using an XML parser in dwgupload(). Though Julian says tunnel is only mostly correct XML

- Validate image files using a magic recogniser in scanupload() https://pypi.org/project/reportlab/ or 
  https://stackoverflow.com/questions/889333/how-to-check-if-a-file-is-a-valid-image-file

- Enable folder creation in dwguploads or as a separate form

'''

class FilesForm(forms.Form):  # not a model-form, just a form-form
    uploadfiles = forms.FileField() 
     
class FilesRenameForm(forms.Form):  # not a model-form, just a form-form
    uploadfiles = forms.FileField() 
    renameto = forms.CharField(strip=True, required=False)

class WalletGotoForm(forms.Form):  # not a model-form, just a form-form
    walletgoto = forms.CharField(strip=True, required=False) 
    
class TextForm(forms.Form):  # not a model-form, just a form-form
    photographer = forms.CharField(strip=True) 

class WalletForm(forms.Form):  # not a model-form, just a form-form
    descriptionw = forms.CharField(strip=True, required=False) 
    people = forms.CharField(strip=True, required=False) 
    survexnr = forms.CharField(strip=True, required=False) 
    qmsw = forms.CharField(strip=True, required=False) 
    date = forms.CharField(strip=True, required=True) # the only required field
    websiteupt = forms.CharField(strip=True, required=False) 
    elevnr = forms.CharField(strip=True, required=False) 
    cave = forms.CharField(strip=True, required=False) 
    psg = forms.CharField(strip=True, required=False) 
    plannr = forms.CharField(strip=True, required=False) 
    electronic = forms.CharField(strip=True, required=False) 
    pland = forms.CharField(strip=True, required=False) 
    elevd = forms.CharField(strip=True, required=False) 
    url = forms.CharField(strip=True, required=False) 
    survex = forms.CharField(strip=True, required=False) 

xlate = {"url": "description url",
        "descriptionw": "description written",
        "people": "people",
        "date": "date",
        "cave": "cave",
        "plannr": "plan not required",
        "survexnr": "survex not required",
        "qmsw": "qms written",
        "elevnr": "elev not required",
        "websiteupt": "website updated",
        "electronic": "electronic survey",
        "pland": "plan drawn",
        "elevd": "elev drawn",
        "psg": "name", # a name for this wallet
        "survex": "survex file",
        }

def get_complaints(complaints, waldata, svxfiles, files, wallet, wurl):
    '''Taken from old script wallets.py and edited to make more comprehensible
    Loads the survex files names and processes all complaints
    
    '''
    # If skipping through the wllets on the upload form, the wallet may not yet exist
    try:
        w = Wallet.objects.get(walletname=wallet)
    except ObjectDoesNotExist:
        return None, None
        
    ticks = w.get_ticks()
    
    # Date
    if not waldata["date"]:
        complaints.append("A date is mandatory. No data can be updated or edited unless you specify a date. Look in the survex file if there is one.") 

    # People
    if waldata["people"]==["NOBODY"] or waldata["people"]==["Unknown"]:
        complaints.append("Someody must have done this. Look in the survex file, or in the logbook entries for this date, for the people who created this data.") 
        
    # survex, but get_ticks has already done much of this ??
    survex_complaint = ""

    if waldata["survex file"]:
        if not type(waldata["survex file"])==list: # a string also is a sequence type, so do it this way
            waldata["survex file"] = [waldata["survex file"]]       
        for svx in waldata["survex file"]:
            if svx !="":
                svxfiles.append(svx)
                if not (Path(settings.SURVEX_DATA) / svx).is_file():
                    file_complaint = f"{wallet} Incorrect survex file name in {wallet} wallet data. {svx} was not found in LOSER repo"
                    complaints.append(file_complaint)
                    message = f"! {file_complaint}"
                    print(message)
                    DataIssue.objects.update_or_create(parser='scans', message=message, url=wurl) # set URL to this wallet folder

    if waldata["survex not required"] and waldata["survex file"] != "":
        survex_complaint = "Survex is stated as not required and yet there is a survex file!"    
    if not waldata["survex not required"] and waldata["survex file"] == "":
        survex_complaint = "A survex file is required, but has not been specified!"    
    if survex_complaint:
        complaints.append(survex_complaint)


    # Notes required
    if ticks['N'] != "green": 
        complaints.append("The notes needs scanning (or renaming): no noteNN.jpg or XXnote.jpg file found; and this is not an electronic survey.") 

    # Plan drawing required
    if ticks['P'] != "green": 
         complaints.append("The plan needs drawing (or renaming, or tick 'Plan drawn' checkbox or 'Plan not required' checkbox): no planNN.jpg or XXplan.jpg file found.") 

    # Elev drawing required
    if ticks['E'] != "green": 
        complaints.append("The elevation needs drawing (or renaming, or tick 'Elev drawn' checkbox or 'Elev not required' checkbox): no elevNN.jpg or XXelev.jpg file found.") 

    # ETherion
    if ticks['T'] != "green": 
        complaints.append("Tunnel or Therion drawing files need drawing. Or if this an electronic survey, please tick the 'Electronic survey' checkbox.") 
     
    # Description
    if not waldata["description written"]: 
        complaints.append("The guidebook description needs writing into the survex file. Tick the 'Cave description written' checkbox when this is done.") 
    # QMs
    if not waldata["qms written"]:
        complaints.append("The QMs needs writing into the survex file. Tick the 'QMs written' checkbox when this is done.") 

    # Website
    if not waldata["website updated"]:
        complaints.append("The cave description website is marked as needing updating using the guidebook description from the survex file. Tick the 'Website updated' checkbox when this is done.")

    # FInd the cave, if it exists
    if waldata["cave"]:
        try:
            caveid = waldata["cave"]
            caveid = caveid.replace("/","-")
            caveobject = getCave(caveid)   
            print(f'getCave for id "{waldata["cave"]}" {caveobject}')
            if not caveobject.url == waldata["description url"]:
                complaints.append(f'The URL of cave description \"{waldata["description url"]}\" does not match the one on record for this cave which is: "{caveobject.url}". If the wallet is not for a cave, put a useful URL here.')
        except Cave.MultipleObjectsReturned: 
            complaints.append(f'The cave ID \'{waldata["cave"]}\' is AMBIGUOUS. Please fix it.')
            caveobject = None
        except ObjectDoesNotExist:
            complaints.append(f'The cave ID \'{waldata["cave"]}\' is not recognised. Please fix it.')
            caveobject = None
    else:
        complaints.append(f'No cave ID is given. Please give some text, even if it is just "surface survey" or "scraps found in hut"')
        caveobject = None
         

    return complaints, caveobject
    
# @login_required_if_public
def scanupload(request, path=None):
    '''Upload scanned image files into a wallet on /expofiles
    Also display AND EDIT the contents.json data in the wallet.
    
    The Wallet object and the contents.json file are created when the user
    first uploads files. 
    
    This does NOT use a Django model linked to a Django form. Just a simple Django form.
    You will find the Django documentation on forms very confusing,
    as it covers many very differnet things we do not need. This is simpler.
    
    This subsumes much of the code which was in the old wallets.py script and so this function is very long
    indeed and needs refactoring.
    
    REWRITE bits using the ticklist, dateify, caveify, populate etc utility functions in core.view.scans.py 
    '''
    git = settings.GIT
    filesaved = False
    actual_saved = []
    
    def save_json(jsondict):
        newfolder = contents_path.parent
        print(f'---  wallet directory in :drawings: repo {newfolder=}')
        if not os.path.exists(contents_path.parent):
            print(f'--- No wallet directory in :drawings: repo, so creating it')
            os.makedirs(contents_path.parent)
        
        with open(contents_path, "w") as jfile:
            json.dump(jsondict, jfile, indent = 1)
        print(f'--- FINISHED saving to JSON at {contents_path}')
        
    def make_wallet(walletname):
        '''We need a wallet Object so that the django template stuff can find the files
        '''
        try:
            w, created = Wallet.objects.get_or_create(walletname=walletname)
            print(f'wallet string {walletname}, wallet object {w} created new?: {created}')   
            if created:
                w.fpath = Path(settings.SCANS_ROOT, walletname[0:4], walletname)
                w.save()
        except:
             print(f'wallet string {walletname}, FAIL TO GET or create WALLET OBJECT')
             raise

    def commit_json():
        destfolder = contents_path.parent
        dr_add = subprocess.run([git, "add", contentsjson], cwd=destfolder, capture_output=True, text=True)
        if dr_add.returncode != 0:
            msgdata = 'Ask a nerd to fix this.\n--' + dr_add.stderr + '\n--' + dr_add.stdout  + '\n--return code: ' + str(dr_add.returncode)
            message = f'CANNOT git on server for this file {contentsjson}. Edits saved but not added to git.\n\n' + msgdata
            print(message)
            return render(request,'errors/generic.html', {'message': message})
        else:
            dr_commit = subprocess.run([git, "commit", "-m", f'Update of {contentsjson} in wallet'], cwd=destfolder, capture_output=True, text=True)
            # This produces return code = 1 if it commits OK
            if dr_commit.returncode != 0:
                msgdata = 'Ask a nerd to fix this.\n\n' + dr_commit.stderr + '\n\n' + dr_commit.stdout  + '\n\nreturn code: ' + str(dr_commit.returncode)
                message = f'Error code with git on server for this {contentsjson}. File is added to git, but NOT committed.\n' + msgdata
                print(message)
                return render(request,'errors/generic.html', {'message': message})

    
    checkboxes = ["description written", "survex not required", "qms written", "website updated",
            "plan not required", "plan drawn", "elev not required", "elev drawn", "electronic survey" ]
    if path:
        wallet = urllib.parse.unquote(path)
    else:
        wallet = "2022#01" # improve this later

    year = wallet[:4]
    try:
        if int(year) <= 1998: 
            #print(f'! - FORM scanupload - start {wallet} REDIRECT TO OLDWALLET')
            return(oldwallet(request, path))
    except:
        # if nonumeric wallet name for example
        return(oldwallet(request, path))
   
    
    if str(wallet).lower().endswith('indexpages'):
        # print(f'! - FORM scanupload - start {wallet} REDIRECT TO OLDWALLET')
        return(walletindex(request, path))

    if not re.match('(19|20)\d\d[:#]\d\d', wallet):
        wallet = "2022:01" # improve this later
    # print(f'! - FORM scanupload - start {wallet}')
    
    if path:
        pass
        # print(f'! - FORM scanupload - start wallet:{wallet}: path:{path}:')
    if int(year) < 1977: 
        year = "1977"
    if int(year) > 2050: 
        year = "2050"
    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}'

    wurl = f"/scanupload/{wallet}".replace('#', ':')     
    wallet = wallet.replace(':','#')
    dirpath = Path(settings.SCANS_ROOT, year, wallet)
    contents_path = Path(settings.DRAWINGS_DATA, "walletjson") / year / wallet / contentsjson
    
    form = FilesForm()
   
    if request.method == 'POST':
        if "psg" in request.POST:              
            formj = WalletForm(request.POST)
            # Beware. All fields returned as strings. Must re-type them as lists etc. before using or re-saving
            # Also lots of hassle with lists of strings interpreted as a single string
            # Unset checkboxes do not return any value, checked ones return "True". So need initialising to False
            if formj.is_valid():
                posted = request.POST.copy()
                posted.pop("csrfmiddlewaretoken") # discard this
                wd = wallet_blank_json
                for f in checkboxes:
                    wd[f] = False
                    #print(f'--- wd ${f}$  - {wd[f]}')
                for f in posted:
                    wd[xlate[f]] = posted[f].replace("\'", "\"")

                    if posted[f] =="True":
                        wd[xlate[f]] = True

                wd["people"] = wd["people"][1:-1].replace("\"", "").split(",")
                for i, elem in enumerate(wd["people"]):
                    wd["people"][i] = elem.strip()
                    
                #print(f'--- ${wd["survex file"]}$  - {type(wd["survex file"])}')
                if wd["survex file"]: # allow for no survex file at all
                    if wd["survex file"][0] == '[':
                        wd["survex file"] = wd["survex file"][1:-1]
                    wd["survex file"] = wd["survex file"].replace("\"", "").split(",")
                    for i, elem in enumerate(wd["survex file"]):
                       wd["survex file"][i] = elem.strip()
                    #print(f'--- {wd["survex file"]} - {type(wd["survex file"])}')

                save_json(wd)
                make_wallet(wallet)
                commit_json()
                
            else:
                print(f'--- INVALID JSON Update form submitted')
                print(formj.errors)
                return render(request,'errors/generic.html', {'message': formj.errors})

        elif "walletgoto" in request.POST:  # not editing wallet data, uploading a file.. going direct to a named wallet
            formg = WalletGotoForm(request.POST,request.FILES) 
            if formg.is_valid():
                walletgoto = request.POST["walletgoto"]
               
                return HttpResponseRedirect(f'/scanupload/{walletgoto.replace("#",":")}')
                
        else: # not editing wallet data, uploading a file..
            form = FilesForm(request.POST,request.FILES)
     
            if form.is_valid():
                #f = request.FILES["uploadfiles"]
                multiple = request.FILES.getlist('uploadfiles')
                fs = FileSystemStorage(os.path.join(dirpath)) # creates wallet folder if necessary
               
                actual_saved = []
                if multiple:
                    for f in multiple:
                        try: # crashes in Django os.chmod call if on WSL, but does save file!
                            saved_filename = fs.save(f.name, content=f) 
                        except:
                            print(f'\n !! Permissions failure ?! on attempting to save file {f.name}')
                            if 'saved_filename' in locals():
                                if saved_filename.is_file():
                                    actual_saved.append(saved_filename)
                                    # print(f'! - FORM scanupload multiple {actual_saved}')
                                    filesaved = True
                    save_json(wallet_blank_json)
                    make_wallet(wallet)
                    commit_json()
    files = []
    dirs = []
    # print(f'! - FORM scanupload - start {wallet} {dirpath}')
    if dirpath.is_dir():
        create = False
        try:
            for f in dirpath.iterdir():
                if f.is_dir():
                    dirs.append(f.name)
                if f.is_file():
                    files.append(f.name)
        except FileNotFoundError:
            files.append('(no wallet yet. It would be created if you upload a scan and then save the form with a date.)')
    else:
           create = True
         
    if len(files) >0 :
        files = sorted(files)
        
    if dirs:
        dirs = sorted(dirs)
        
    waldata = {}   
    if contents_path.is_file():
        with open(contents_path) as json_file:
            try:
                waldata = json.load(json_file)
            except:
                message = f"! {wallet} Failed to load {contents_path} JSON file"
                print(message)
                DataIssue.objects.create(parser='scans', message=message, url=wurl)  # set URL to this wallet folder
                raise
    cave =""
    psg = ""
    chkplannr = ""
    chkpland = ""
    svxfiles = []
    checked = {}
    if waldata:
        if not waldata["people"]:
            waldata["people"]=["NOBODY"]
        if not type(waldata["people"])==list: 
            if waldata["people"][0] == '"':
                waldata["people"] = waldata["people"][1:-1]
            waldata["people"] = list(waldata["people"])

        if not waldata["description url"]:
            waldata["description url"]=""
        if waldata["cave"]:
            cave =  waldata["cave"] # text string
        if waldata["name"]:
            psg =  waldata["name"]

        
        #Survex and survex complaints
        complaints, caveobject = get_complaints([], waldata, svxfiles, files, wallet, wurl)

     
        for f in checkboxes:
             if waldata[f]:
                 checked[f] = "checked"
                 
        context = {'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty,
                'files': files, 'dirs': dirs, 'waldata': waldata, 'svxfiles': svxfiles,
                'checked': checked,
                'create': create, 
                'complaints': complaints,
                'caveobject': caveobject,
                'people':  waldata["people"], 'peoplesize': str(len(str(waldata["people"]))),
                'filesaved': filesaved, 'actual_saved': actual_saved }

        return render(request, 'walletform.html', 
            {'form': form, 'wallet': wallet, **context, 
            'date': waldata["date"],
            'url':  waldata["description url"], 'urlsize': str(len(str(waldata["description url"]))),
            'survex': waldata["survex file"], 'survexsize': str(len(str(waldata["survex file"]))),
            'cave': cave, 'psg': psg, 'psgsize': str(max(12,len(str(psg))))})
    else: # no wallet here
        context = {'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty,
                'files': files, 'dirs': dirs, 'waldata': waldata, 'svxfiles': svxfiles,
                'checked': checked,
                'create': create, 
                'people':  "", 'peoplesize': 12,
                'filesaved': filesaved, 'actual_saved': actual_saved }
        
        return render(request, 'walletform.html', 
            {'form': form, 'wallet': wallet, **context, 
            'date': "",
            'url':  "", 'urlsize': 12,
            'survex': "", 'survexsize': 12,
            'cave': cave, 'psg': psg, 'psgsize': 12})

@login_required_if_public
def photoupload(request, folder=None):
    '''Upload photo image files into /expofiles/photos/<year>/<photographer>/
    This does NOT use a Django model linked to a Django form. Just a simple Django form.
    You will find the Django documentation on forms very confusing, This is simpler.
    
        
    When uploading from a phone, it is useful to be able to rename the file to something 
    meaningful as this is difficult to do on a phone. Previously we had assumed files would
    be renamed to something useful before starting the upload.
    Unfortunately this only works when uploading one file at at time ,
    inevitable once you think about it.
    '''
    year = settings.PHOTOS_YEAR
    filesaved = False
    actual_saved = []
 
    context = {'year': year, 'placeholder': "AnathemaDevice"}
    
    yearpath = Path(settings.PHOTOS_ROOT, year)
    
    if folder == str(year) or folder == str(year) +"/":
        folder = None
    
    if folder is None:
        folder = "" # improve this later
        dirpath = Path(settings.PHOTOS_ROOT, year)
        urlfile = f'/expofiles/photos/{year}'
        urldir  = f'/photoupload/{year}'
    else: # it will contain the year as well as the photographer
        dirpath = Path(settings.PHOTOS_ROOT, folder)
        if dirpath.is_dir():
            urlfile = f'/expofiles/photos/{folder}'
            urldir  = Path('/photoupload') / folder
        else:
            folder = "" # improve this later
            dirpath = Path(settings.PHOTOS_ROOT, year)
            urlfile = f'/expofiles/photos/{year}'
            urldir  = f'/photoupload/{year}'
      
        
    form = FilesRenameForm()
    formd = TextForm()  
     
    if request.method == 'POST':
        if "photographer" in request.POST:              
            formd = TextForm(request.POST)
            if formd.is_valid():
                newphotographer = request.POST["photographer"]
                (yearpath / newphotographer).mkdir(exist_ok=True)
        else:
            form = FilesRenameForm(request.POST,request.FILES)
            if form.is_valid():
                f = request.FILES["uploadfiles"]
                multiple = request.FILES.getlist('uploadfiles')
                # NO CHECK that the files being uploaded are image files
                fs = FileSystemStorage(dirpath)
                
                renameto = request.POST["renameto"]
               
                actual_saved = []
                if multiple:                   
                    if len(multiple) == 1:
                        if renameto != "":
                            try: # crashes in Django os.chmod call if on WSL, but does save file!
                                saved_filename = fs.save(renameto, content=f) 
                            except:
                                print(f'\n !! Permissions failure ?! on attempting to save file {f.name}')
                                if 'saved_filename' in locals():
                                    if saved_filename.is_file():
                                        actual_saved.append(saved_filename)
                                        filesaved = True  
                        else: # multiple is the uploaded content
                            try: # crashes in Django os.chmod call if on WSL, but does save file!
                                saved_filename = fs.save(f.name, content=f) 
                            except:
                                print(f'\n !! Permissions failure ?! on attempting to save file {f.name}')
                                if 'saved_filename' in locals():
                                    if saved_filename.is_file():
                                        actual_saved.append(saved_filename)
                                        filesaved = True    
                    else: # multiole is a list of content
                        for f in multiple:
                            try: # crashes in Django os.chmod call if on WSL, but does save file!
                                saved_filename = fs.save(f.name, content=f) 
                            except:
                                print(f'\n !! Permissions failure ?! on attempting to save file {f.name}')
                                if 'saved_filename' in locals():
                                    if saved_filename.is_file():
                                        actual_saved.append(saved_filename)
                                        filesaved = True    
    files = []
    dirs = []
    try:
        for f in dirpath.iterdir():
            if f.is_dir():
                dirs.append(f.name)
            if f.is_file():
                files.append(f.name)           
    except FileNotFoundError:
        files.append('(no folder yet - would be created)')
    if len(files) >0 :
        files = sorted(files)
        
    if dirs:
        dirs = sorted(dirs)

    return render(request, 'photouploadform.html', 
        {'form': form, **context, 'urlfile': urlfile, 'urldir': urldir,'folder': folder, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved})

@login_required_if_public
def dwgupload(request, folder=None, gitdisable='no'):
    '''Upload DRAWING files (tunnel or therion) into the upload folder in :drawings
    AND registers it into the :drawings: git repo.
    
    This does NOT use a Django model linked to a Django form. Just a simple Django form.
    You will find the Django documentation on forms very confusing, This is simpler.
    
    We could validate the uploaded files as being a valid files using an XML parser, not a dubious script or hack

    
    We use get_or_create instead of simply creating a new object in case someone uploads the same file
    several times in one session, and expects them to be overwritten in the database. Although
    the actual file will be duplicated in the filesystem with different random name ending.
    '''
    def dwgvalid(name):
        if name in [ '.gitignore', ]:
            return False
        if Path(name).suffix.lower() in ['.xml', '.th', '.th2', '', '.svg', '.txt']:
            return True # dangerous, we should check the actual file binary signature
        return False

    def dwgvaliddisp(name):
        '''OK to display, even if we are not going to allow a new one to be uploaded
        '''
        if name in [ '.gitignore', ]:
            return False
        if Path(name).suffix.lower() in ['.xml', '.th', '.th2', '', '.svg', '.txt', '.jpg', '.jpeg', '.png', '.pdf', '.top', '.topo']:
            return True # dangerous, we should check the actual file binary signature
        return False

    filesaved = False
    actual_saved = []
    refused = []
    doesnotexist = ''
    # print(f'! - FORM dwgupload - start "{folder}" - gitdisable "{gitdisable}"')
    if folder is None:
        folder = "" # improve this later
        dirpath = Path(settings.DRAWINGS_DATA)
        urlfile = '/dwgdataraw'
        urldir  = '/dwgupload'
    else:
        dirpath = Path(settings.DRAWINGS_DATA, folder)
        urlfile = Path('/dwgdataraw/') / folder
        urldir  = Path('/dwgupload/') / folder
    
    form = FilesForm()
    
    if request.method == 'POST':
        form = FilesForm(request.POST,request.FILES)
        if form.is_valid():
            # print(f'! - FORM dwgupload - POST valid: "{request.FILES["uploadfiles"]}" ')
            f = request.FILES["uploadfiles"]
            multiple = request.FILES.getlist('uploadfiles')
            fs = FileSystemStorage(os.path.join(settings.DRAWINGS_DATA, folder))
           
            actual_saved = []
            refused = []
            
            # GIT see also core/views/expo.py editexpopage()
            # GIT see also core/models/cave.py writetrogglefile()
            if gitdisable != 'yes': # set in url 'dwguploadnogit/'
                git = settings.GIT
            else:
                git = 'echo'
                # print(f'git DISABLED {f.name}')

            if multiple:
                for f in multiple:
                    # print(f'! - FORM dwgupload - file {f} in {multiple=}')
                    if dwgvalid(f.name):
                        try: # crashes in Django os.chmod call if on WSL without metadata drvfs, but does save file!
                            saved_filename = fs.save(f.name, content=f) 
                        except:
                            print(f'! - FORM dwgupload - \n!! Permissions failure ?! on attempting to save file {f.name}. Attempting to continue..')
                        if 'saved_filename' in locals():
                            if Path(dirpath, saved_filename).is_file():
                                actual_saved.append(saved_filename)
                                if gitdisable != 'yes':
                                    dr_add = subprocess.run([git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True)
                                    msgdata =  dr_add.stderr + '\n' + dr_add.stdout  + '\nreturn code: ' + str(dr_add.returncode)
                                    # message = f'! - FORM dwgupload - Success: git ADD on server for this file {saved_filename}.' + msgdata
                                    # print(message)
                                    if dr_add.returncode != 0:
                                        msgdata = 'Ask a nerd to fix this.\n\n' + dr_add.stderr + '\n\n' + dr_add.stdout  + '\n\nreturn code: ' + str(dr_add.returncode)
                                        message = f'! - FORM dwgupload - CANNOT git ADD on server for this file {saved_filename}. Edits saved but not added to git.\n' + msgdata
                                        print(message)
                                        return render(request,'errors/generic.html', {'message': message})
                                dwgfile, created = DrawingFile.objects.get_or_create(dwgpath=saved_filename, dwgname=Path(f.name).stem, filesize=f.size)  
                                dwgfile.save()
                            else:
                                message = f'! - FORM dwgupload - NOT A FILE {Path(dirpath, saved_filename)=}. '
                                print(message)
                        else:
                            message = f'! - FORM dwgupload - Save failure for {saved_filename}. Changes NOT saved.'
                            print(message)
                            return render(request,'errors/generic.html', {'message': message})
                            
                        if saved_filename != f.name:
                            # message = f'! - FORM dwgupload - Save RENAME {f.name} renamed as {saved_filename}. This is OK.'
                            # print(message)
                            pass
                       
                    else:
                        refused.append(f.name)
                        # print(f'REFUSED {f.name}')
                        
            if actual_saved: 
                filesaved = True
                if len(actual_saved) > 1:
                    dots = "..."
                else:
                    dots = ""
                if gitdisable != 'yes':
                    dr_commit = subprocess.run([git, "commit", "-m", f'Drawings upload - {actual_saved[0]}{dots}'], cwd=dirpath, capture_output=True, text=True)
                    # message = f'! - FORM dwgupload - For uploading {actual_saved[0]}{dots}. Edits saved, added to git, and COMMITTED.\n' + msgdata
                    # print(message)
                    # This produces return code = 1 if it commits OK
                    if dr_commit.returncode != 0:
                        msgdata = 'Ask a nerd to fix this.\n\n' + dr_commit.stderr + '\n' + dr_commit.stdout  + '\nreturn code: ' + str(dr_commit.returncode)
                        message = f'! - FORM dwgupload -Error code with git on server for this {actual_saved[0]}{dots}. Edits saved, added to git, but NOT committed.\n' + msgdata
                        print(message)
                        return render(request,'errors/generic.html', {'message': message})
                else:
                    print(f' git disabled "{git=}"')
            else:# maybe all were refused by the suffix test in dwgvalid()
                message = f'! - FORM dwgupload - Nothing actually saved. All were refused. {actual_saved=}'
                print(message)

    files = []
    dirs = []
    # print(f'! - FORM dwgupload - start {folder=} \n"{dirpath=}" \n"{dirpath.parent=}" \n"{dirpath.exists()=}"')
    try:
        for f in dirpath.iterdir():
            if f.is_dir():
                if f.name not in ['.git' ]:
                    dirs.append(f.name)
                    continue
            if f.is_file():
                if dwgvaliddisp(f.name): 
                    files.append(f.name)
                    continue
    except FileNotFoundError:
        doesnotexist = True
    if files:
        files = sorted(files)
        
    if dirs:
        dirs = sorted(dirs)

    return render(request, 'dwguploadform.html', 
        {'form': form, 'doesnotexist': doesnotexist, 'urlfile': urlfile, 'urldir': urldir,'folder': folder, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved, 'refused': refused})