2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-22 15:21:52 +00:00
troggle/core/models/logbooks.py

292 lines
11 KiB
Python
Raw Normal View History

2023-08-31 16:55:20 +01:00
import re
2023-01-29 16:47:46 +00:00
from pathlib import Path
from urllib.parse import urljoin
from django.db import models
from django.urls import reverse
2023-08-31 16:55:20 +01:00
from django.template import loader
2023-01-29 16:47:46 +00:00
import settings
2023-01-30 23:04:11 +00:00
from troggle.core.models.troggle import Expedition, TroggleModel
2023-01-29 16:47:46 +00:00
"""The model declarations LogBookEntry, PersonLogEntry, QM
"""
todo = """
- Can we rewrite things to eliminate the CaveSlug and objects? No
Surely foreign keys work fine ?! No
Foreign keys do not allow for there being multiple ways to refer to a cave, eg 1623-1999-03 aka 1623-204
Having slugs allows for much more loose coupling to caves, which removes alot of the need to reset the database, which interupts work flow.
It also means we do not have to be creating temporary cave objects in the database, where we do not have the underlying file in cave_data.
To Do move Cave Slug back to troggle.core.models
"""
2023-01-29 16:47:46 +00:00
2023-01-29 22:11:00 +00:00
class CaveSlug(models.Model):
2023-03-06 16:37:38 +00:00
"""Moved here to avoid nasty cyclic import error
CASCADE means that if the Cave is deleted, this is too
"""
cave = models.ForeignKey("Cave", on_delete=models.CASCADE)
slug = models.SlugField(max_length=50, unique=True)
2023-01-29 22:11:00 +00:00
primary = models.BooleanField(default=False)
def __str__(self):
return f"{self.slug}: {self.cave}"
2023-01-29 16:47:46 +00:00
class LogbookEntry(TroggleModel):
2023-03-06 16:37:38 +00:00
"""Single parsed entry from Logbook
Gets deleted if the Expedition gets deleted"""
date = (
models.DateField()
) # MJG wants to turn this into a datetime such that multiple Logbook entries on the same day can be ordered.ld()
2023-03-06 16:37:38 +00:00
expedition = models.ForeignKey(Expedition, blank=True, null=True, on_delete=models.CASCADE) # yes this is double-
title = models.CharField(max_length=200)
cave_slug = models.SlugField(max_length=50, blank=True, null=True)
place = models.CharField(
max_length=100, blank=True, null=True, help_text="Only use this if you haven't chosen a cave"
)
2023-09-02 12:49:43 +01:00
other_people = models.CharField(max_length=200, blank=True, null=True) # foreign_friends and *guests
text = models.TextField()
2023-08-31 16:55:20 +01:00
slug = models.SlugField(max_length=50) # this is tripid
time_underground = models.FloatField(null=True, help_text="In decimal hours")
2023-01-29 16:47:46 +00:00
class Meta:
verbose_name_plural = "Logbook Entries"
2023-01-30 16:18:19 +00:00
# several PersonLogEntrys point in to this object
ordering = ("-date",)
2023-01-29 16:47:46 +00:00
def cave(self): # Why didn't he just make this a foreign key to Cave ?
2023-01-29 16:47:46 +00:00
c = CaveSlug.objects.get(slug=self.cave_slug, primary=True).cave
return c
def isLogbookEntry(self): # Function used in templates
2023-01-29 16:47:46 +00:00
return True
def get_absolute_url(self):
return urljoin(settings.URL_ROOT, reverse("logbookentry", kwargs={"date": self.date, "slug": self.slug}))
2023-01-29 16:47:46 +00:00
def __str__(self):
return f"{self.date}: {self.title}"
2023-01-29 16:47:46 +00:00
def get_next_by_id(self):
LogbookEntry.objects.get(id=self.id + 1)
2023-01-29 16:47:46 +00:00
def get_previous_by_id(self):
LogbookEntry.objects.get(id=self.id - 1)
2023-01-29 16:47:46 +00:00
def DayIndex(self):
"""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
todays = list(LogbookEntry.objects.filter(date=self.date))
if self in todays:
index = todays.index(self)
else:
2023-07-14 10:13:06 +01:00
print(f"DayIndex: Synchronization error in logbook entries. Restart server or do full reset. {self}")
index = 0
2023-01-30 15:28:11 +00:00
if index not in range(0, mx):
2023-07-14 10:13:06 +01:00
print(f"DayIndex: More than {mx-1} LogbookEntry items on one day '{index}' {self}, restarting colour sequence.")
index = index % mx
return index
2023-08-31 16:55:20 +01:00
def writelogbook(year, filename):
current_expedition = Expedition.objects.get(year=year)
logbook_entries = LogbookEntry.objects.filter(expedition=current_expedition).order_by(
"slug"
) # now that slug, aka tripid, is in our standard date form, this will preserve ordering.
2023-09-02 17:23:22 +01:00
print(f" - Logbook to be exported has {len(logbook_entries)} entries in it.")
2023-08-31 16:55:20 +01:00
template = "logbook2005style.html"
2023-09-02 17:23:22 +01:00
try:
t = loader.get_template(template)
logbookfile = t.render({"logbook_entries": logbook_entries})
except:
print(" ! Very Bad Error RENDERING template " + template)
raise
# print(" - template rendered")
2023-09-02 17:23:22 +01:00
try:
2023-08-31 16:55:20 +01:00
try:
endpath = Path(settings.EXPOWEB, "years", str(year), "endmatter.html")
2023-08-31 16:55:20 +01:00
except:
2023-09-02 17:23:22 +01:00
print(" ! FAIL Path " + {(settings.EXPOWEB, "years", year, "endmatter.html")})
raise
# print(f" - endpath {endpath}")
2023-09-02 17:23:22 +01:00
endmatter = ""
if endpath.is_file():
# print(" - endpath")
2023-09-02 17:23:22 +01:00
try:
with open(endpath, "r") as end:
endmatter = end.read()
except:
print(" ! Very Bad Error opening " + endpath)
raise
2023-09-02 17:23:22 +01:00
except:
print(" ! FAIL endpath " + endpath)
raise
# print(" - endpath opened")
2023-08-31 16:55:20 +01:00
frontpath = Path(settings.EXPOWEB, "years", str(year), "frontmatter.html")
2023-08-31 16:55:20 +01:00
if frontpath.is_file():
try:
with open(frontpath, "r") as front:
frontmatter = front.read()
except:
print(" ! Very Bad Error opening " + frontpath)
logbookfile = re.sub(r"<body>", "<body>\n" + frontmatter + endmatter, logbookfile)
else:
logbookfile = re.sub(r"<body>", f"<body>\n<h1>Expo {year}</h1>\n" + endmatter, logbookfile)
# print(" - frontmatter opened")
2023-08-31 16:55:20 +01:00
dir = Path(settings.EXPOWEB) / "years" / str(year)
2023-08-31 16:55:20 +01:00
filepath = Path(dir, filename)
2023-09-02 17:23:22 +01:00
try:
with (open(filepath, "w")) as lb:
lb.writelines(logbookfile)
except:
print(" ! Very Bad Error writing to " + filepath)
raise
2023-08-31 16:55:20 +01:00
# print(f'Logbook exported to {filepath}')
2023-01-30 16:18:19 +00:00
class PersonLogEntry(TroggleModel):
2023-01-29 16:47:46 +00:00
"""Single Person going on a trip, which may or may not be written up.
It could account for different T/U for people in same logbook entry.
2023-03-06 16:37:38 +00:00
CASCADE means that if the personexpedition or the logbookentry is deleted,
then this PersonLogEntry is deleted too
2023-01-29 16:47:46 +00:00
"""
personexpedition = models.ForeignKey("PersonExpedition", null=True, on_delete=models.CASCADE)
2023-01-29 16:47:46 +00:00
time_underground = models.FloatField(help_text="In decimal hours")
logbook_entry = models.ForeignKey(LogbookEntry, on_delete=models.CASCADE)
2023-01-29 16:47:46 +00:00
is_logbook_entry_author = models.BooleanField(default=False)
2023-08-26 16:39:29 +01:00
nickname_used = models.CharField(max_length=100,default="") # e.g. "Animal" or "Zonker", as it appears in the original logbook
2023-01-29 16:47:46 +00:00
class Meta:
ordering = ("-personexpedition",)
# order_with_respect_to = 'personexpedition'
2023-01-30 16:07:44 +00:00
def next_personlog(self):
futurePTs = (
PersonLogEntry.objects.filter(
personexpedition=self.personexpedition, logbook_entry__date__gt=self.logbook_entry.date
)
.order_by("logbook_entry__date")
.all()
)
2023-01-29 16:47:46 +00:00
if len(futurePTs) > 0:
return futurePTs[0]
else:
return None
2023-01-30 16:07:44 +00:00
def prev_personlog(self):
pastPTs = (
PersonLogEntry.objects.filter(
personexpedition=self.personexpedition, logbook_entry__date__lt=self.logbook_entry.date
)
.order_by("-logbook_entry__date")
.all()
)
2023-01-29 16:47:46 +00:00
if len(pastPTs) > 0:
return pastPTs[0]
else:
return None
def place(self):
return self.logbook_entry.cave and self.logbook_entry.cave or self.logbook_entry.place
def __str__(self):
return f"{self.personexpedition} ({self.logbook_entry.date})"
2023-01-29 16:47:46 +00:00
class QM(TroggleModel):
"""This is based on qm.csv in trunk/expoweb/1623/204 which has the fields:
"Number","Grade","Area","Description","Page reference","Nearest station","Completion description","Comment"
2023-03-14 16:11:37 +00:00
All the stuff handling TICK QMs is INCOMPLETE
2023-01-29 16:47:46 +00:00
"""
2023-03-16 21:06:52 +00:00
number = models.IntegerField(
help_text="this is the sequential number in the year, only unique for CSV imports",
)
2023-03-17 20:01:52 +00:00
grade = models.CharField(max_length=1, blank=True, null=True, help_text="A/B/C/D/X")
cave = models.ForeignKey("Cave", related_name="QMs", blank=True, null=True, on_delete=models.SET_NULL)
block = models.ForeignKey("SurvexBlock", null=True, on_delete=models.SET_NULL) # only for QMs from survex files
blockname = models.TextField(blank=True, null=True) # NB truncated copy of survexblock name with last char added
2023-03-17 20:01:52 +00:00
expoyear = models.CharField(max_length=4, blank=True, null=True)
ticked = models.BooleanField(default=False)
2023-03-18 03:03:06 +00:00
location_description = models.TextField(blank=True, null=True)
completion_description = models.TextField(blank=True, null=True)
2023-03-17 20:01:52 +00:00
completion_date = models.DateField(blank=True, null=True)
2023-03-16 21:06:52 +00:00
nearest_station_name = models.CharField(max_length=200, blank=True, null=True)
resolution_station_name = models.CharField(max_length=200, blank=True, null=True)
area = models.CharField(max_length=100, blank=True, null=True)
2023-03-17 20:01:52 +00:00
page_ref = models.TextField(blank=True, null=True)
comment = models.TextField(blank=True, null=True)
2023-01-29 16:47:46 +00:00
def __str__(self):
return f"{self.code()}"
2023-01-29 16:47:46 +00:00
def code(self):
if self.cave:
cavestr = str(self.cave.slug())[5:]
else:
cavestr = ""
if self.expoyear:
expoyearstr = str(self.expoyear)
else:
expoyearstr = str(self.cave.slug())[5:9]
if self.blockname:
blocknamestr = "-" + str(self.blockname)
else:
blocknamestr = ""
return f"{cavestr}-{expoyearstr}-{self.number}{self.grade}{blocknamestr}"
2023-01-29 16:47:46 +00:00
2023-03-18 03:03:06 +00:00
# def get_completion_url(self):
# """assumes html file named is in same folder as cave description file
# WRONG - needs rewriting!"""
# cd = None
# if self.completion_description:
# try:
# dir = Path(self.cave.url).parent
# cd = dir / self.completion_description
# except:
# cd = None
# return cd
def newslug(self):
qmslug = f"{str(self.cave)}-{self.expoyear}-{self.blockname}{self.number}{self.grade}"
2023-01-29 16:47:46 +00:00
return qmslug
2023-01-29 16:47:46 +00:00
def get_absolute_url(self):
# This reverse resolution stuff is pure magic. Just change the regex in urls.py and everything changes to suit. Whacky.
return urljoin(
settings.URL_ROOT,
reverse(
"qm",
kwargs={
"cave_id": self.cave.slug(),
"year": self.expoyear,
"blockname": self.blockname,
"qm_id": self.number,
"grade": self.grade,
},
),
)
2023-03-17 20:01:52 +00:00
def get_next_by_id(self): # called in template
2023-03-17 14:33:30 +00:00
return QM.objects.get(id=self.id + 1)
2023-01-29 16:47:46 +00:00
2023-03-17 20:01:52 +00:00
def get_previous_by_id(self): # called in template
2023-03-17 14:33:30 +00:00
return QM.objects.get(id=self.id - 1)