2023-01-19 18:35:56 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from urllib.parse import urljoin
|
2023-02-26 22:13:37 +00:00
|
|
|
from pathlib import Path
|
2020-05-28 01:16:45 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2023-01-19 18:35:56 +00:00
|
|
|
from django.db import models
|
2020-06-18 21:50:16 +01:00
|
|
|
from django.urls import reverse
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2022-09-20 20:52:31 +01:00
|
|
|
# from troggle.core.models.troggle import DataIssue # circular import. Hmm
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class SurvexDirectory(models.Model):
|
2023-03-06 16:37:38 +00:00
|
|
|
"""This relates a Cave to the primary SurvexFile which is the 'head' of the survex tree for
|
|
|
|
that cave. Surely this could just be a property of Cave ? No. Several subdirectories
|
|
|
|
all relate to the same Cave
|
|
|
|
"""
|
|
|
|
path = models.CharField(max_length=200)
|
2023-01-30 19:04:36 +00: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
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class Meta:
|
2023-01-30 19:04:36 +00:00
|
|
|
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):
|
2023-01-30 19:04:36 +00: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)
|
2023-01-30 19:04:36 +00: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:
|
2023-01-30 19:04:36 +00:00
|
|
|
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):
|
2023-01-30 19:04:36 +00:00
|
|
|
# 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):
|
2023-02-26 22:13:37 +00:00
|
|
|
fname = Path(settings.SURVEX_DATA, self.path + ".svx")
|
|
|
|
return fname.is_file()
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def OpenFile(self):
|
|
|
|
fname = os.path.join(settings.SURVEX_DATA, self.path + ".svx")
|
|
|
|
return open(fname)
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
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()
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.path
|
2020-05-28 01:16:45 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class SurvexStationLookUpManager(models.Manager):
|
2023-03-06 22:30:36 +00:00
|
|
|
"""what this does,
|
2023-03-06 16:37:38 +00:00
|
|
|
https://docs.djangoproject.com/en/dev/topics/db/managers/
|
|
|
|
This changes the .objects thinggy to use a case-insensitive match name__iexact
|
|
|
|
so that now SurvexStation.objects.lookup() works as a case-insensitive match
|
|
|
|
"""
|
2011-07-11 02:10:22 +01:00
|
|
|
def lookup(self, name):
|
|
|
|
blocknames, sep, stationname = name.rpartition(".")
|
2023-01-30 19:04:36 +00:00
|
|
|
return self.get(block=SurvexBlock.objects.lookup(blocknames), name__iexact=stationname)
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
class SurvexStation(models.Model):
|
2023-01-30 19:04:36 +00:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
block = models.ForeignKey("SurvexBlock", null=True, on_delete=models.SET_NULL)
|
2023-03-08 18:02:00 +00:00
|
|
|
objects = SurvexStationLookUpManager() # overwrites SurvexStation.objects and enables lookup()
|
2011-07-11 02:10:22 +01:00
|
|
|
x = models.FloatField(blank=True, null=True)
|
|
|
|
y = models.FloatField(blank=True, null=True)
|
|
|
|
z = models.FloatField(blank=True, null=True)
|
2023-01-30 19:04:36 +00:00
|
|
|
|
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:
|
2023-01-30 19:04:36 +00:00
|
|
|
ordering = ("id",)
|
|
|
|
|
2021-04-20 19:47:08 +01:00
|
|
|
def __str__(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
return self.name and str(self.name) or "no name"
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
#
|
2023-01-30 19:04:36 +00:00
|
|
|
# Single SurvexBlock
|
|
|
|
#
|
2023-03-06 22:30:36 +00:00
|
|
|
class SurvexBlockLookUpManager(models.Manager):
|
|
|
|
"""what this does,
|
|
|
|
https://docs.djangoproject.com/en/dev/topics/db/managers/
|
2023-03-06 16:37:38 +00:00
|
|
|
|
2023-03-06 22:30:36 +00:00
|
|
|
This adds a method to the .objects thinggy to use a case-insensitive match name__iexact
|
|
|
|
so that now SurvexBlock.objects.lookup() works as a case-insensitive match.
|
|
|
|
This is used in lookup() in SurvexStationLookUpManager()
|
|
|
|
which is used in Entrance().other_location() which is used in the Cave webpage
|
|
|
|
"""
|
|
|
|
def lookup(self, name):
|
|
|
|
if name == "":
|
|
|
|
blocknames = []
|
|
|
|
else:
|
|
|
|
blocknames = name.split(".")
|
2023-03-08 18:02:00 +00:00
|
|
|
block = SurvexBlock.objects.get(parent=None, survexfile__path=settings.SURVEX_TOPNAME)
|
2023-03-06 22:30:36 +00:00
|
|
|
for blockname in blocknames:
|
|
|
|
block = SurvexBlock.objects.get(parent=block, name__iexact=blockname)
|
|
|
|
return block
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class SurvexBlock(models.Model):
|
2023-03-06 04:52:41 +00:00
|
|
|
"""One begin..end block within a survex file. The basic element of a survey trip.
|
|
|
|
Multiple anonymous survex blocks are possible within the same surfex file
|
|
|
|
"""
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-03-08 18:02:00 +00:00
|
|
|
objects = SurvexBlockLookUpManager() # overwrites SurvexBlock.objects and enables lookup()
|
2023-01-30 19:04:36 +00:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
title = models.CharField(max_length=200)
|
|
|
|
parent = models.ForeignKey("SurvexBlock", blank=True, null=True, on_delete=models.SET_NULL)
|
|
|
|
|
|
|
|
date = models.DateField(blank=True, null=True)
|
|
|
|
expedition = models.ForeignKey("Expedition", blank=True, null=True, on_delete=models.SET_NULL)
|
|
|
|
|
|
|
|
survexfile = models.ForeignKey("SurvexFile", blank=True, null=True, on_delete=models.SET_NULL)
|
|
|
|
survexpath = models.CharField(max_length=200) # the path for the survex stations
|
|
|
|
|
|
|
|
scanswallet = models.ForeignKey(
|
|
|
|
"Wallet", null=True, on_delete=models.SET_NULL
|
|
|
|
) # only ONE wallet per block. The most recent seen overwites.. ugh.
|
|
|
|
|
|
|
|
legsall = models.IntegerField(null=True) # summary data for this block
|
2020-07-04 13:31:46 +01:00
|
|
|
legslength = models.FloatField(null=True)
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class Meta:
|
2023-01-30 19:04:36 +00:00
|
|
|
ordering = ("id",)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
def __str__(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
return "[SurvexBlock:" + str(self.name) + "-path:" + str(self.survexpath) + "-cave:" + str(self.cave) + "]"
|
|
|
|
|
2020-06-28 14:42:26 +01:00
|
|
|
def __str__(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
return self.name and str(self.name) or "no name"
|
2020-06-28 14:42:26 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
def isSurvexBlock(self): # Function used in templates
|
2011-07-11 02:10:22 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def DayIndex(self):
|
2023-01-29 20:59:56 +00:00
|
|
|
"""This is used to set different colours for the different trips on
|
|
|
|
the calendar view of the expedition"""
|
2023-01-30 15:28:11 +00:00
|
|
|
mx = 10
|
2023-01-29 20:59:56 +00:00
|
|
|
index = list(SurvexBlock.objects.filter(date=self.date)).index(self)
|
2023-01-30 15:28:11 +00:00
|
|
|
if index not in range(0, mx):
|
|
|
|
print(f"DayIndex: More than {mx-1} SurvexBlock items on one day '{index}' {self}")
|
|
|
|
index = 0
|
2023-01-30 19:04:36 +00:00
|
|
|
# return list(self.survexblock_set.all()).index(self)
|
2023-01-29 20:59:56 +00:00
|
|
|
return index
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class SurvexPersonRole(models.Model):
|
2023-03-06 16:37:38 +00:00
|
|
|
"""The CASCADE means that if a SurvexBlock or a Person is deleted, then the SurvexPersonRole
|
|
|
|
is deleted too
|
|
|
|
"""
|
2023-01-30 19:04:36 +00:00
|
|
|
survexblock = models.ForeignKey("SurvexBlock", on_delete=models.CASCADE)
|
|
|
|
# increasing levels of precision, Surely we only need survexblock and person now that we have no link to a logbook entry?
|
|
|
|
personname = models.CharField(max_length=100)
|
2023-03-06 16:37:38 +00:00
|
|
|
person = models.ForeignKey("Person", blank=True, null=True, on_delete=models.CASCADE) # not needed
|
2023-01-30 19:04:36 +00:00
|
|
|
personexpedition = models.ForeignKey("PersonExpedition", blank=True, null=True, on_delete=models.SET_NULL)
|
|
|
|
|
2020-05-26 02:21:36 +01:00
|
|
|
def __str__(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
return str(self.personname) + " - " + str(self.survexblock)
|
|
|
|
|
2020-05-28 01:16:45 +01:00
|
|
|
|
2020-06-24 00:18:01 +01:00
|
|
|
class SingleScan(models.Model):
|
2023-01-30 19:04:36 +00:00
|
|
|
"""A single file holding an image. Could be raw notes, an elevation plot or whatever"""
|
|
|
|
|
|
|
|
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:
|
2023-01-30 19:04:36 +00:00
|
|
|
ordering = ("name",)
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def get_absolute_url(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
return urljoin(
|
|
|
|
settings.URL_ROOT,
|
|
|
|
reverse("scansingle", kwargs={"path": re.sub("#", "%23", self.wallet.walletname), "file": self.name}),
|
|
|
|
)
|
|
|
|
|
2020-05-26 02:21:36 +01:00
|
|
|
def __str__(self):
|
2022-12-23 23:32:59 +00:00
|
|
|
return "Scan Image: " + str(self.name) + " in " + str(self.wallet)
|
2020-05-28 01:16:45 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2021-04-26 18:08:42 +01:00
|
|
|
class DrawingFile(models.Model):
|
2023-01-30 19:04:36 +00:00
|
|
|
"""A file holding a Therion (several types) or a Tunnel drawing"""
|
|
|
|
|
|
|
|
dwgpath = models.CharField(max_length=200)
|
|
|
|
dwgname = models.CharField(max_length=200)
|
|
|
|
dwgwallets = models.ManyToManyField("Wallet") # implicitly links via folders to scans to SVX files
|
|
|
|
scans = models.ManyToManyField("SingleScan") # implicitly links via scans to SVX files
|
|
|
|
dwgcontains = models.ManyToManyField("DrawingFile") # case when its a frame type
|
|
|
|
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:
|
2023-01-30 19:04:36 +00:00
|
|
|
ordering = ("dwgpath",)
|
2021-05-05 00:35:10 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-01-30 19:04:36 +00:00
|
|
|
return "Drawing File: " + str(self.dwgname) + " (" + str(self.filesize) + " bytes)"
|