From 37403a7234a78ff10af4310b91e2258eef8bfb14 Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@klebos.com>
Date: Mon, 26 Apr 2021 18:08:42 +0100
Subject: [PATCH] renamed tunnel to drawing or dwg

---
 core/models/survex.py   |  4 +--
 core/views/other.py     |  2 +-
 core/views/surveys.py   | 22 ++++++-------
 parsers/surveys.py      | 70 ++++++++++++++++++++---------------------
 templates/dwgfiles.html | 16 +++++-----
 5 files changed, 57 insertions(+), 57 deletions(-)

diff --git a/core/models/survex.py b/core/models/survex.py
index 94c6271..31f4646 100644
--- a/core/models/survex.py
+++ b/core/models/survex.py
@@ -199,13 +199,13 @@ class SingleScan(models.Model):
     def __str__(self):
         return "Survey Scan Image: " + str(self.name) + " in " + str(self.scansfolder)
 
-class TunnelFile(models.Model):
+class DrawingFile(models.Model):
     tunnelpath          = models.CharField(max_length=200)
     tunnelname          = models.CharField(max_length=200)
     bfontcolours        = models.BooleanField(default=False) # UNUSED now, can be deleted
     manyscansfolders    = models.ManyToManyField("ScansFolder") # implicitly links via folders to scans to SVX files
     scans               = models.ManyToManyField("SingleScan")  # implicitly links via scans to SVX files
-    tunnelcontains      = models.ManyToManyField("TunnelFile")  # case when its a frame type
+    tunnelcontains      = models.ManyToManyField("DrawingFile")  # case when its a frame type
     filesize            = models.IntegerField(default=0)
     npaths              = models.IntegerField(default=0)
     survexfiles         = models.ManyToManyField("SurvexFile")  # direct link to SVX files - not populated yet
diff --git a/core/views/other.py b/core/views/other.py
index c864593..0f74657 100644
--- a/core/views/other.py
+++ b/core/views/other.py
@@ -92,7 +92,7 @@ def controlpanel(request):
             import_surveyscans()
             import_logbooks()
             import_QMs()
-            import_tunnelfiles()
+            import_dwgfiles()
             import_survexblks()
             import_survexpos()
         else:
diff --git a/core/views/surveys.py b/core/views/surveys.py
index 3de395f..d149d4e 100644
--- a/core/views/surveys.py
+++ b/core/views/surveys.py
@@ -8,7 +8,7 @@ from django.conf import settings
 from django.shortcuts import render
 from django.http import HttpResponse, Http404
 
-from troggle.core.models.survex import ScansFolder, SingleScan, SurvexBlock, TunnelFile
+from troggle.core.models.survex import ScansFolder, SingleScan, SurvexBlock, DrawingFile
 from troggle.core.views.expo import getmimetype
 import parsers.surveys
 
@@ -50,8 +50,8 @@ def scanswallets(request):
 def dwgdata(request):
     '''Report on all the drawing files in the system. These were loaded by parsing the entire directory tree
     '''
-    tunnelfiles = TunnelFile.objects.all()
-    return render(request, 'dwgfiles.html', { 'tunnelfiles':tunnelfiles, 'settings': settings })
+    dwgfiles = DrawingFile.objects.all()
+    return render(request, 'dwgfiles.html', { 'dwgfiles':dwgfiles, 'settings': settings })
     
 
 def dwgfilesingle(request, path):
