From d598a6d0f5174ede98aea4d5220c38ffdcae31ab Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@klebos.com>
Date: Wed, 14 Apr 2021 21:08:06 +0100
Subject: [PATCH] better integration of svx file DatIssues

---
 core/views/survex.py      |  9 ++++++++-
 parsers/caves.py          | 21 +++++++++++++--------
 templates/dataissues.html |  6 +++---
 templates/svxfile.html    |  4 ++++
 4 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/core/views/survex.py b/core/views/survex.py
index 350c404..df8a629 100644
--- a/core/views/survex.py
+++ b/core/views/survex.py
@@ -104,10 +104,13 @@ class SvxForm(forms.Form):
     outputtype = forms.CharField(widget=forms.TextInput(attrs={"readonly":True}))
     code = forms.CharField(widget=forms.Textarea(attrs={"cols":150, "rows":36}))
     
+    template = False
+    
     def GetDiscCode(self):
         fname = survexdatasetpath / (self.data['filename'] + ".svx")
         if not os.path.isfile(fname):
             print(">>> >>> WARNING - svx file not found, showing TEMPLATE SVX",fname, flush=True)
+            self.template = True
             return survextemplatefile
         fin = open(fname, "rt",encoding='utf8',newline='')
         svxtext = fin.read() 
@@ -167,6 +170,8 @@ def svx(request, survex_file):
     editing) with buttons which allow SAVE, check for DIFFerences from saved, and RUN (which runs the
     cavern executable and displays the output below the main textarea). Requires CSRF to be set upcorrect;ly, and requires permission to write to  the filesystem.
     '''
+    warning = False
+    
     # get the basic data from the file given in the URL
     dirname = os.path.split(survex_file)[0]
     dirname += "/"
@@ -210,7 +215,8 @@ def svx(request, survex_file):
     #process(survex_file)
     if 'code' not in form.data:    
         form.data['code'] = form.GetDiscCode()
-    
+        if form.template:
+            warning = True
     if not difflist:
         difflist.append("none")
     if message:
@@ -220,6 +226,7 @@ def svx(request, survex_file):
     svxincludes = re.findall(r'\*include\s+(\S+)(?i)', form.data['code'] or "")
     
     vmap = {'settings': settings,
+            'warning': warning,
             'has_3d': os.path.isfile(survexdatasetpath / survex_file / ".3d"),
             'title': survex_file,
             'svxincludes': svxincludes,
diff --git a/parsers/caves.py b/parsers/caves.py
index 168e66d..cce680d 100644
--- a/parsers/caves.py
+++ b/parsers/caves.py
@@ -43,31 +43,34 @@ def readcaves():
         print (" - Setting pending caves")
         # Do this first, so that these empty entries are overwritten as they get properly created.
         
-        # For those caves which do not have XML files even though they exist and have surveys
+        # For those caves which do not have cave_data/1623-xxx.html XML files even though they exist and have surveys
         # also needs to be done *before* entrances so that the entrance-cave links work properly.
         pending = ["2007-04", "2007-05", "2007-06", "2007-07", "2007-12", "2009-01", "2009-02", 
                 "2010-06", "2010-07", "2012-ns-01", "2012-ns-02", "2010-04", "2012-ns-05", "2012-ns-06", 
                 "2012-ns-07", "2012-ns-08", "2012-ns-12", "2012-ns-14", "2012-ns-15", "2014-bl888", 
-                "2018-pf-01", "2018-pf-02", "haldenloch", "gruenstein"]
+                "2018-pf-01", "2018-pf-02", "haldenloch"]
         for k in pending:
-            try:
+            url = "1623/" + k
+            try:    
                 cave = Cave(
                         unofficial_number = k, 
-    #                    official_name = "",
-                         underground_description = "Pending cave write-up - creating as empty object. No XML file available yet.",
-                       notes="_Survex file found in loser repo but no description in expoweb")
+    #                   official_name = "",
+                        underground_description = "Pending cave write-up - creating as empty object. No XML file available yet.",
+                        survex_file = "caves-1623/" + k + "/" + k +".svx",
+                        url = url,
+                        notes="_Survex file found in loser repo but no description in expoweb")
                 if cave:
                     cave.save() # must save to have id before foreign keys work. This is also a ManyToMany key.
                     #print(f' ! - READ CAVES:  cave {k} {cave}')
                     cave.area.add(area_1623[0])
                     cave.save()
                     message = " ! {:11s} {}".format(cave.unofficial_number, cave.underground_description)
-                    DataIssue.objects.create(parser='caves', message=message)
+                    DataIssue.objects.create(parser='caves', message=message, url=url)
                     print(message)
                     
                     try: # Now create a cave slug ID
                         cs = CaveSlug.objects.update_or_create(cave = cave,
-                                  slug = "TEMP-" + k,
+                                  slug = "1623-PENDING-" + k,
                                   primary = False)
                     except:
                         message = " ! {:11s} {} PENDING cave slug create failure".format(k)
@@ -316,6 +319,8 @@ def readcave(filename):
                     message = f' ! description filename does not exist :{EXPOWEB}:"{description_file[0]}" in "{filename}"'
                     DataIssue.objects.create(parser='caves', message=message, url=f'/cave/{slug}/edit/')
                     print(message)
+                    #c.description_file="" # done only once, to clear out cruft.
+                    #c.save()
 
 def getXML(text, itemname, minItems = 1, maxItems = None, printwarnings = True, context = ""):
     items = re.findall("<%(itemname)s>(.*?)</%(itemname)s>" % {"itemname": itemname}, text, re.S)
diff --git a/templates/dataissues.html b/templates/dataissues.html
index 46538a0..d42821d 100644
--- a/templates/dataissues.html
+++ b/templates/dataissues.html
@@ -6,7 +6,7 @@
 <h1>Loading data from files: Issues arising that need attention</h1>
 
 <p>
-This is work in progress (April 2021).The URL links to the offending objects are not enabled yet.
+This is work in progress (April 2021).The URL links to the offending objects are enabled on only some types of fault as yet.
 <style>
 tr {    text-align:center;     
         font-family: Tahoma,'Trebuchet MS','Lucida Grande',Verdana, Arial, Helvetica, Sans-Serif;
@@ -19,8 +19,8 @@ td {    background : lightblue;     }
 <table>
 <tr><th>Parser</th><th>Issue</th></tr>
 {% for di  in didict %}
-    {% ifchanged di.parser %}
-        <tr {% cycle 'LightGoldenRodYellow' 'lightcyan' as mycolor %}>
+    {% ifchanged di.parser %}<!-- this combination of ifchnaged + cycle is a Django template idiom -->
+        <tr {% cycle 'LightGoldenRodYellow' 'palegreen' 'lightcyan'  'gainsboro' 'paleturquoise' as mycolor %}>
     {% else %}
         <tr>
     {% endifchanged %}
diff --git a/templates/svxfile.html b/templates/svxfile.html
index 72e4446..707c5fd 100644
--- a/templates/svxfile.html
+++ b/templates/svxfile.html
@@ -38,7 +38,11 @@ $(document).ready(function()
 {% endblock %}
 
 {% block content %}
+{% if warning %}
+<h1 style="color: red">Survex Template - will save as file '{{ title }}.svx'</h1>
+{% else %}
 <h1>Survex File: {{ title }}</h1>
+{% endif %}
 
 {% if svxincludes %}
     <p><b>Included files:</b>