From 030c49ff7caf4cbd36dc878bdf0ff1ec142ec5be Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@klebos.com>
Date: Thu, 25 Jun 2020 03:17:56 +0100
Subject: [PATCH] rewritten QM loading from SVX files

---
 parsers/survex.py | 77 +++++++++++++++++------------------------------
 1 file changed, 28 insertions(+), 49 deletions(-)

diff --git a/parsers/survex.py b/parsers/survex.py
index 923bb0e..a13f7b2 100644
--- a/parsers/survex.py
+++ b/parsers/survex.py
@@ -235,62 +235,41 @@ class LoadSurvex():
         insp = self.insp
         qm_no = qmline.group(1)
         qm_grade = qmline.group(2)
-        qm_from_block = qmline.group(3) # usual closest survey station
-        qm_from_station = qmline.group(4)
-        qm_resolve_section = qmline.group(6)
-        qm_resolve_station = qmline.group(7)
+        if qmline.group(3):  # usual closest survey station
+            qm_nearest = qmline.group(3) 
+            if qmline.group(4):
+                qm_nearest = qm_nearest +"."+ qmline.group(4)
+                
+        if qmline.group(6) and qmline.group(6) != '-':
+            qm_resolve_station = qmline.group(6)
+            if qmline.group(7):
+                qm_resolve_station = qm_resolve_station +"."+ qmline.group(7) 
+        else:
+            qm_resolve_station = ""
         qm_notes = qmline.group(8)
-        # This whole section should be moved if we can have *QM become a proper survex command
-        # Spec of QM in SVX files, currently commented out need to add to survex
-        # needs to match self.rx_qm
+        # Spec of QM in SVX files:
         # ;Serial number   grade(A/B/C/D/X)  nearest-station  resolution-station description
         # ;QM1	a	hobnob_hallway_2.42	hobnob-hallway_3.42	junction of keyhole passage
         # ;QM1	a	hobnob_hallway_2.42	-	junction of keyhole passage
 
-        # print(insp+'Cave - %s' % survexfile.cave)
-        # print(insp+'QM no %d' % int(qm_no))
-        # print(insp+'QM grade %s' % qm_grade)
-        # print(insp+'QM section %s' % qm_from_block)
-        # print(insp+'QM station %s' % qm_from_station)
-        # print(insp+'QM res section %s' % qm_resolve_section)
-        # print(insp+'QM res station %s' % qm_resolve_station)
-        # print(insp+'QM notes %s' % qm_notes)
-
-        # If the QM isn't resolved (has a resolving station) then load it.
-        # NB none of the SurveyStations are in the DB now, so searching always fails
-        if not qm_resolve_section or qm_resolve_section != '-' or qm_resolve_section != 'None':
-            from_block = models_survex.SurvexBlock.objects.filter(name=qm_from_block)
-            # If we can find a section (survex note chunk, named)
-            if len(from_block) > 0:
-                from_station = models_survex.SurvexStation.objects.filter(block=from_block[0], name=qm_from_station)
-                # If we can find a from station then we have the nearest station and can import it
-                # but now that we don't import all the stations this will fail
-                if len(from_station) > 0:
-                    qm = models_caves.QM.objects.create(number=qm_no,
-                                                  nearest_station=from_station[0],
-                                                  grade=qm_grade.upper(),
-                                                  location_description=qm_notes)
-                    message = " - QM{} in '{}' loaded into DB using '{}'".format(qm_no, survexblock.survexfile.path,str(from_station[0]))
-                    print(insp+message)
-                    models.DataIssue.objects.create(parser='survex', message=message)
-                else:
-                    try:
-                        qm = models_caves.QM.objects.create(number=qm_no,
-                              nearest_station=qm_from_block,
-                              grade=qm_grade.upper(),
-                              location_description=qm_notes)
-                        message = " ! QM{} in '{}' CREATED in DB at '{}'".format(qm_no, survexblock.survexfile.path, qm_from_block)
-                        print(insp+message)
-                        models.DataIssue.objects.create(parser='survex', message=message)
-                    except:
-                        message = " ! QM{} in '{}' failed to create in DB at '{}'".format(qm_no, survexblock.survexfile.path, qm_from_block)
-                        print(insp+message)
-                        models.DataIssue.objects.create(parser='survex', message=message)
-        else:
-            message = " - QM{} in '{}' resolved as '{}'".format(qm_no, survexblock.survexfile.path,qm_resolve_section)
+        # NB none of the SurveyStations are in the DB now, so if we want to link to aSurvexStation
+        # we would have to create one. But that is not obligatory and no QMs loaded from CSVs have one
+        try:
+            qm = models_caves.QM.objects.create(number=qm_no,
+                                            # nearest_station=a_survex_station_object, # can be null
+                                              nearest_station_description=qm_resolve_station,
+                                              nearest_station_name=qm_nearest,
+                                              grade=qm_grade.upper(),
+                                              location_description=qm_notes)
+            qm.save
+            # message = " ! QM{} '{}' CREATED in DB in '{}'".format(qm_no, qm_nearest,survexblock.survexfile.path)
+            # print(insp+message)
+            # models.DataIssue.objects.create(parser='survex', message=message)
+        except:
+            message = " ! QM{} FAIL to create {} in'{}'".format(qm_no, qm_nearest,survexblock.survexfile.path)
             print(insp+message)
             models.DataIssue.objects.create(parser='survex', message=message)
-            pass
+
 
     def LoadSurvexComment(self, survexblock, comment):
         # ignore all comments except ;ref and ;QM