@@ -59,12 +59,12 @@ def dwgfilesingle(request, path):
     but it might be a Therion file
     '''
     try:
-        tunnelfile = TunnelFile.objects.get(tunnelpath=urlunquote(path)) 
+        dwgfile = DrawingFile.objects.get(tunnelpath=urlunquote(path)) 
     except:
         message = f'Drawing file error or not found \'{path}\' .'
         return render(request, 'errors/generic.html', {'message': message})   
         
-    tfile = Path(settings.TUNNEL_DATA, tunnelfile.tunnelpath)
+    tfile = Path(settings.TUNNEL_DATA, dwgfile.tunnelpath)
     try: # for display not download
         return HttpResponse(content=open(tfile, errors='strict'), content_type="text/xhtml") 
     except UnicodeDecodeError:
@@ -82,11 +82,11 @@ def dwgfileupload(request, path):
     '''uploads a drawing file, but where is the Form? This just processes POST info. Apparently unfinished?
     '''
     try:
-        tunnelfile = TunnelFile.objects.get(tunnelpath=urlunquote(path)) # need to check if inavlid query string and produce friendly error
+        dwgfile = DrawingFile.objects.get(tunnelpath=urlunquote(path)) # need to check if inavlid query string and produce friendly error
     except:
         message = f'Drawing file error or not found \'{path}\' .'
         return render(request, 'errors/generic.html', {'message': message})   
-    tfile = Path(settings.TUNNEL_DATA, tunnelfile.tunnelpath)
+    tfile = Path(settings.TUNNEL_DATA, dwgfile.tunnelpath)
     
     project, user, password, tunnelversion = request.POST["tunnelproject"], request.POST["tunneluser"], request.POST["tunnelpassword"], request.POST["tunnelversion"]
     print(project, user, tunnelversion)
@@ -106,7 +106,7 @@ def dwgfileupload(request, path):
     if os.path.split(path)[1] != uploadedfile.name:  
         return HttpResponse(content="Error: name disagrees", content_type="text/plain")
     
-    orgsize = tunnelfile.filesize   # = os.stat(tfile)[stat.ST_SIZE]
+    orgsize = dwgfile.filesize   # = os.stat(tfile)[stat.ST_SIZE]
         
     ttext = uploadedfile.read()
     
@@ -117,11 +117,11 @@ def dwgfileupload(request, path):
     fout.close()
     
     # redo its settings of 
-    parsers.surveys.SetTunnelfileInfo(tunnelfile)
-    tunnelfile.save()
+    parsers.surveys.SetTunnelfileInfo(dwgfile) # commented out
+    dwgfile.save()
     
     uploadedfile.close()
-    message = "File size %d overwritten with size %d" % (orgsize, tunnelfile.filesize)
+    message = "File size %d overwritten with size %d" % (orgsize, dwgfile.filesize)
     return HttpResponse(content=message, content_type="text/plain")
 
     
diff --git a/parsers/surveys.py b/parsers/surveys.py
index 11aa805..30361d4 100644
--- a/parsers/surveys.py
+++ b/parsers/surveys.py
@@ -10,7 +10,7 @@ from PIL import Image
 from functools import reduce
 
 import settings
-from troggle.core.models.survex import SingleScan, ScansFolder, TunnelFile
+from troggle.core.models.survex import SingleScan, ScansFolder, DrawingFile
 from troggle.core.models.troggle import DataIssue
 from troggle.core.utils import save_carefully
 
@@ -118,7 +118,7 @@ def LoadListScans():
             LoadListScansFile(scansfolder)
             
 
-def find_tunnel_scan(tunnelfile, path):
+def find_tunnel_scan(dwgfile, path):
     '''Is given a line of text 'path' which may or may not contain a recognisable name of a scanned file
     which we have already seen when we imported all the files we could find in the surveyscans direstories
     '''
@@ -130,7 +130,7 @@ def find_tunnel_scan(tunnelfile, path):
         if len(scansfolderl):
             scansfolder = scansfolderl[0]
             if len(scansfolderl) > 1:
-                message = "! More than one scan FOLDER matches filter query. [{}]: {} {} {} {}".format(scansfilel[0], mscansdir.group(1), mscansdir.group(2), tunnelfile.tunnelpath, path)
+                message = "! More than one scan FOLDER matches filter query. [{}]: {} {} {} {}".format(scansfilel[0], mscansdir.group(1), mscansdir.group(2), dwgfile.tunnelpath, path)
                 print(message)
                 DataIssue.objects.create(parser='Tunnel', message=message)
             
@@ -138,27 +138,27 @@ def find_tunnel_scan(tunnelfile, path):
             scansfilel = scansfolder.singlescan_set.filter(name=mscansdir.group(2))
             if len(scansfilel):
                 if len(scansfilel) > 1:
-                    message = "! More than one image FILENAME matches filter query. [{}]: {} {} {} {}".format(scansfilel[0], mscansdir.group(1), mscansdir.group(2), tunnelfile.tunnelpath, path)
+                    message = "! More than one image FILENAME matches filter query. [{}]: {} {} {} {}".format(scansfilel[0], mscansdir.group(1), mscansdir.group(2), dwgfile.tunnelpath, path)
                     print(message)
                     DataIssue.objects.create(parser='Tunnel', message=message)
                 scansfile = scansfilel[0]
 
         if scansfolder:
-            tunnelfile.manyscansfolders.add(scansfolder)
+            dwgfile.manyscansfolders.add(scansfolder)
         if scansfile:
-            tunnelfile.scans.add(scansfile)
+            dwgfile.scans.add(scansfile)
     
     elif path and not re.search(r"\.(?:png|jpg|pdf|jpeg|gif|pdf)$(?i)", path):
         name = os.path.split(path)[1]
-        rtunnelfilel = TunnelFile.objects.filter(tunnelname=name)
-        if len(rtunnelfilel):
+        rdwgfilel = DrawingFile.objects.filter(tunnelname=name)
+        if len(rdwgfilel):
             message = "! Two paths with same name [{}]: {}".format(path, name)
             print(message)
             DataIssue.objects.create(parser='Tunnel', message=message)
-            rtunnelfile = rtunnelfilel[0]
-            tunnelfile.tunnelcontains.add(rtunnelfile)
+            rdwgfile = rdwgfilel[0]
+            dwgfile.tunnelcontains.add(rdwgfile)
 
-    tunnelfile.save()
+    dwgfile.save()
 
 def findimageinsert(therionfile, xth_me):
     '''Tries to link the drawing file (Therion format) to the referenced image (scan) file
