From 8530b0643d552dbaaeef42c8598afd9402338793 Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@klebos.com>
Date: Mon, 6 Jul 2020 01:24:43 +0100
Subject: [PATCH] person attribution of surveyed length working

---
 core/models.py                  | 12 +++++++++---
 core/models_survex.py           |  3 ---
 core/views_logbooks.py          | 14 ++++++++------
 core/views_survex.py            |  4 ++--
 parsers/people.py               |  4 +---
 parsers/survex.py               | 29 +++++++++++++++++++++--------
 templates/personexpedition.html | 29 +++++++++++++++++++++++------
 templates/personindex.html      | 10 +++++-----
 templates/svxcavesingle.html    |  2 +-
 9 files changed, 70 insertions(+), 37 deletions(-)

diff --git a/core/models.py b/core/models.py
index 7ccef54..9900dd4 100644
--- a/core/models.py
+++ b/core/models.py
@@ -157,10 +157,15 @@ class Person(TroggleModel):
 
         for personexpedition in self.personexpedition_set.all():
              if not personexpedition.is_guest:
-                print((personexpedition.expedition.year))
                 notability += Decimal(1) / (max_expo_val - int(personexpedition.expedition.year))
         return notability
 
+    def legslength(self):
+        for personexpedition in self.personexpedition_set.all():
+             if not personexpedition.is_guest:
+                length += personexpedition.legslength
+        return length
+
     def bisnotable(self):
         return self.notability() > Decimal(1)/Decimal(3)
     
@@ -178,7 +183,8 @@ class PersonExpedition(TroggleModel):
     expedition  = models.ForeignKey(Expedition,on_delete=models.CASCADE)
     person      = models.ForeignKey(Person,on_delete=models.CASCADE)
     slugfield   = models.SlugField(max_length=50,blank=True, null=True)
