2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-26 17:21:52 +00:00
troggle/core/models_survex.py

239 lines
9.4 KiB
Python
Raw Normal View History

2011-07-11 02:10:22 +01:00
import os
2020-06-18 00:20:47 +01:00
from urllib.parse import urljoin
2011-07-11 02:10:22 +01:00
import re
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
###########################################################
# These will allow browsing and editing of the survex data
###########################################################
# Needs to add:
# Equates
# reloading
class SurvexDirectory(models.Model):
path = models.CharField(max_length=200)
2020-06-18 00:20:47 +01:00
cave = models.ForeignKey('Cave', blank=True, null=True,on_delete=models.CASCADE)
primarysurvexfile = models.ForeignKey('SurvexFile', related_name='primarysurvexfile', blank=True, null=True,on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
# could also include files in directory but not referenced
class Meta:
ordering = ('id',)
2011-07-11 02:10:22 +01:00
class SurvexFile(models.Model):
path = models.CharField(max_length=200)
2020-06-18 00:20:47 +01:00
survexdirectory = models.ForeignKey("SurvexDirectory", blank=True, null=True,on_delete=models.CASCADE)
cave = models.ForeignKey('Cave', blank=True, null=True,on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
class Meta:
ordering = ('id',)
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]
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()
2011-07-11 02:10:22 +01:00
class SurvexEquate(models.Model):
2020-06-18 00:20:47 +01:00
cave = models.ForeignKey('Cave', blank=True, null=True,on_delete=models.CASCADE)
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),
name__iexact = stationname)
2011-07-11 02:10:22 +01:00
class SurvexStation(models.Model):
name = models.CharField(max_length=100)
2020-06-18 00:20:47 +01:00
block = models.ForeignKey('SurvexBlock',on_delete=models.CASCADE)
equate = models.ForeignKey('SurvexEquate', blank=True, null=True,on_delete=models.CASCADE)
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)
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
2020-06-16 19:27:32 +01:00
# class SurvexLeg(models.Model):
2020-06-18 00:20:47 +01:00
# block = models.ForeignKey('SurvexBlock',on_delete=models.CASCADE)
# #title = models.ForeignKey('SurvexTitle',on_delete=models.CASCADE)
# stationfrom = models.ForeignKey('SurvexStation', related_name='stationfrom',on_delete=models.CASCADE)
# stationto = models.ForeignKey('SurvexStation', related_name='stationto',on_delete=models.CASCADE)
2020-06-16 19:27:32 +01:00
# tape = models.FloatField()
# compass = models.FloatField()
# clino = models.FloatField()
class SurvexLeg():
tape = 0.0
compass = 0.0
clino = 0.0
2011-07-11 02:10:22 +01:00
#
# Single SurvexBlock
#
class SurvexBlockLookUpManager(models.Manager):
def lookup(self, name):
if name == "":
blocknames = []
else:
blocknames = name.split(".")
block = SurvexBlock.objects.get(parent=None, survexfile__path=settings.SURVEX_TOPNAME)
2011-07-11 02:10:22 +01:00
for blockname in blocknames:
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-06-18 00:20:47 +01:00
parent = models.ForeignKey('SurvexBlock', blank=True, null=True,on_delete=models.CASCADE)
2020-06-16 19:27:32 +01:00
# text = models.TextField()
2020-06-18 00:20:47 +01:00
cave = models.ForeignKey('Cave', blank=True, null=True,on_delete=models.CASCADE)
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-18 00:20:47 +01:00
expeditionday = models.ForeignKey("ExpeditionDay", null=True,on_delete=models.CASCADE)
expedition = models.ForeignKey('Expedition', blank=True, null=True,on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
2020-06-18 00:20:47 +01:00
survexfile = models.ForeignKey("SurvexFile", blank=True, null=True,on_delete=models.CASCADE)
2020-06-16 19:27:32 +01:00
# begin_char = models.IntegerField() # code for where in the survex data files this block sits
2011-07-11 02:10:22 +01:00
survexpath = models.CharField(max_length=200) # the path for the survex stations
2020-06-18 00:20:47 +01:00
survexscansfolder = models.ForeignKey("SurvexScansFolder", null=True,on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
#refscandir = models.CharField(max_length=100)
2020-06-18 00:20:47 +01:00
legsall = models.IntegerField(null=True) # summary data for this block
legssplay = models.IntegerField(null=True) # summary data for this block
legssurfc = models.IntegerField(null=True) # summary data for this block
totalleglength = models.FloatField(null=True)
2011-07-11 02:10:22 +01:00
class Meta:
ordering = ('id',)
def isSurvexBlock(self): # Function used in templates
return True
2020-05-24 01:57:06 +01:00
def __str__(self):
return self.name and str(self.name) or 'no name'
2011-07-11 02:10:22 +01:00
def GetPersonroles(self):
res = [ ]
for personrole in self.personrole_set.order_by('personexpedition'):
if res and res[-1]['person'] == personrole.personexpedition.person:
res[-1]['roles'] += ", " + str(personrole.role)
else:
res.append({'person':personrole.personexpedition.person, 'expeditionyear':personrole.personexpedition.expedition.year, 'roles':str(personrole.role)})
return res
def MakeSurvexStation(self, name):
ssl = self.survexstation_set.filter(name=name)
if ssl:
assert len(ssl) == 1
return ssl[0]
#print name
2011-07-11 02:10:22 +01:00
ss = SurvexStation(name=name, block=self)
2020-06-15 03:28:51 +01:00
#ss.save()
2011-07-11 02:10:22 +01:00
return ss
def DayIndex(self):
return list(self.expeditionday.survexblock_set.all()).index(self)
class SurvexTitle(models.Model):
2020-06-18 00:20:47 +01:00
survexblock = models.ForeignKey('SurvexBlock',on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
title = models.CharField(max_length=200)
2020-06-18 00:20:47 +01:00
cave = models.ForeignKey('Cave', blank=True, null=True,on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
#
# member of a SurvexBlock
#
ROLE_CHOICES = (
('insts','Instruments'),
('dog','Other'),
('notes','Notes'),
('pics','Pictures'),
('tape','Tape measure'),
('useless','Useless'),
('helper','Helper'),
('disto','Disto'),
('consultant','Consultant'),
)
class SurvexPersonRole(models.Model):
2020-06-18 00:20:47 +01:00
survexblock = models.ForeignKey('SurvexBlock',on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
nrole = models.CharField(choices=ROLE_CHOICES, max_length=200, blank=True, null=True)
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-18 00:20:47 +01:00
person = models.ForeignKey('Person', blank=True, null=True,on_delete=models.CASCADE)
personexpedition = models.ForeignKey('PersonExpedition', blank=True, null=True,on_delete=models.CASCADE)
persontrip = models.ForeignKey('PersonTrip', blank=True, null=True,on_delete=models.CASCADE)
expeditionday = models.ForeignKey("ExpeditionDay", null=True,on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
2020-05-26 02:21:36 +01:00
def __str__(self):
2020-05-24 01:57:06 +01:00
return str(self.person) + " - " + str(self.survexblock) + " - " + str(self.nrole)
2011-07-11 02:10:22 +01:00
class SurvexScansFolder(models.Model):
fpath = models.CharField(max_length=200)
walletname = models.CharField(max_length=200)
class Meta:
ordering = ('walletname',)
def get_absolute_url(self):
2020-06-18 00:20:47 +01:00
return urljoin(settings.URL_ROOT, reverse('surveyscansfolder', kwargs={"path":re.sub("#", "%23", self.walletname)}))
2020-05-15 21:32:55 +01:00
2020-05-26 02:21:36 +01:00
def __str__(self):
2020-05-24 01:57:06 +01:00
return str(self.walletname) + " (Survey Scans Folder)"
2011-07-11 02:10:22 +01:00
class SurvexScanSingle(models.Model):
ffile = models.CharField(max_length=200)
name = models.CharField(max_length=200)
2020-06-18 00:20:47 +01:00
survexscansfolder = models.ForeignKey("SurvexScansFolder", null=True,on_delete=models.CASCADE)
2011-07-11 02:10:22 +01:00
class Meta:
ordering = ('name',)
def get_absolute_url(self):
2020-06-18 00:20:47 +01:00
return urljoin(settings.URL_ROOT, reverse('surveyscansingle', kwargs={"path":re.sub("#", "%23", self.survexscansfolder.walletname), "file":self.name}))
2020-05-15 21:32:55 +01:00
2020-05-26 02:21:36 +01:00
def __str__(self):
2020-05-24 01:57:06 +01:00
return "Survey Scan Image: " + str(self.name) + " in " + str(self.survexscansfolder)
2011-07-11 02:10:22 +01:00
class TunnelFile(models.Model):
tunnelpath = models.CharField(max_length=200)
tunnelname = models.CharField(max_length=200)
bfontcolours = models.BooleanField(default=False)
2011-07-11 02:10:22 +01:00
survexscansfolders = models.ManyToManyField("SurvexScansFolder")
survexscans = models.ManyToManyField("SurvexScanSingle")
survexblocks = models.ManyToManyField("SurvexBlock")
tunnelcontains = models.ManyToManyField("TunnelFile") # case when its a frame type
filesize = models.IntegerField(default=0)
npaths = models.IntegerField(default=0)
survextitles = models.ManyToManyField("SurvexTitle")
class Meta:
ordering = ('tunnelpath',)