2023-01-29 16:23:58 +00:00
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
import operator
|
|
|
|
import re
|
|
|
|
from functools import reduce
|
|
|
|
from pathlib import Path
|
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
|
|
|
from django.urls import reverse
|
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
# from troggle.core.models.survex import SurvexBlock
|
2023-01-29 16:23:58 +00:00
|
|
|
# from troggle.core.models.troggle import DataIssue # circular import. Hmm
|
|
|
|
|
2023-02-01 19:10:46 +00:00
|
|
|
YEAR_RANGE = (1975, 2050)
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
class Wallet(models.Model):
|
2023-01-30 19:04:36 +00:00
|
|
|
"""We do not keep the JSON values in the database, we query them afresh each time,
|
2023-01-29 16:23:58 +00:00
|
|
|
but we will change this when we need to do a Django query on e.g. personame
|
2023-01-30 19:04:36 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
fpath = models.CharField(max_length=200)
|
|
|
|
walletname = models.CharField(max_length=200)
|
|
|
|
walletdate = models.DateField(blank=True, null=True)
|
|
|
|
walletyear = models.DateField(blank=True, null=True)
|
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
class Meta:
|
2023-01-30 19:04:36 +00:00
|
|
|
ordering = ("walletname",)
|
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
def get_absolute_url(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
return urljoin(settings.URL_ROOT, reverse("singlewallet", kwargs={"path": re.sub("#", "%23", self.walletname)}))
|
2023-01-29 16:23:58 +00:00
|
|
|
|
|
|
|
def get_json(self):
|
2023-02-03 22:19:51 +00:00
|
|
|
"""Read the JSON file for the wallet and do stuff
|
|
|
|
Do it every time it is queried, to be sure the result is fresh"""
|
2023-01-30 19:04:36 +00:00
|
|
|
# jsonfile = Path(self.fpath, 'contents.json')
|
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
# Get from git repo instead
|
|
|
|
# :drawings: walletjson/2022/2022#01/contents.json
|
|
|
|
# fpath = /mnt/d/EXPO/expofiles/surveyscans/1999/1999#02
|
|
|
|
fp = Path(self.fpath)
|
|
|
|
wname = fp.name
|
|
|
|
wyear = fp.parent.name
|
2023-01-31 17:13:41 +00:00
|
|
|
wurl = f"/walletedit/{self.walletname}" # .replace('#', ':')
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-03-18 20:32:35 +00:00
|
|
|
if len(wyear) != 4 or len(wname) !=6:
|
|
|
|
# no contents.json for old-style wallets
|
2023-03-21 18:23:07 +00:00
|
|
|
# but this ruined all the tick-list displays.. why?!
|
|
|
|
# return None
|
|
|
|
pass
|
2023-03-18 20:32:35 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
jsonfile = Path(settings.DRAWINGS_DATA, "walletjson") / wyear / wname / "contents.json"
|
|
|
|
if not Path(jsonfile).is_file():
|
2023-03-18 20:32:35 +00:00
|
|
|
print(f'{jsonfile} is not a file {wyear=} {wname=} ')
|
2023-01-29 16:23:58 +00:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
with open(jsonfile) as json_f:
|
|
|
|
try:
|
|
|
|
waldata = json.load(json_f)
|
|
|
|
except:
|
|
|
|
message = f"! {str(self.walletname)} Failed to load {jsonfile} JSON file"
|
2023-01-30 19:04:36 +00:00
|
|
|
# print(message)
|
2023-01-29 16:23:58 +00:00
|
|
|
raise
|
|
|
|
if waldata["date"]:
|
2023-01-30 19:04:36 +00:00
|
|
|
datestr = waldata["date"].replace(".", "-")
|
2023-01-29 16:23:58 +00:00
|
|
|
try:
|
|
|
|
thisdate = datetime.date.fromisoformat(datestr)
|
|
|
|
except ValueError:
|
|
|
|
# probably a single digit day number. HACKUS MAXIMUS.
|
|
|
|
# clearly we need to fix this when we first import date strings..
|
2023-01-30 19:04:36 +00:00
|
|
|
datestr = datestr[:-1] + "0" + datestr[-1]
|
|
|
|
print(f" - {datestr=} ")
|
2023-01-29 16:23:58 +00:00
|
|
|
try:
|
|
|
|
thisdate = datetime.date.fromisoformat(datestr)
|
2023-01-30 19:04:36 +00:00
|
|
|
self.walletdate = thisdate
|
2023-01-29 16:23:58 +00:00
|
|
|
self.save()
|
|
|
|
try:
|
|
|
|
waldata["date"] = thisdate.isoformat()
|
|
|
|
except:
|
|
|
|
message = f"! {str(self.walletname)} Date formatting failure {thisdate}. Failed to load from {jsonfile} JSON file"
|
2023-01-30 19:04:36 +00:00
|
|
|
from troggle.core.models.troggle import DataIssue
|
|
|
|
|
|
|
|
DataIssue.objects.update_or_create(parser="scans", message=message, url=wurl)
|
2023-01-29 16:23:58 +00:00
|
|
|
except:
|
|
|
|
message = f"! {str(self.walletname)} Date format not ISO {datestr}. Failed to load from {jsonfile} JSON file"
|
|
|
|
from troggle.core.models.troggle import DataIssue
|
2023-01-30 19:04:36 +00:00
|
|
|
|
|
|
|
DataIssue.objects.update_or_create(parser="scans", message=message, url=wurl)
|
2023-01-29 16:23:58 +00:00
|
|
|
return waldata
|
2023-01-30 19:04:36 +00:00
|
|
|
|
|
|
|
def year(self):
|
|
|
|
"""This gets the year syntactically without opening and reading the JSON"""
|
2023-01-29 16:23:58 +00:00
|
|
|
if len(self.walletname) < 5:
|
2023-01-30 19:04:36 +00:00
|
|
|
return None
|
2023-01-29 16:23:58 +00:00
|
|
|
if self.walletname[4] != "#":
|
2023-01-30 19:04:36 +00:00
|
|
|
return None
|
2023-01-29 16:23:58 +00:00
|
|
|
year = int(self.walletname[0:4])
|
2023-02-01 19:10:46 +00:00
|
|
|
ymin, ymax = YEAR_RANGE
|
|
|
|
if year < ymin or year > ymax:
|
2023-01-30 19:04:36 +00:00
|
|
|
return None
|
2023-01-29 16:23:58 +00:00
|
|
|
else:
|
2023-01-30 19:04:36 +00:00
|
|
|
self.walletyear = datetime.date(year, 1, 1)
|
2023-01-29 16:23:58 +00:00
|
|
|
self.save()
|
|
|
|
return str(year)
|
|
|
|
|
|
|
|
# Yes this is horribly, horribly inefficient, esp. for a page that have date, people and cave in it
|
|
|
|
def date(self):
|
2023-02-01 23:43:05 +00:00
|
|
|
"""Reads all the JSON data just to get the JSON date."""
|
2023-01-29 16:23:58 +00:00
|
|
|
if self.walletdate:
|
|
|
|
return self.walletdate
|
2023-02-03 22:19:51 +00:00
|
|
|
if not (jsondata := self.get_json()): # WALRUS
|
2023-01-29 16:23:58 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
datestr = jsondata["date"]
|
|
|
|
if not datestr:
|
|
|
|
return None
|
|
|
|
else:
|
2023-01-30 19:04:36 +00:00
|
|
|
datestr = datestr.replace(".", "-")
|
2023-01-29 16:23:58 +00:00
|
|
|
try:
|
|
|
|
samedate = datetime.date.fromisoformat(datestr)
|
|
|
|
self.walletdate = samedate.isoformat()
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
samedate = datetime.date.fromisoformat(datestr[:10])
|
|
|
|
self.walletdate = samedate.isoformat()
|
|
|
|
except:
|
|
|
|
samedate = None
|
|
|
|
self.save()
|
|
|
|
return self.walletdate
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
def people(self):
|
|
|
|
if not self.get_json():
|
|
|
|
return None
|
|
|
|
jsondata = self.get_json()
|
|
|
|
return jsondata["people"]
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
def cave(self):
|
|
|
|
if not self.get_json():
|
|
|
|
return None
|
|
|
|
jsondata = self.get_json()
|
|
|
|
return jsondata["cave"]
|
|
|
|
|
|
|
|
def name(self):
|
|
|
|
if not self.get_json():
|
|
|
|
return None
|
|
|
|
jsondata = self.get_json()
|
|
|
|
return jsondata["name"]
|
|
|
|
|
|
|
|
def get_fnames(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
'''Filenames without the suffix, i.e. without the ".jpg"'''
|
|
|
|
dirpath = Path(settings.SCANS_ROOT, self.fpath) # does nowt as fpath is a rooted path already
|
2023-01-29 16:23:58 +00:00
|
|
|
files = []
|
|
|
|
if not self.fpath:
|
|
|
|
files.append(f"Incorrect path to wallet contents: '{self.fpath}'")
|
|
|
|
return files
|
|
|
|
if not dirpath.is_dir():
|
|
|
|
files.append(f"Incorrect path to wallet contents: '{self.fpath}'")
|
|
|
|
return files
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
for f in dirpath.iterdir():
|
|
|
|
if f.is_file():
|
|
|
|
files.append(Path(f.name).stem)
|
|
|
|
else:
|
|
|
|
files.append(f"-{Path(f.name).stem}-")
|
|
|
|
except FileNotFoundError:
|
|
|
|
files.append("FileNotFoundError")
|
|
|
|
pass
|
|
|
|
return files
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
def fixsurvextick(self, tick):
|
2023-01-29 20:59:56 +00:00
|
|
|
blocks = self.survexblock_set.all()
|
2023-01-30 19:04:36 +00:00
|
|
|
# blocks = SurvexBlock.objects.filter(scanswallet = self)
|
2023-01-29 16:23:58 +00:00
|
|
|
result = tick
|
2023-01-30 19:04:36 +00:00
|
|
|
for b in blocks:
|
|
|
|
if b.survexfile: # if any exist in db, no check for validity or a real file. Refactor.
|
|
|
|
result = "seagreen" # slightly different shade of green
|
2023-01-29 16:23:58 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def get_ticks(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
"""Reads all the JSON data and sets the colour of the completion tick for each condition"""
|
2023-01-29 16:23:58 +00:00
|
|
|
ticks = {}
|
|
|
|
waldata = self.get_json()
|
|
|
|
if not waldata:
|
2023-02-02 17:39:56 +00:00
|
|
|
ticks["S"] = "darkgrey"
|
|
|
|
ticks["C"] = "darkgrey"
|
|
|
|
ticks["Q"] = "darkgrey"
|
|
|
|
ticks["N"] = "darkgrey"
|
|
|
|
ticks["P"] = "darkgrey"
|
|
|
|
ticks["E"] = "darkgrey"
|
|
|
|
ticks["T"] = "darkgrey"
|
|
|
|
ticks["W"] = "darkgrey"
|
2023-01-29 16:23:58 +00:00
|
|
|
return ticks
|
|
|
|
ticks = {}
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
# Initially, are there any required survex files present ?
|
|
|
|
# Note that we can't set the survexblock here on the wallet as that info is only available while parsing the survex file
|
|
|
|
survexok = "red"
|
|
|
|
ticks["S"] = "red"
|
|
|
|
if waldata["survex not required"]:
|
|
|
|
survexok = "green"
|
|
|
|
ticks["S"] = "green"
|
|
|
|
else:
|
|
|
|
if waldata["survex file"]:
|
2023-01-30 19:04:36 +00:00
|
|
|
if not type(waldata["survex file"]) == list: # a string also is a sequence type, so do it this way
|
2023-01-29 16:23:58 +00:00
|
|
|
waldata["survex file"] = [waldata["survex file"]]
|
|
|
|
ngood = 0
|
|
|
|
nbad = 0
|
|
|
|
ticks["S"] = "purple"
|
|
|
|
for sx in waldata["survex file"]:
|
2023-01-30 19:04:36 +00:00
|
|
|
# this logic appears in several places, inc uploads.py). Refactor.
|
|
|
|
if sx != "":
|
2023-01-29 16:23:58 +00:00
|
|
|
if Path(sx).suffix.lower() != ".svx":
|
|
|
|
sx = sx + ".svx"
|
|
|
|
if (Path(settings.SURVEX_DATA) / sx).is_file():
|
|
|
|
ngood += 1
|
|
|
|
else:
|
|
|
|
nbad += 1
|
2023-02-02 17:39:56 +00:00
|
|
|
if nbad == 0 and ngood >= 1: # all valid
|
2023-01-29 16:23:58 +00:00
|
|
|
ticks["S"] = "green"
|
2023-02-02 17:39:56 +00:00
|
|
|
elif nbad >= 1 and ngood >= 1: # some valid, some invalid
|
2023-01-29 16:23:58 +00:00
|
|
|
ticks["S"] = "orange"
|
2023-02-02 17:39:56 +00:00
|
|
|
elif nbad >= 1 and ngood == 0: # all bad
|
|
|
|
ticks["S"] = "red"
|
|
|
|
elif nbad == 0 and ngood == 0: # list of blank strings
|
2023-01-29 16:23:58 +00:00
|
|
|
ticks["S"] = "red"
|
|
|
|
else:
|
2023-02-02 17:39:56 +00:00
|
|
|
ticks["S"] = "fuchsia" # have fun working out what this means
|
2023-01-30 19:04:36 +00:00
|
|
|
|
|
|
|
# Cave Description
|
|
|
|
if waldata["description written"]:
|
2023-01-29 16:23:58 +00:00
|
|
|
ticks["C"] = "green"
|
|
|
|
else:
|
|
|
|
ticks["C"] = survexok
|
|
|
|
# QMs
|
|
|
|
if waldata["qms written"]:
|
|
|
|
ticks["Q"] = "green"
|
|
|
|
else:
|
|
|
|
ticks["Q"] = survexok
|
|
|
|
if not self.year():
|
|
|
|
ticks["Q"] = "darkgrey"
|
|
|
|
else:
|
2023-01-30 19:04:36 +00:00
|
|
|
if int(self.year()) < 2015:
|
2023-01-29 16:23:58 +00:00
|
|
|
ticks["Q"] = "lightgrey"
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-02-02 17:39:56 +00:00
|
|
|
if 'notes not required' not in waldata:
|
|
|
|
waldata['notes not required'] = False
|
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
# Notes, Plan, Elevation; Tunnel
|
|
|
|
if waldata["electronic survey"]:
|
|
|
|
ticks["N"] = "green"
|
|
|
|
ticks["P"] = "green"
|
|
|
|
ticks["E"] = "green"
|
|
|
|
ticks["T"] = "green"
|
|
|
|
else:
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
files = self.get_fnames()
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
# Notes required
|
|
|
|
notes_scanned = reduce(operator.or_, [f.startswith("note") for f in files], False)
|
|
|
|
notes_scanned = reduce(operator.or_, [f.endswith("notes") for f in files], notes_scanned)
|
2023-02-02 17:39:56 +00:00
|
|
|
notes_required = not (notes_scanned or waldata["notes not required"])
|
|
|
|
if notes_required:
|
2023-01-29 16:23:58 +00:00
|
|
|
ticks["N"] = "red"
|
2023-02-02 17:39:56 +00:00
|
|
|
else:
|
|
|
|
ticks["N"] = "green"
|
2023-02-03 22:19:51 +00:00
|
|
|
# print(f"{self.walletname} {ticks['N'].upper()} {notes_scanned=} {notes_required=} {waldata['notes not required']=}")
|
2023-02-02 17:39:56 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
# Plan drawing required
|
|
|
|
plan_scanned = reduce(operator.or_, [f.startswith("plan") for f in files], False)
|
|
|
|
plan_scanned = reduce(operator.or_, [f.endswith("plan") for f in files], plan_scanned)
|
|
|
|
plan_drawing_required = not (plan_scanned or waldata["plan drawn"] or waldata["plan not required"])
|
|
|
|
if plan_drawing_required:
|
|
|
|
ticks["P"] = "red"
|
|
|
|
else:
|
|
|
|
ticks["P"] = "green"
|
|
|
|
|
|
|
|
# Elev drawing required
|
|
|
|
elev_scanned = reduce(operator.or_, [f.startswith("elev") for f in files], False)
|
|
|
|
elev_scanned = reduce(operator.or_, [f.endswith("elev") for f in files], elev_scanned)
|
|
|
|
elev_scanned = reduce(operator.or_, [f.endswith("elevation") for f in files], elev_scanned)
|
|
|
|
elev_drawing_required = not (elev_scanned or waldata["elev drawn"] or waldata["elev not required"])
|
|
|
|
if elev_drawing_required:
|
|
|
|
ticks["E"] = "red"
|
|
|
|
else:
|
|
|
|
ticks["E"] = "green"
|
|
|
|
|
|
|
|
# Tunnel / Therion
|
|
|
|
if elev_drawing_required or plan_drawing_required:
|
|
|
|
ticks["T"] = "red"
|
|
|
|
else:
|
|
|
|
ticks["T"] = "green"
|
|
|
|
|
|
|
|
# Website
|
|
|
|
if waldata["website updated"]:
|
|
|
|
ticks["W"] = "green"
|
|
|
|
else:
|
|
|
|
ticks["W"] = "red"
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
return ticks
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-01-29 16:23:58 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "[" + str(self.walletname) + " (Wallet)]"
|