-    
+    legslength  = models.FloatField(null=True)
+
     is_guest    = models.BooleanField(default=False)  
     COMMITTEE_CHOICES = (
         ('leader','Expo leader'),
@@ -218,7 +224,7 @@ class PersonExpedition(TroggleModel):
         return urljoin(settings.URL_ROOT, reverse('personexpedition',kwargs={'first_name':self.person.first_name,'last_name':self.person.last_name,'year':self.expedition.year}))
 	
     def surveyedleglength(self):
-        survexblocks = [personrole.survexblock  for personrole in self.personrole_set.all() ]
+        survexblocks = [personrole.survexblock  for personrole in self.survexpersonrole_set.all() ]
         return sum([survexblock.legslength  for survexblock in set(survexblocks)])
     
     # would prefer to return actual person trips so we could link to first and last ones
diff --git a/core/models_survex.py b/core/models_survex.py
index 375144c..6751165 100644
--- a/core/models_survex.py
+++ b/core/models_survex.py
@@ -125,9 +125,6 @@ class SurvexBlock(models.Model):
             # str(self.cave) + "]" 
     def __str__(self):
         return self.name and str(self.name) or 'no name'
-    
-
-
 
     def isSurvexBlock(self): # Function used in templates
         return True
diff --git a/core/views_logbooks.py b/core/views_logbooks.py
index 1f1e2d1..31b046e 100644
--- a/core/views_logbooks.py
+++ b/core/views_logbooks.py
@@ -108,6 +108,9 @@ def person(request, first_name='', last_name='', ):
 
 
 def GetPersonChronology(personexpedition):
+    '''Horrible bug here whern ther eis more than one survex block per day, it duplicates the entry but gets it wrong
+    Fortunately this is just the display on this page which is wroing, no bad calculations get into the database.
+    '''
     res = { }
     for persontrip in personexpedition.persontrip_set.all():
         a = res.setdefault(persontrip.logbook_entry.date, { })
@@ -119,14 +122,13 @@ def GetPersonChronology(personexpedition):
     
     # build up the tables
     rdates = sorted(list(res.keys()))
-    
-    
+
     res2 = [ ]
     for rdate in rdates:
-        persontrips = res[rdate].get("persontrips", [])
-        personroles = res[rdate].get("personroles", [])
-        for n in range(max(len(persontrips), len(personroles))):
-            res2.append(((n == 0 and rdate or "--"), (n < len(persontrips) and persontrips[n]), (n < len(personroles) and personroles[n])))
+        persontrips   = res[rdate].get("persontrips", [])
+        personroles   = res[rdate].get("personroles", [])
+        for n in range(max(len(persontrips), len(personroles) )):
+            res2.append(((n == 0 and rdate or "--"), (n < len(persontrips) and persontrips[n]), (n < len(personroles) and personroles[n]) ))
             
     return res2
 
diff --git a/core/views_survex.py b/core/views_survex.py
index 2a199d3..76e79dc 100644
--- a/core/views_survex.py
+++ b/core/views_survex.py
@@ -348,10 +348,10 @@ def survexcaveslist(request):
 # parsing all the survex files of a single cave and showing that it's consistent and can find all the files and people
 # doesn't use recursion.  just writes it twice
 
-# currently produces title and blank page.. link test is "dates and explorers"
+# currently not showing Explorers or Titles. link test from SurvexFile page is "dates and explorers"
 def survexcavesingle(request, survex_cave):
     breload = False
     cave = Cave.objects.get(kataster_number=survex_cave)
     if breload:
-        parsers.survex.ReloadSurvexCave(survex_cave)
+        parsers.survex.ReloadSurvexCave(survex_cave) # does not exit now, needs re-writing to work.
     return render_to_response('svxcavesingle.html', {'settings': settings, "cave":cave })
diff --git a/parsers/people.py b/parsers/people.py
index cadeb5c..d0fa242 100644
--- a/parsers/people.py
+++ b/parsers/people.py
@@ -1,5 +1,3 @@
-#.-*- coding: utf-8 -*-
-
 from django.conf import settings
 import troggle.core.models as models
 import csv, re, datetime, os, shutil
@@ -109,7 +107,7 @@ def LoadPersonsExpos():
             expedition = models.Expedition.objects.get(year=year)
             if attended == "1" or attended == "-1":
                 lookupAttribs = {'person':person, 'expedition':expedition}
-                nonLookupAttribs = {'nickname':nickname, 'is_guest':(personline[header["Guest"]] == "1")}
+                nonLookupAttribs = {'nickname':nickname, 'legslength':0.0,'is_guest':(personline[header["Guest"]] == "1")}
                 save_carefully(models.PersonExpedition, lookupAttribs, nonLookupAttribs)
 
 
diff --git a/parsers/survex.py b/parsers/survex.py
index 460fc3f..1cf223b 100644
--- a/parsers/survex.py
+++ b/parsers/survex.py
@@ -36,7 +36,7 @@ class LoadingSurvex():
     A 'scansfolder' is what we today call a "survey scans folder" or a "wallet".
     """
 
-    rx_flags   = re.compile(r"not\s")
+    rx_flagsnot= re.compile(r"not\s")
     rx_linelen = re.compile(r"[\d\-+.]+$")
     rx_team    = re.compile(r"(?i)(Insts|Notes|Tape|Dog|Useless|Pics|Helper|Disto|Consultant)\s+(.*)$")
     rx_person  = re.compile(r"(?i) and | / |, | & | \+ |^both$|^none$")
@@ -76,6 +76,7 @@ class LoadingSurvex():
     depthinclude = 0
     legsnumberstack = []
     slengthstack = []
+    personexpedstack = []
     stackbegin =[]
     flagsstack =[]
     datastack =[]
@@ -95,6 +96,7 @@ class LoadingSurvex():
     currentsurvexfile = None
     currentcave = None
     caverndate = None
+    currentpersonexped = []
 
     def __init__(self):
         self.caveslist = GetCaveLookup()
@@ -130,6 +132,7 @@ class LoadingSurvex():
                         personrole.expeditionday = survexblock.expeditionday
                         if personexpedition:
                             personrole.person=personexpedition.person
+                            self.currentpersonexped.append(personexpedition)
                         personrole.save()
 
     def LoadSurvexEntrance(self, survexblock, line):
@@ -200,10 +203,10 @@ class LoadingSurvex():
         ls = sline.lower().split()
 
         # skip all splay legs
-        if ls[datastar["from"]] == "..":
+        if ls[datastar["from"]] == ".." or ls[datastar["from"]] == ".":
             #print("Splay in ", survexblock.survexfile.path)
             return
-        if ls[datastar["to"]] == "..":
+        if ls[datastar["to"]] == ".." or ls[datastar["to"]] == ".":
             #print("Splay in ", survexblock.survexfile.path)
             return
         if self.flagsstar["splayalias"]:
@@ -441,10 +444,11 @@ class LoadingSurvex():
         self.flagsstar = copy.deepcopy(self.flagsdefault)
         flags = []
         
-        args = self.rx_flags.sub("not",args)
+        args = self.rx_flagsnot.sub("not",args)
         argslist = args.split()
         for s in argslist:
             flags.append(s)
+        print(" # flagslist:{}".format(flags),)
         
         if "duplicate" in flags:
             self.flagsstar["duplicate"] = True
@@ -460,7 +464,6 @@ class LoadingSurvex():
         if "notsplay" in flags:
             self.flagsstar["splay"] = False
 
-
         # if self.flagsstar["duplicate"] == True or self.flagsstar["surface"] == True or self.flagsstar["splay"] == True:
         # actually we do want to count duplicates as this is for "effort expended in surveying underground"
         if self.flagsstar["surface"] == True or self.flagsstar["splay"] == True:
@@ -664,6 +667,10 @@ class LoadingSurvex():
                 print("    ", file=sys.stderr,end='')
             sys.stderr.flush()
 
+        def addpersonlengths():
+            for personexpedition in self.currentpersonexped:
+                personexpedition.legslength += self.slength
+
         def printbegin():
             nonlocal blkid
             nonlocal pathlist
@@ -700,6 +707,7 @@ class LoadingSurvex():
                 # print("'{}'   ".format(dict["type"].upper()), end="")
             # print("")
             # print("'{}' self.datastar  ".format(self.datastar["type"].upper()))
+            
             # ------------ * FLAGS
             self.flagsstack.append(copy.deepcopy(self.flagsstar))
             # ------------ * FLAGS
@@ -722,11 +730,12 @@ class LoadingSurvex():
                 # print("'{}'   ".format(dict["type"].upper()), end="")
             # print("")
             # print("'{}' self.datastar  ".format(self.datastar["type"].upper()))
+            
             # ------------ * FLAGS
             self.flagsstar = copy.deepcopy(self.flagsstack.pop()) 
             # ------------ * FLAGS
-            # if oldflags["skiplegs"] != self.flagsstar["skiplegs"]:
-            #    print(" # POP  'any' flag now:'{}'  was:{} ".format(self.flagsstar["skiplegs"], oldflags["skiplegs"]))
+            if oldflags["skiplegs"] != self.flagsstar["skiplegs"]:
+                print(" # POP  'any' flag now:'{}'  was:{} ".format(self.flagsstar["skiplegs"], oldflags["skiplegs"]))
 
         def starstatement(star):
             nonlocal survexblock
@@ -747,10 +756,12 @@ class LoadingSurvex():
                 self.stackbegin.append(blkid)
                 self.legsnumberstack.append(self.legsnumber)
                 self.slengthstack.append(self.slength)
+                self.personexpedstack.append(self.currentpersonexped)
                 pushblock()
                 # PUSH state ++++++++++++++
                 self.legsnumber = 0
                 self.slength = 0.0
+                self.currentpersonexped = []
                 printbegin()
                 newsurvexblock = models_survex.SurvexBlock(name=blkid, parent=survexblock, 
                         survexpath=pathlist, 
@@ -766,10 +777,11 @@ class LoadingSurvex():
             elif self.rx_end.match(cmd):
                 survexblock.legsall = self.legsnumber
                 survexblock.legslength = self.slength
+                addpersonlengths()
                 printend()
                 slengthtotal += self.slength
                 nlegstotal += self.legsnumber
-                 
+
                 try:
                     survexblock.parent.save() # django insists on this although it is already saved !?
                 except:
@@ -782,6 +794,7 @@ class LoadingSurvex():
                     raise
                # POP  state ++++++++++++++
                 popblock()
+                self.currentpersonexped = self.personexpedstack.pop()
                 self.legsnumber = self.legsnumberstack.pop()
                 self.slength = self.slengthstack.pop()
                 blkid = self.stackbegin.pop()
diff --git a/templates/personexpedition.html b/templates/personexpedition.html
index 93228d9..4428756 100644
--- a/templates/personexpedition.html
+++ b/templates/personexpedition.html
@@ -12,6 +12,7 @@
 </h1>
 
 <p>{{message}}</p>
+<p>{{personexpedition.surveyedleglength|stringformat:".1f"}} m surveyed this year.</p>
 
 <p><b>Other years:</b>
 {% for otherpersonexpedition in personexpedition.person.personexpedition_set.all %}
@@ -26,32 +27,48 @@
 <h3>Table of all trips and surveys aligned by date</h3>
 <div>
 <table class="survexcontibutions">
-<tr><th>Date</th><th colspan="2">Trips</th><th colspan="2">Surveys</th></tr>
+<tr><th>Date</th><th colspan="2">Trips</th><th colspan="3">Surveys</th></tr>
 {% for persondate in personchronology %}
 <tr>
   <td class="date">{{persondate.0}}</td>
 
   {% if persondate.1 %}
-     <td class="trip"><a href="{{ persondate.1.logbook_entry.get_absolute_url }}">{{persondate.1.logbook_entry.title|safe}}</a></td>
+     <td width="35%" class="trip"><a href="{{ persondate.1.logbook_entry.get_absolute_url }}">{{persondate.1.logbook_entry.title|safe}}</a></td>
      <td><a href="{{ persondate.1.logbook_entry.cave.get_absolute_url }}">{{persondate.1.place|safe}}</a></td>
   {% else %}
      <td colspan="2"> </td>
   {% endif %}
 
   {% if persondate.2 %}
-    <td class="survexblock"><a href="{% url "svx" persondate.2.survexfile.path %}">{{persondate.2}}</a></td>
-    <td class="roles">
+    <td class="survexblock"><a href="{% url "svx" persondate.2.survexfile.path %}">{{persondate.2.name}}</a></td>
+    <td class="roles" style="padding-right: 3px; text-align:right">
     {% for survexpersonrole in persondate.2.survexpersonrole_set.all %}
       {{survexpersonrole.nrole}}
     {% endfor %}
     </td>
+    <td style="text-align:right">
+    {{persondate.2.legslength|stringformat:".1f"}} m
+    </td>
   {% else %}
-    <td colspan="2"> </td>
+    <td colspan="3"> </td>
   {% endif %}
 
 </tr>
 {% endfor %}
 </table>
 </div>
-
+<div style="color: slategray; background-color:lightcyan">
+<p>Horrible bug here but only when there is more than one survex block per day, 
+e.g. see <a href="/personexpedition/Wookey/1999">Wookey 1999</a> where there are 3 eiscream survex blocks on 5th August.
+it duplicates the entry but gets it wrong. The length from the first block is displayed twice but there should be 3 rows: eiscream, eiscream2, eiscream3.
+<p>Fortunately it is <b>just this display on this page which is wrong</b>: no bad calculations get into the database.
+<p>The interaction of django database query idioms with <a href="https://docs.djangoproject.com/en/1.11/ref/templates/api/">django HTML templating language</a> is a bit impenetrable here. <br>
+- The template is in <var>troggle/templates/personexpedition.html</var>
+<br>
+- The code is in function <var>personexpedition()</var> which calls 
+<var>GetPersonChronology()</var> in <var>troggle/core/views_logbooks.py</var>
+<br>
+- the connection between the two is made in the URL resolver in <var>troggle/urls.py</var>
+<p>To be fixed!
+</div>
 {% endblock %}
diff --git a/templates/personindex.html b/templates/personindex.html
index 210fe4a..e21d7e6 100644
--- a/templates/personindex.html
+++ b/templates/personindex.html
@@ -5,19 +5,19 @@
 
 {% block content %}
 
-<h2>Notable expoers</h2>
+<h2>Notably Recent Expoers</h2>
 <table class="searchable">
-<tr><th>Person</th><th>First</th><th>Last</th><th>Notability</th></tr>
+<tr><th>Person</th><th>First</th><th>Last</th><th>Recency</th></tr>
 {% for person in notablepersons|dictsortreversed:"notability" %}
 <tr>
   <td><a href="{{ person.get_absolute_url }}">{{person|wiki_to_html_short}}</a></td>
   <td><a href="{{ person.first.get_absolute_url }}">{{ person.first.expedition.year }}</a></td> 
   <td><a href="{{ person.last.get_absolute_url }}">{{ person.last.expedition.year }}</a></td> 
-  <td>{{person.notability|floatformat:2}}</td>
+  <td style="text-align:right">{{person.notability|floatformat:2}}</td>
 </tr>
 {% endfor %}
 </table>
-<p>This is based purely on attendance, not on activities, surveying or usefulness of any kind. But as Woody Allen said: "90% of success is just turning up". It should really be called "Notably recent expoers" as the metric is just a geometric "recency" (1/2 for attending last year, 1/3 for the year before, etc., added up. Display cuttoff is 1/3.).
+<p>This is based purely on attendance, not on activities, surveying or usefulness of any kind. But as Woody Allen said: "90% of success is just turning up". This is mostly people who have been within the past three years, with an additional bias for number of attendances since 1976. The metric is just a geometric "recency" (1/2 for attending last year, 1/3 for the year before, etc., added up.) The display cuttoff is 1/3 so if you came just once, three years ago, you are on the list.
 
 
 <h2>All expoers</h2>
@@ -33,7 +33,7 @@
   <td><a href="{{ person.get_absolute_url }}">{{person|wiki_to_html_short}}</a></td>
   <td><a href="{{ person.first.get_absolute_url }}">{{person.first.expedition.year}}</a></td> 
   <td><a href="{{ person.last.get_absolute_url }}">{{person.last.expedition.year}}</a></td>
-  <td></td>
+  <td style="text-align:right">{{person.surveyedleglength|stringformat:".0f"}} m </td>
 </tr>
 {% endfor %}
 </table>
diff --git a/templates/svxcavesingle.html b/templates/svxcavesingle.html
index 42f3c1c..ca9ba58 100644
--- a/templates/svxcavesingle.html
+++ b/templates/svxcavesingle.html
@@ -48,7 +48,7 @@
   </td>
 
   <td>
-  {% for personrole in survexblock.personrole_set.all %}
+  {% for personrole in survexblock.survexpersonrole_set.all %}
     {% if personrole.personexpedition %}
       <a href="{{personrole.personexpedition.get_absolute_url}}">{{personrole.personname}}</a>
     {% else %}