2011-07-11 02:10:22 +01:00
|
|
|
import os
|
|
|
|
import re
|
2022-07-27 23:48:22 +01:00
|
|
|
import json
|
|
|
|
from urllib.parse import urljoin
|
|
|
|
from pathlib import Path
|
2020-05-28 01:16:45 +01:00
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.conf import settings
|
2020-06-18 21:50:16 +01:00
|
|
|
from django.urls import reverse
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SurvexDirectory(models.Model):
|
|
|
|
path = models.CharField(max_length=200)
|
2020-06-30 15:26:03 +01:00
|
|
|
cave = models.ForeignKey('Cave', blank=True, null=True,on_delete=models.SET_NULL)
|
|
|
|
primarysurvexfile = models.ForeignKey('SurvexFile', related_name='primarysurvexfile', blank=True, null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
# could also include files in directory but not referenced
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
2021-05-05 00:35:10 +01:00
|
|
|
verbose_name_plural = "Survex directories"
|
2020-05-28 01:16:45 +01:00
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
def __str__(self):
|
2022-07-28 13:15:11 +01:00
|
|
|
return "[SurvexDirectory:"+str(self.path) + " | Primary svx:" + str(self.primarysurvexfile.path) +".svx ]"
|
2020-06-28 01:50:34 +01:00
|
|
|
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class SurvexFile(models.Model):
|
|
|
|
path = models.CharField(max_length=200)
|
2020-06-30 15:26:03 +01:00
|
|
|
survexdirectory = models.ForeignKey("SurvexDirectory", blank=True, null=True,on_delete=models.SET_NULL)
|
|
|
|
cave = models.ForeignKey('Cave', blank=True, null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
2020-06-28 14:42:26 +01:00
|
|
|
|
2020-06-29 21:16:13 +01:00
|
|
|
# Don't change from the default as that breaks troggle webpages and internal referencing!
|
|
|
|
# def __str__(self):
|
|
|
|
# return "[SurvexFile:"+str(self.path) + "-" + str(self.survexdirectory) + "-" + str(self.cave)+"]"
|
2020-06-28 14:42:26 +01:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def exists(self):
|
|
|
|
fname = os.path.join(settings.SURVEX_DATA, self.path + ".svx")
|
|
|
|
return os.path.isfile(fname)
|
|
|
|
|
|
|
|
def OpenFile(self):
|
|
|
|
fname = os.path.join(settings.SURVEX_DATA, self.path + ".svx")
|
|
|
|
return open(fname)
|
|
|
|
|
|
|
|
def SetDirectory(self):
|
|
|
|
dirpath = os.path.split(self.path)[0]
|
2020-06-27 17:55:59 +01:00
|
|
|
# pointless search every time we import a survex file if we know there are no duplicates..
|
|
|
|
# don't use this for initial import.
|
2011-07-11 02:10:22 +01:00
|
|
|
survexdirectorylist = SurvexDirectory.objects.filter(cave=self.cave, path=dirpath)
|
|
|
|
if survexdirectorylist:
|
|
|
|
self.survexdirectory = survexdirectorylist[0]
|
|
|
|
else:
|
|
|
|
survexdirectory = SurvexDirectory(path=dirpath, cave=self.cave, primarysurvexfile=self)
|
|
|
|
survexdirectory.save()
|
|
|
|
self.survexdirectory = survexdirectory
|
|
|
|
self.save()
|
2021-05-05 00:35:10 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.path
|
2020-05-28 01:16:45 +01:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class SurvexStationLookUpManager(models.Manager):
|
|
|
|
def lookup(self, name):
|
|
|
|
blocknames, sep, stationname = name.rpartition(".")
|
|
|
|
return self.get(block = SurvexBlock.objects.lookup(blocknames),
|
2012-06-10 14:59:21 +01:00
|
|
|
name__iexact = stationname)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
class SurvexStation(models.Model):
|
2012-06-10 14:59:21 +01:00
|
|
|
name = models.CharField(max_length=100)
|
2020-06-30 15:26:03 +01:00
|
|
|
block = models.ForeignKey('SurvexBlock', null=True,on_delete=models.SET_NULL)
|
|
|
|
# equate = models.ForeignKey('SurvexEquate', blank=True, null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
objects = SurvexStationLookUpManager()
|
|
|
|
x = models.FloatField(blank=True, null=True)
|
|
|
|
y = models.FloatField(blank=True, null=True)
|
|
|
|
z = models.FloatField(blank=True, null=True)
|
2012-06-10 14:59:21 +01:00
|
|
|
|
|
|
|
def path(self):
|
|
|
|
r = self.name
|
|
|
|
b = self.block
|
|
|
|
while True:
|
|
|
|
if b.name:
|
|
|
|
r = b.name + "." + r
|
|
|
|
if b.parent:
|
|
|
|
b = b.parent
|
|
|
|
else:
|
|
|
|
return r
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2021-04-20 19:47:08 +01:00
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
|
|
|
def __str__(self):
|
|
|
|
return self.name and str(self.name) or 'no name'
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
#
|
|
|
|
# Single SurvexBlock
|
|
|
|
#
|
|
|
|
class SurvexBlockLookUpManager(models.Manager):
|
|
|
|
def lookup(self, name):
|
2012-08-10 18:02:13 +01:00
|
|
|
if name == "":
|
|
|
|
blocknames = []
|
|
|
|
else:
|
|
|
|
blocknames = name.split(".")
|
2019-06-26 03:32:18 +01:00
|
|
|
block = SurvexBlock.objects.get(parent=None, survexfile__path=settings.SURVEX_TOPNAME)
|
2011-07-11 02:10:22 +01:00
|
|
|
for blockname in blocknames:
|
2012-06-10 14:59:21 +01:00
|
|
|
block = SurvexBlock.objects.get(parent=block, name__iexact=blockname)
|
2011-07-11 02:10:22 +01:00
|
|
|
return block
|
|
|
|
|
|
|
|
class SurvexBlock(models.Model):
|
|
|
|
objects = SurvexBlockLookUpManager()
|
|
|
|
name = models.CharField(max_length=100)
|
2020-07-20 23:25:49 +01:00
|
|
|
title = models.CharField(max_length=200)
|
2020-06-30 15:26:03 +01:00
|
|
|
parent = models.ForeignKey('SurvexBlock', blank=True, null=True,on_delete=models.SET_NULL)
|
|
|
|
cave = models.ForeignKey('Cave', blank=True, null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2019-06-26 20:57:24 +01:00
|
|
|
date = models.DateField(blank=True, null=True)
|
2020-06-30 15:26:03 +01:00
|
|
|
expeditionday = models.ForeignKey("ExpeditionDay", null=True,on_delete=models.SET_NULL)
|
|
|
|
expedition = models.ForeignKey('Expedition', blank=True, null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2020-06-30 15:26:03 +01:00
|
|
|
survexfile = models.ForeignKey("SurvexFile", blank=True, null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
survexpath = models.CharField(max_length=200) # the path for the survex stations
|
|
|
|
|
2021-04-26 19:50:03 +01:00
|
|
|
scanswallet = models.ForeignKey("Wallet", null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2020-06-18 00:20:47 +01:00
|
|
|
legsall = models.IntegerField(null=True) # summary data for this block
|
2020-07-04 13:31:46 +01:00
|
|
|
legslength = models.FloatField(null=True)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
def __str__(self):
|
|
|
|
return "[SurvexBlock:"+ str(self.name) + "-path:" + \
|
|
|
|
str(self.survexpath) + "-cave:" + \
|
|
|
|
str(self.cave) + "]"
|
|
|
|
|
2020-06-28 14:42:26 +01:00
|
|
|
def __str__(self):
|
2020-06-29 21:16:13 +01:00
|
|
|
return self.name and str(self.name) or 'no name'
|
2020-06-28 14:42:26 +01:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def isSurvexBlock(self): # Function used in templates
|
|
|
|
return True
|
|
|
|
|
|
|
|
def GetPersonroles(self):
|
2021-05-05 00:35:10 +01:00
|
|
|
'''
|
2021-04-30 00:24:36 +01:00
|
|
|
But apparently never used !?
|
|
|
|
'''
|
2011-07-11 02:10:22 +01:00
|
|
|
res = [ ]
|
2021-03-26 21:20:08 +00:00
|
|
|
for personrole in self.survexpersonrole_set.order_by('personexpedition'):
|
2021-04-30 00:24:36 +01:00
|
|
|
res.append({'person':personrole.personexpedition.person, 'expeditionyear':personrole.personexpedition.expedition.year})
|
2011-07-11 02:10:22 +01:00
|
|
|
return res
|
2020-06-27 12:04:34 +01:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def DayIndex(self):
|
|
|
|
return list(self.expeditionday.survexblock_set.all()).index(self)
|
|
|
|
|
|
|
|
class SurvexPersonRole(models.Model):
|
2020-06-18 00:20:47 +01:00
|
|
|
survexblock = models.ForeignKey('SurvexBlock',on_delete=models.CASCADE)
|
2019-06-26 20:57:24 +01:00
|
|
|
# increasing levels of precision
|
2011-07-11 02:10:22 +01:00
|
|
|
personname = models.CharField(max_length=100)
|
2020-06-30 15:26:03 +01:00
|
|
|
person = models.ForeignKey('Person', blank=True, null=True,on_delete=models.SET_NULL)
|
|
|
|
personexpedition = models.ForeignKey('PersonExpedition', blank=True, null=True,on_delete=models.SET_NULL)
|
|
|
|
persontrip = models.ForeignKey('PersonTrip', blank=True, null=True,on_delete=models.SET_NULL)
|
|
|
|
expeditionday = models.ForeignKey("ExpeditionDay", null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2020-05-26 02:21:36 +01:00
|
|
|
def __str__(self):
|
2021-05-05 00:35:10 +01:00
|
|
|
return str(self.person) + " - " + str(self.survexblock)
|
2020-05-28 01:16:45 +01:00
|
|
|
|
2021-04-26 18:18:16 +01:00
|
|
|
class Wallet(models.Model):
|
2022-07-27 23:48:22 +01:00
|
|
|
'''We do not keep the JSON values in the database, we query them afresh each time,
|
|
|
|
but we will change this when we need to do a Django query on e.g. personame
|
|
|
|
'''
|
2011-07-11 02:10:22 +01:00
|
|
|
fpath = models.CharField(max_length=200)
|
|
|
|
walletname = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('walletname',)
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2021-04-26 19:50:03 +01:00
|
|
|
return urljoin(settings.URL_ROOT, reverse('singlewallet', kwargs={"path":re.sub("#", "%23", self.walletname)}))
|
2020-05-15 21:32:55 +01:00
|
|
|
|
2022-07-27 23:48:22 +01:00
|
|
|
def get_json(self):
|
|
|
|
jsonfile = Path(self.fpath, 'contents.json')
|
|
|
|
if not Path(jsonfile).is_file():
|
|
|
|
print(f'{jsonfile} is not a file')
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
with open(jsonfile) as json_f:
|
|
|
|
try:
|
|
|
|
waldata = json.load(json_f)
|
|
|
|
except:
|
|
|
|
wurl = f"/scanupload/{self.walletname}" # .replace('#', ':')
|
|
|
|
message = f"! {str(self.walletname)} Failed to load {jsonfile} JSON file"
|
|
|
|
print(message)
|
|
|
|
raise
|
|
|
|
|
|
|
|
return waldata
|
2022-07-30 23:02:02 +01:00
|
|
|
|
|
|
|
def year(self):
|
|
|
|
if self.walletname[4] != "#":
|
|
|
|
return None
|
|
|
|
year = int(self.walletname[0:4])
|
|
|
|
if year < 1976 or year > 2050:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return str(year)
|
|
|
|
|
2022-07-27 23:48:22 +01:00
|
|
|
|
2022-07-29 18:55:19 +01:00
|
|
|
# Yes this is horribly, horribly inefficient, esp. for a page that have date, people and cave in it
|
2022-07-27 23:48:22 +01:00
|
|
|
def date(self):
|
|
|
|
jsondata = self.get_json()
|
|
|
|
return jsondata["date"]
|
|
|
|
|
2022-07-28 00:37:44 +01:00
|
|
|
def people(self):
|
|
|
|
jsondata = self.get_json()
|
|
|
|
return jsondata["people"]
|
|
|
|
|
2022-07-29 18:55:19 +01:00
|
|
|
def cave(self):
|
|
|
|
jsondata = self.get_json()
|
|
|
|
return jsondata["cave"]
|
|
|
|
|
2022-07-27 23:48:22 +01:00
|
|
|
def name(self):
|
|
|
|
jsondata = self.get_json()
|
|
|
|
return jsondata["name"]
|
|
|
|
|
2020-05-26 02:21:36 +01:00
|
|
|
def __str__(self):
|
2022-07-29 18:55:19 +01:00
|
|
|
return "[" + str(self.walletname) + " (Wallet)]"
|
2020-05-28 01:16:45 +01:00
|
|
|
|
2020-06-24 00:18:01 +01:00
|
|
|
class SingleScan(models.Model):
|
2021-04-26 19:50:03 +01:00
|
|
|
ffile = models.CharField(max_length=200)
|
|
|
|
name = models.CharField(max_length=200)
|
|
|
|
wallet = models.ForeignKey("Wallet", null=True,on_delete=models.SET_NULL)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2021-04-26 19:50:03 +01:00
|
|
|
return urljoin(settings.URL_ROOT, reverse('scansingle', kwargs={"path":re.sub("#", "%23", self.wallet.walletname), "file":self.name}))
|
2020-05-15 21:32:55 +01:00
|
|
|
|
2020-05-26 02:21:36 +01:00
|
|
|
def __str__(self):
|
2021-04-26 19:50:03 +01:00
|
|
|
return "Survey Scan Image: " + str(self.name) + " in " + str(self.wallet)
|
2020-05-28 01:16:45 +01:00
|
|
|
|
2021-04-26 18:08:42 +01:00
|
|
|
class DrawingFile(models.Model):
|
2021-04-26 18:11:14 +01:00
|
|
|
dwgpath = models.CharField(max_length=200)
|
2021-04-26 18:37:59 +01:00
|
|
|
dwgname = models.CharField(max_length=200)
|
2022-07-27 23:48:22 +01:00
|
|
|
dwgwallets = models.ManyToManyField("Wallet") # implicitly links via folders to scans to SVX files
|
2021-04-26 19:22:29 +01:00
|
|
|
scans = models.ManyToManyField("SingleScan") # implicitly links via scans to SVX files
|
2021-04-26 18:54:17 +01:00
|
|
|
dwgcontains = models.ManyToManyField("DrawingFile") # case when its a frame type
|
2021-04-26 19:22:29 +01:00
|
|
|
filesize = models.IntegerField(default=0)
|
|
|
|
npaths = models.IntegerField(default=0)
|
|
|
|
survexfiles = models.ManyToManyField("SurvexFile") # direct link to SVX files - not populated yet
|
2020-06-24 01:57:20 +01:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class Meta:
|
2021-05-05 00:35:10 +01:00
|
|
|
ordering = ('dwgpath',)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Drawing File: " + str(self.dwgname) + " (" + str(self.filesize) + " bytes)"
|
|
|
|
|