@@ -175,7 +175,7 @@ rx_scrap  = re.compile(r'^survey (\w*).*$', re.MULTILINE)
 rx_input  = re.compile(r'^input (\w*).*$', re.MULTILINE)
 
 def settherionfileinfo(filetuple):
-    '''Read in the drawing file contents and sets values on the tunnelfile object
+    '''Read in the drawing file contents and sets values on the dwgfile object
     '''
     thtype, therionfile = filetuple
     
@@ -199,7 +199,7 @@ def settherionfileinfo(filetuple):
     therionfile.save()
   
     # scan and look for survex blocks that might have been included, and image scans (as for tunnel drawings)
-    # which would populate tunnelfile.survexfile
+    # which would populate dwgfile.survexfile
     
     # in .th2 files:
     # ##XTHERION## xth_me_image_insert {500 1 1.0} {1700 {}} ../../../expofiles/surveyscans/2014/01popped_elev1.jpeg 0 {}
@@ -224,14 +224,14 @@ def settherionfileinfo(filetuple):
 rx_skpath = re.compile(rb'<skpath')
 rx_pcpath = re.compile(rb'<pcarea area_signal="frame".*?sfsketch="([^"]*)" sfstyle="([^"]*)"')
 
-def settunnelfileinfo(tunnelfile):
-    '''Read in the drawing file contents and sets values on the tunnelfile object
+def setdwgfileinfo(dwgfile):
+    '''Read in the drawing file contents and sets values on the dwgfile object
     Should try to read the date too e.g. tunneldate="2010-08-16 22:51:57
     then we could display on the master calendar per expo.
     '''
-    ff = os.path.join(settings.TUNNEL_DATA, tunnelfile.tunnelpath)
-    tunnelfile.filesize = os.stat(ff)[stat.ST_SIZE]
-    if tunnelfile.filesize <= 0:
+    ff = os.path.join(settings.TUNNEL_DATA, dwgfile.tunnelpath)
+    dwgfile.filesize = os.stat(ff)[stat.ST_SIZE]
+    if dwgfile.filesize <= 0:
         message = "! Zero length xml file {}".format(ff)
         print(message)
         DataIssue.objects.create(parser='Drawings', message=message)
@@ -240,20 +240,20 @@ def settunnelfileinfo(tunnelfile):
     ttext = fin.read()
     fin.close()
    
-    tunnelfile.npaths = len(rx_skpath.findall(ttext))
-    tunnelfile.save()
+    dwgfile.npaths = len(rx_skpath.findall(ttext))
+    dwgfile.save()
     
     # example drawing file in Tunnel format.
     # <tunnelxml tunnelversion="version2009-06-21 Matienzo" tunnelproject="ireby" tunneluser="goatchurch" tunneldate="2009-06-29 23:22:17">
     # <pcarea area_signal="frame" sfscaledown="12.282584" sfrotatedeg="-90.76982" sfxtrans="11.676667377221136" sfytrans="-15.677173422877454" sfsketch="204description/scans/plan(38).png" sfstyle="" nodeconnzsetrelative="0.0">
     
     for path, style in rx_pcpath.findall(ttext):
-        find_tunnel_scan(tunnelfile, path.decode())
+        find_tunnel_scan(dwgfile, path.decode())
     
     # should also scan and look for survex blocks that might have been included, and image scans
-    # which would populate tunnelfile.survexfile
+    # which would populate dwgfile.survexfile
 
-    tunnelfile.save()
+    dwgfile.save()
 
 
 def load_drawings_files():
