troggle-unchained/core/views/other.py

217 lines
9.0 KiB
Python
Raw Normal View History

2023-01-19 18:35:56 +00:00
import os
import re
import subprocess
from pathlib import Path
2020-05-28 02:20:50 +01:00
2011-07-11 02:10:22 +01:00
from django import forms
2020-05-28 02:20:50 +01:00
from django.conf import settings
2023-01-19 18:35:56 +00:00
from django.core.files.storage import FileSystemStorage, default_storage
2011-07-11 02:10:22 +01:00
from django.db.models import Q
2020-05-28 02:20:50 +01:00
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
2020-05-28 02:20:50 +01:00
from django.template import Context, loader
2023-01-19 18:35:56 +00:00
from django.urls import reverse
2020-05-28 02:20:50 +01:00
2023-01-29 16:47:46 +00:00
from troggle.core.models.caves import Cave
from troggle.core.models.logbooks import QM, LogbookEntry, PersonTrip
2023-01-19 18:35:56 +00:00
from troggle.core.models.survex import DrawingFile
2021-04-27 20:44:24 +01:00
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
2021-04-13 00:43:57 +01:00
from troggle.core.models.troggle import Expedition, Person, PersonExpedition
2023-01-19 18:35:56 +00:00
from troggle.parsers.imports import (import_caves, import_drawingsfiles,
import_logbooks, import_people,
import_QMs, import_survex,
import_surveyscans)
2021-05-03 20:35:35 +01:00
from .auth import login_required_if_public
2011-07-11 02:10:22 +01:00
2021-04-21 19:08:42 +01:00
'''Utility functions and code to serve the control panel and individual user's
2020-07-20 22:53:26 +01:00
progress and task list (deprecated as we do not have individual user login).
2021-04-21 19:08:42 +01:00
'''
todo = '''
- Use logbookdownloader to convert all older logbooks into the 2005-variant of HTML then we can
get rid of the parsers for older formats.
When we have done all the old logbooks, delete this function and the two templates.
2022-12-29 13:56:46 +00:00
OR invent a new format, e.g. using <article> and <section>?, which is better!
2021-04-21 19:08:42 +01:00
'''
def todos(request, module):
'''produces todo text from module
2022-12-29 13:56:46 +00:00
We could automate this to find all those strings automatically
2021-04-21 19:08:42 +01:00
'''
2023-01-19 18:35:56 +00:00
from troggle.core.forms import todo as forms
from troggle.core.middleware import todo as middleware
from troggle.core.models.caves import todo as modelcaves
2021-04-21 19:08:42 +01:00
from troggle.core.TESTS.tests import todo as tests
2021-11-06 22:36:44 +00:00
from troggle.core.views.caves import todo as viewcaves
2021-05-04 20:56:44 +01:00
from troggle.core.views.drawings import todo as viewdrawings
2023-01-19 18:35:56 +00:00
from troggle.core.views.logbooks import todo as viewlogbooks
from troggle.core.views.survex import todo as viewsurvex
2021-04-27 00:32:01 +01:00
from troggle.parsers.caves import todo as parserscaves
2021-05-04 20:56:44 +01:00
from troggle.parsers.drawings import todo as parsersdrawings
2023-01-19 18:35:56 +00:00
from troggle.parsers.logbooks import todo as parserslogbooks
from troggle.parsers.survex import todo as parserssurvex
2021-04-21 19:08:42 +01:00
tododict = {'views/other': todo,
'tests': tests,
'views/logbooks': viewlogbooks,
2021-05-02 22:48:25 +01:00
'views/survex': viewsurvex,
2021-11-06 22:36:44 +00:00
'views/caves': viewcaves,
2021-05-04 20:56:44 +01:00
'views/drawings': viewdrawings,
2021-04-27 00:32:01 +01:00
'parsers/caves': parserscaves,
'parsers/logbooks': parserslogbooks,
2021-05-04 20:56:44 +01:00
'parsers/drawings': parsersdrawings,
'parsers/survex': parserssurvex,
'core/models/caves': modelcaves,
2021-04-30 23:21:38 +01:00
'core/middleware': middleware,
2021-04-30 22:44:03 +01:00
'core/forms': forms}
2021-04-21 19:08:42 +01:00
return render(request,'core/todos.html', {'tododict': tododict})
2020-07-20 22:53:26 +01:00
2021-03-25 16:15:58 +00:00
def troggle404(request): # cannot get this to work. Handler404 in urls.py not right syntax
'''Custom 404 page to be used even when Debug=True
https://blog.juanwolf.fr/posts/programming/how-to-create-404-page-django/
'''
context = RequestContext(request)
#context['caves'] = Cave.objects.all()
2022-12-29 13:56:46 +00:00
return render(request, ('errors/generic.html', context.flatten()))
2021-03-25 16:15:58 +00:00
def frontpage(request):
'''never seen in common practice. Logon should redirect here when this is more useful'''
# the messages system does a popup on this page if there is a recent message, e.g. from the admin site actions.
# via django.contrib.messages.middleware.MessageMiddleware
# this is set in the templates.
2021-04-07 21:53:17 +01:00
if request.user.is_authenticated:
return render(request,'tasks.html')
2011-07-11 02:10:22 +01:00
expeditions = Expedition.objects.order_by("-year")
logbookentry = LogbookEntry
cave = Cave
#from django.contrib.admin.templatetags import log
return render(request,'frontpage.html', locals())
2011-07-11 02:10:22 +01:00
2021-04-27 20:44:24 +01:00
@login_required_if_public
2021-04-21 19:08:42 +01:00
def controlpanel(request):
2022-12-29 13:56:46 +00:00
'''Admin requires expoadmin user logged on
Mostly disabled apart from logbook export
DANGEROUS, these import functions kill the ground under your feet !
2021-04-27 20:44:24 +01:00
'''
2011-07-11 02:10:22 +01:00
jobs_completed=[]
2021-04-27 20:44:24 +01:00
def process_imports():
'''databaseReset.py
jq.enq("reinit",reinit_db)
jq.enq("caves",import_caves)
jq.enq("people",import_people)
jq.enq("scans",import_surveyscans)
jq.enq("logbooks",import_logbooks)
jq.enq("QMs",import_QMs)
jq.enq("drawings",import_drawingsfiles)
jq.enq("survex",import_survex)
'''
if request.POST.get("import_caves", False):
2020-06-16 16:07:36 +01:00
import_caves()
2021-04-27 20:44:24 +01:00
jobs_completed.append('Caves')
if request.POST.get("import_people", False):
2020-06-16 16:07:36 +01:00
import_people()
2021-04-27 20:44:24 +01:00
jobs_completed.append('People')
if request.POST.get("import_surveyscans", False):
2020-06-16 16:07:36 +01:00
import_surveyscans()
2021-04-27 20:44:24 +01:00
jobs_completed.append('Scans')
if request.POST.get("import_logbooks", False):
2020-06-16 16:07:36 +01:00
import_logbooks()
2021-04-27 20:44:24 +01:00
jobs_completed.append('Logbooks')
if request.POST.get("import_QMs", False):
2020-06-16 16:07:36 +01:00
import_QMs()
2021-04-27 20:44:24 +01:00
jobs_completed.append('QMs')
if request.POST.get("import_drawingsfiles", False):
import_drawingsfiles()
jobs_completed.append('Drawings')
if request.POST.get("import_survex", False):
import_survex()
jobs_completed.append('Survex')
print("", flush=True)
if not request.user.is_superuser: # expoadmin is both .is_staff and ._is_superuser
return render(request,'controlPanel.html', {'error': 'You are logged in, but not logged in as "expoadmin". \nLogout and login again to contnue.'})
else:
if request.method=='POST':
#reinit_db()
process_imports()
return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':jobs_completed})
2011-07-11 02:10:22 +01:00
else:
2021-04-27 20:44:24 +01:00
return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':jobs_completed})
2011-07-11 02:10:22 +01:00
def exportlogbook(request,year=None,extension=None):
'''Constructs, from the database, a complete HTML formatted logbook
for the current year. Formats available are HTML2005 (others old & broken or not written yet)
2021-04-27 15:38:20 +01:00
There are no images stored in the database, so this is only a tool for a first pass, to be followed by
hand-editing. However links to images work in the HTML text of a logbook entry
2011-07-11 02:10:22 +01:00
NEED TO ADD IN THE MATERIAL WHICH IS NOT IN ANY LBE ! e.g. front matter.
2021-12-30 00:55:42 +00:00
This function is the recipient of the POST action os the export form in the control panel
'''
def lbeKey(lbe):
"""This function goes into a lexicogrpahic sort function
"""
return str(lbe.date)
if not request.method=='POST':
return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':""})
else:
print(f'Logbook export {request.POST}')
year = request.POST['year']
current_expedition=Expedition.objects.get(year=year)
logbook_entries=LogbookEntry.objects.filter(expedition=current_expedition).order_by('date') # need to be sorted by date!
2011-07-11 02:10:22 +01:00
print(f'Logbook has {len(logbook_entries)} entries in it.')
extension ='html'
response = HttpResponse(content_type='text/html')
style='2005'
filename='logbook-new-format.' + extension
template='logbook'+style+'style.'+ extension
response['Content-Disposition'] = 'attachment; filename='+filename
t=loader.get_template(template)
logbookfile = (t.render({'logbook_entries':logbook_entries}))
2022-12-21 02:05:26 +00:00
endpath = Path(settings.EXPOWEB, "years", year, "endmatter.html")
endmatter = ""
if endpath.is_file():
try:
with open(endpath,"r") as end:
endmatter = end.read()
except:
print(" ! Very Bad Error opening " + endpath)
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)
2022-12-21 02:05:26 +00:00
logbookfile = re.sub(r"<body>", "<body>\n"+frontmatter+endmatter , logbookfile)
else:
2022-12-21 02:05:26 +00:00
logbookfile = re.sub(r"<body>", f"<body>\n<h1>Expo {year}</h1>\n"+endmatter, logbookfile)
dir = Path(settings.EXPOWEB) / "years" / year
filepath = Path(dir, filename)
with(open(filepath, 'w')) as lb:
lb.writelines(logbookfile)
#print(f'Logbook exported to {filepath}')
completed = f'Logbook exported to <a href="/years/{filename}">{filename}</a>'
return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':[completed]})