import re from pathlib import Path from urllib.parse import urljoin from django.db import models from django.urls import reverse from django.template import loader import settings from troggle.core.models.troggle import Expedition, TroggleModel """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 """ class CaveSlug(models.Model): """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) primary = models.BooleanField(default=False) def __str__(self): return f"{self.slug}: {self.cave}" class LogbookEntry(TroggleModel): """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() 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" ) other_people = models.CharField(max_length=200, blank=True, null=True) # foreign_friends and *guests text = models.TextField() slug = models.SlugField(max_length=50) # this is tripid time_underground = models.FloatField(null=True, help_text="In decimal hours") class Meta: verbose_name_plural = "Logbook Entries" # several PersonLogEntrys point in to this object ordering = ("-date",) def cave(self): # Why didn't he just make this a foreign key to Cave ? c = CaveSlug.objects.get(slug=self.cave_slug, primary=True).cave return c def isLogbookEntry(self): # Function used in templates return True def get_absolute_url(self): return urljoin(settings.URL_ROOT, reverse("logbookentry", kwargs={"date": self.date, "slug": self.slug})) def __str__(self): return f"{self.date}: {self.title}" def get_next_by_id(self): LogbookEntry.objects.get(id=self.id + 1) def get_previous_by_id(self): LogbookEntry.objects.get(id=self.id - 1) def DayIndex(self): """This is used to set different colours for the different trips on the calendar view of the expedition""" mx = 10 todays = list(LogbookEntry.objects.filter(date=self.date)) if self in todays: index = todays.index(self) else: print(f"DayIndex: Synchronization error in logbook entries. Restart server or do full reset. {self}") index = 0 if index not in range(0, mx): print(f"DayIndex: More than {mx-1} LogbookEntry items on one day '{index}' {self}, restarting colour sequence.") index = index % mx return index 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. print(f" - Logbook to be exported has {len(logbook_entries)} entries in it.") template = "logbook2005style.html" 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") try: print(" - end") print(f" - endmatter {year} {filename} {settings.EXPOWEB}") try: endpath = Path(settings.EXPOWEB, "years", year, "endmatter.html") except: print(" ! FAIL Path " + {(settings.EXPOWEB, "years", year, "endmatter.html")}) print(f" - endpath {endpath}") endmatter = "" if endpath.is_file(): print(" - endpath") try: with open(endpath, "r") as end: endmatter = end.read() except: print(" ! Very Bad Error opening " + endpath) except: print(" ! FAIL endpath " + endpath) raise print(" - endpath opened") frontpath = Path(settings.EXPOWEB, "years", year, "frontmatter.html") 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"
", "\n" + frontmatter + endmatter, logbookfile) else: logbookfile = re.sub(r"", f"\n