@@ -261,7 +261,7 @@ def load_drawings_files():
     '''
     all_xml = []
     drawdatadir = settings.TUNNEL_DATA
-    TunnelFile.objects.all().delete()
+    DrawingFile.objects.all().delete()
     DataIssue.objects.filter(parser='Drawings').delete()
     DataIssue.objects.filter(parser='Therion').delete()
     DataIssue.objects.filter(parser='Tunnel').delete()
@@ -278,30 +278,30 @@ def load_drawings_files():
                 drawingsdirs.append(lf) # lunatic! adding to list in middle of list while loop!
             elif f[-4:] == ".xml":
                 # Always creates new
-                tunnelfile = TunnelFile(tunnelpath=lf, tunnelname=os.path.split(f[:-4])[1])
-                tunnelfile.save()
-                all_xml.append(('xml',tunnelfile))
+                dwgfile = DrawingFile(tunnelpath=lf, tunnelname=os.path.split(f[:-4])[1])
+                dwgfile.save()
+                all_xml.append(('xml',dwgfile))
             elif f[-3:] == ".th":
                 # Always creates new
-                tunnelfile = TunnelFile(tunnelpath=lf, tunnelname=os.path.split(f[:-4])[1])
-                tunnelfile.save()
-                all_xml.append(('th',tunnelfile))
+                dwgfile = DrawingFile(tunnelpath=lf, tunnelname=os.path.split(f[:-4])[1])
+                dwgfile.save()
+                all_xml.append(('th',dwgfile))
             elif f[-4:] == ".th2":
                 # Always creates new
-                tunnelfile = TunnelFile(tunnelpath=lf, tunnelname=os.path.split(f[:-4])[1])
-                tunnelfile.save()
-                all_xml.append(('th2',tunnelfile))
+                dwgfile = DrawingFile(tunnelpath=lf, tunnelname=os.path.split(f[:-4])[1])
+                dwgfile.save()
+                all_xml.append(('th2',dwgfile))
 
     print(f' - {len(all_xml)} Drawings files found')
 
     for d in all_xml:
         if d[0] == 'xml':
-            settunnelfileinfo(d[1])
+            setdwgfileinfo(d[1])
         # important to import .th2 files before .th so that we can assign them when found in .th files
         if d[0] == 'th2':
             settherionfileinfo(d)
         if d[0] == 'th':
             settherionfileinfo(d)
            
-    # for drawfile in TunnelFile.objects.all():
+    # for drawfile in DrawingFile.objects.all():
         # SetTunnelfileInfo(drawfile)
diff --git a/templates/dwgfiles.html b/templates/dwgfiles.html
index 23fa314..2f71196 100644
--- a/templates/dwgfiles.html
+++ b/templates/dwgfiles.html
@@ -10,27 +10,27 @@
 <table  style="font-size: 85%" width=95%>
 <tr><th>File</th><th>Size</th><th>Paths</th><th>Scans folder</th><th>Scan files</th><th>Frames</th></tr>
 
-{% for tunnelfile in tunnelfiles %}
+{% for dwgfile in dwgfiles %}
   <tr>
-    <td><a href="{% url "dwgfilesingle" tunnelfile.tunnelpath %}">{{tunnelfile.tunnelpath}}</a></td>
-    <td align="right" style="padding:2px">{{tunnelfile.filesize}}</td>
-    <td align="right" style="padding:2px">{{tunnelfile.npaths}}</td>   
+    <td><a href="{% url "dwgfilesingle" dwgfile.tunnelpath %}">{{dwgfile.tunnelpath}}</a></td>
+    <td align="right" style="padding:2px">{{dwgfile.filesize}}</td>
+    <td align="right" style="padding:2px">{{dwgfile.npaths}}</td>   
 
     <td style="padding:2px">
-    {% for scansfolder in tunnelfile.manyscansfolders.all %}
+    {% for scansfolder in dwgfile.manyscansfolders.all %}
       <a href="{{scansfolder.get_absolute_url}}">{{scansfolder.walletname}}</a>
     {% endfor %}
     </td>   
 
     <td style="padding:2px">
-    {% for singlescan in tunnelfile.scans.all %}
+    {% for singlescan in dwgfile.scans.all %}
       <a href="{{singlescan.get_absolute_url}}">{{singlescan.name}}</a>
     {% endfor %}
     </td>   
 
     <td style="padding:2px">
-    {% for rtunnelfile in tunnelfile.tunnelcontains.all %}
-      <a href="{% url "dwgfilesingle" rtunnelfile.tunnelpath %}">{{rtunnelfile.tunnelpath}}</a>
+    {% for rdwgfile in dwgfile.tunnelcontains.all %}
+      <a href="{% url "dwgfilesingle" rdwgfile.tunnelpath %}">{{rdwgfile.tunnelpath}}</a>
     {% endfor %}
     </td>   
   </tr>