troggle-unchained/logbooksdump.py

89 lines
4.2 KiB
Python
Raw Normal View History

2020-05-13 23:11:47 +01:00
import os
import time
import timeit
2020-05-24 01:57:06 +01:00
2020-05-13 23:11:47 +01:00
import settings
2020-07-18 16:23:54 +01:00
"""Two currently unused functions. To be re-engineered to produce a logbook file
in canonical post-2010 Parseloghtmltxt() format after importing from one of the
older more artisanal formats which will then be retired. For example, 2003 used
a unique HTML format and we should regularise this and deprecate the unique parser
code Parseloghtml03().
"""
2020-05-13 23:11:47 +01:00
os.environ['PYTHONPATH'] = settings.PYTHON_PATH
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
2020-05-24 01:57:06 +01:00
2020-05-13 23:11:47 +01:00
from django.core import management
from django.db import connection, close_old_connections
from django.contrib.auth.models import User
from django.http import HttpResponse
2020-06-18 21:50:16 +01:00
from django.urls import reverse
2020-05-24 01:57:06 +01:00
2021-04-13 00:47:17 +01:00
from troggle.core.models.caves import Cave, Entrance
2020-05-13 23:11:47 +01:00
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def import_auto_logbooks():
'''For logbook entries which have been 'typed up' by entering the data in a form,
which makes a copy of the LBE in years/<year>/autologbook/<tripname>.html
this will then re-import all thoise individual LBE files.
Gosh. How complicated. Thank goodness we don't do anything like this anymore.
'''
2020-05-13 23:11:47 +01:00
import os
2020-05-24 01:57:06 +01:00
import troggle.parsers.logbooks
2020-05-13 23:11:47 +01:00
for pt in troggle.core.models.PersonTrip.objects.all():
pt.delete()
for lbe in troggle.core.models.LogbookEntry.objects.all():
lbe.delete()
for expedition in troggle.core.models.Expedition.objects.all():
directory = os.path.join(settings.EXPOWEB,
"years",
expedition.year,
"autologbook")
for root, dirs, filenames in os.walk(directory):
for filename in filenames:
2020-05-24 01:57:06 +01:00
print((os.path.join(root, filename)))
2020-05-13 23:11:47 +01:00
parsers.logbooks.parseAutoLogBookEntry(os.path.join(root, filename))
2020-07-18 16:23:54 +01:00
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2020-05-13 23:11:47 +01:00
#Temporary function until definitive source of data transfered.
from django.template.defaultfilters import slugify
from django.template import loader
2020-05-13 23:11:47 +01:00
def dumplogbooks():
'''This appears to take all the LBEs in the database and to write them all out as infividual html files
so that they can be re-imported.
This is the sort of silly thing you have to do when you started out thinking that the database was
going to be the Source Of All Truth and then retrofitting to make inthe input files be the master.
'''
2020-05-13 23:11:47 +01:00
def get_name(pe):
if pe.nickname:
return pe.nickname
else:
return pe.person.first_name
for lbe in troggle.core.models.LogbookEntry.objects.all():
dateStr = lbe.date.strftime("%Y-%m-%d")
directory = os.path.join(settings.EXPOWEB,
"years",
lbe.expedition.year,
"autologbook")
if not os.path.isdir(directory):
os.mkdir(directory)
filename = os.path.join(directory,
dateStr + "." + slugify(lbe.title)[:50] + ".html")
if lbe.cave:
2020-05-24 01:57:06 +01:00
print((lbe.cave.reference()))
2020-05-13 23:11:47 +01:00
trip = {"title": lbe.title, "html":lbe.text, "cave": lbe.cave.reference(), "caveOrLocation": "cave"}
else:
trip = {"title": lbe.title, "html":lbe.text, "location":lbe.place, "caveOrLocation": "location"}
pts = [pt for pt in lbe.persontrip_set.all() if pt.personexpedition]
persons = [{"name": get_name(pt.personexpedition), "TU": pt.time_underground, "author": pt.is_logbook_entry_author} for pt in pts]
f = open(filename, "wb")
template = loader.get_template('dataformat/logbookentry.html')
context = {'trip': trip,
2020-05-13 23:11:47 +01:00
'persons': persons,
'date': dateStr,
'expeditionyear': lbe.expedition.year}
2020-05-13 23:11:47 +01:00
output = template.render(context)
2020-05-24 01:57:06 +01:00
f.write(str(output).encode( "utf-8" ))
2020-05-13 23:11:47 +01:00
f.close()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -