2023-01-19 18:35:56 +00:00
|
|
|
import re
|
2021-04-28 02:43:09 +01:00
|
|
|
from pathlib import Path
|
2020-05-28 02:20:50 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2023-01-30 23:04:11 +00:00
|
|
|
from django.http import HttpResponse
|
2019-03-30 17:02:07 +00:00
|
|
|
from django.shortcuts import render
|
2023-01-30 23:04:11 +00:00
|
|
|
from django.template import loader
|
2020-05-28 02:20:50 +01:00
|
|
|
|
2023-01-29 16:47:46 +00:00
|
|
|
from troggle.core.models.caves import Cave
|
2023-08-31 16:55:20 +01:00
|
|
|
from troggle.core.models.logbooks import LogbookEntry, writelogbook # , PersonLogEntry
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2021-04-27 20:44:24 +01:00
|
|
|
# from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time*
|
2023-01-30 23:04:11 +00:00
|
|
|
from troggle.core.models.troggle import Expedition
|
2023-01-30 19:04:36 +00:00
|
|
|
from troggle.parsers.imports import (
|
|
|
|
import_caves,
|
|
|
|
import_drawingsfiles,
|
|
|
|
import_logbooks,
|
|
|
|
import_people,
|
|
|
|
import_QMs,
|
|
|
|
import_survex,
|
|
|
|
import_surveyscans,
|
|
|
|
)
|
2023-01-19 18:35:56 +00:00
|
|
|
|
2021-05-03 20:35:35 +01:00
|
|
|
from .auth import login_required_if_public
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2023-01-30 19:04:36 +00: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).
|
2023-01-30 19:04:36 +00:00
|
|
|
"""
|
2021-04-21 19:08:42 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
todo = """
|
2023-02-02 21:50:40 +00:00
|
|
|
- [Low priority] Fix Login page so that it produces the frontpage or
|
|
|
|
redirects to the page which produced the login prompt requirement.
|
2023-01-30 19:04:36 +00:00
|
|
|
"""
|
|
|
|
|
2021-04-21 19:08:42 +01:00
|
|
|
|
2023-01-30 19:04:36 +00: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
|
2023-01-30 19:04:36 +00:00
|
|
|
"""
|
2023-02-02 16:15:13 +00:00
|
|
|
from troggle.core.TESTS.tests import todo as tests
|
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
|
2023-02-02 16:15:13 +00:00
|
|
|
from troggle.core.models.logbooks import todo as modellogbooks
|
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
|
2023-02-02 16:15:13 +00:00
|
|
|
from troggle.core.views.other import todo as viewother
|
2023-01-19 18:35:56 +00:00
|
|
|
from troggle.core.views.survex import todo as viewsurvex
|
2023-02-02 16:15:13 +00:00
|
|
|
from troggle.core.views.uploads import todo as viewuploads
|
|
|
|
from troggle.core.views.wallets_edit import todo as viewwallets_edit
|
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-02-02 16:15:13 +00:00
|
|
|
from troggle.parsers.locations import todo as parserslocations
|
2023-01-19 18:35:56 +00:00
|
|
|
from troggle.parsers.logbooks import todo as parserslogbooks
|
2021-04-26 02:10:45 +01:00
|
|
|
from troggle.parsers.survex import todo as parserssurvex
|
2023-02-02 16:15:13 +00:00
|
|
|
from troggle.urls import todo as todourls
|
2023-01-30 19:04:36 +00:00
|
|
|
|
|
|
|
tododict = {
|
|
|
|
"tests": tests,
|
2023-02-02 16:15:13 +00:00
|
|
|
"forms": forms,
|
|
|
|
"middleware": middleware,
|
|
|
|
"models/caves": modelcaves,
|
|
|
|
"models/logbooks": modellogbooks,
|
2023-01-30 19:04:36 +00:00
|
|
|
"views/caves": viewcaves,
|
|
|
|
"views/drawings": viewdrawings,
|
2023-02-02 16:15:13 +00:00
|
|
|
"views/logbooks": viewlogbooks,
|
|
|
|
"views/other": todo,
|
|
|
|
"views/survex": viewsurvex,
|
|
|
|
"views/uploads": viewuploads,
|
|
|
|
"views/wallets_edit": viewwallets_edit,
|
2023-01-30 19:04:36 +00:00
|
|
|
"parsers/caves": parserscaves,
|
|
|
|
"parsers/drawings": parsersdrawings,
|
2023-02-02 16:15:13 +00:00
|
|
|
"parsers/locations": parserslocations,
|
|
|
|
"parsers/logbooks": parserslogbooks,
|
2023-01-30 19:04:36 +00:00
|
|
|
"parsers/survex": parserssurvex,
|
2023-02-02 16:15:13 +00:00
|
|
|
"urls": todourls,
|
2023-01-30 19:04:36 +00:00
|
|
|
}
|
|
|
|
return render(request, "core/todos.html", {"tododict": tododict})
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-03-25 16:15:58 +00:00
|
|
|
https://blog.juanwolf.fr/posts/programming/how-to-create-404-page-django/
|
2023-01-30 19:04:36 +00:00
|
|
|
"""
|
2021-03-25 16:15:58 +00:00
|
|
|
context = RequestContext(request)
|
2023-01-30 19:04:36 +00:00
|
|
|
# context['caves'] = Cave.objects.all()
|
|
|
|
return render(request, ("errors/generic.html", context.flatten()))
|
2021-03-25 16:15:58 +00:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
|
|
|
def frontpage(request):
|
|
|
|
"""never seen in common practice. Logon should redirect here when this is more useful"""
|
2021-04-06 00:49:09 +01:00
|
|
|
# the messages system does a popup on this page if there is a recent message, e.g. from the admin site actions.
|
2023-01-30 19:04:36 +00:00
|
|
|
# via django.contrib.messages.middleware.MessageMiddleware
|
2021-03-27 18:22:07 +00:00
|
|
|
# this is set in the templates.
|
2021-04-07 21:53:17 +01:00
|
|
|
if request.user.is_authenticated:
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(request, "tasks.html")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
expeditions = Expedition.objects.order_by("-year")
|
2011-07-11 02:10:22 +01:00
|
|
|
logbookentry = LogbookEntry
|
|
|
|
cave = Cave
|
2023-01-30 19:04:36 +00:00
|
|
|
# from django.contrib.admin.templatetags import log
|
|
|
|
return render(request, "frontpage.html", locals())
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2020-07-20 18:31:50 +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):
|
2023-01-30 19:04:36 +00:00
|
|
|
"""Admin requires expoadmin user logged on
|
2022-12-29 13:56:46 +00:00
|
|
|
Mostly disabled apart from logbook export
|
|
|
|
DANGEROUS, these import functions kill the ground under your feet !
|
2023-01-30 19:04:36 +00:00
|
|
|
"""
|
|
|
|
jobs_completed = []
|
|
|
|
|
2021-04-27 20:44:24 +01:00
|
|
|
def process_imports():
|
2023-01-30 19:04:36 +00:00
|
|
|
"""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)
|
|
|
|
"""
|
2021-04-27 20:44:24 +01:00
|
|
|
if request.POST.get("import_caves", False):
|
2020-06-16 16:07:36 +01:00
|
|
|
import_caves()
|
2023-01-30 19:04:36 +00:00
|
|
|
jobs_completed.append("Caves")
|
2021-04-27 20:44:24 +01:00
|
|
|
if request.POST.get("import_people", False):
|
2020-06-16 16:07:36 +01:00
|
|
|
import_people()
|
2023-01-30 19:04:36 +00:00
|
|
|
jobs_completed.append("People")
|
2021-04-27 20:44:24 +01:00
|
|
|
if request.POST.get("import_surveyscans", False):
|
2020-06-16 16:07:36 +01:00
|
|
|
import_surveyscans()
|
2023-01-30 19:04:36 +00:00
|
|
|
jobs_completed.append("Scans")
|
2021-04-27 20:44:24 +01:00
|
|
|
if request.POST.get("import_logbooks", False):
|
2020-06-16 16:07:36 +01:00
|
|
|
import_logbooks()
|
2023-01-30 19:04:36 +00:00
|
|
|
jobs_completed.append("Logbooks")
|
2021-04-27 20:44:24 +01:00
|
|
|
if request.POST.get("import_QMs", False):
|
2020-06-16 16:07:36 +01:00
|
|
|
import_QMs()
|
2023-01-30 19:04:36 +00:00
|
|
|
jobs_completed.append("QMs")
|
2021-04-27 20:44:24 +01:00
|
|
|
if request.POST.get("import_drawingsfiles", False):
|
|
|
|
import_drawingsfiles()
|
2023-01-30 19:04:36 +00:00
|
|
|
jobs_completed.append("Drawings")
|
2021-04-27 20:44:24 +01:00
|
|
|
if request.POST.get("import_survex", False):
|
|
|
|
import_survex()
|
2023-01-30 19:04:36 +00:00
|
|
|
jobs_completed.append("Survex")
|
|
|
|
|
2021-04-27 20:44:24 +01:00
|
|
|
print("", flush=True)
|
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
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.'},
|
|
|
|
)
|
2021-04-27 20:44:24 +01:00
|
|
|
else:
|
2023-01-30 19:04:36 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
# reinit_db()
|
2021-04-27 20:44:24 +01:00
|
|
|
process_imports()
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"controlPanel.html",
|
|
|
|
{"expeditions": Expedition.objects.all(), "jobs_completed": jobs_completed},
|
|
|
|
)
|
2011-07-11 02:10:22 +01:00
|
|
|
else:
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"controlPanel.html",
|
|
|
|
{"expeditions": Expedition.objects.all(), "jobs_completed": jobs_completed},
|
|
|
|
)
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2020-07-20 18:31:50 +01:00
|
|
|
|
2023-08-31 16:55:20 +01:00
|
|
|
def exportlogbook(request, year=None):
|
2023-01-30 19:04:36 +00:00
|
|
|
"""Constructs, from the database, a complete HTML formatted logbook
|
2023-08-31 16:55:20 +01:00
|
|
|
for the current year. Format available is now just HTML2005.
|
|
|
|
Other formats have been retired.
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-02-02 21:50:40 +00:00
|
|
|
There are no images stored in the database. However links to images work in the HTML text of a logbook entry.
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2023-08-31 16:55:20 +01:00
|
|
|
This function is the recipient of the POST action as the export form in the control panel
|
2023-01-30 19:04:36 +00:00
|
|
|
"""
|
|
|
|
|
2021-04-28 02:43:09 +01:00
|
|
|
def lbeKey(lbe):
|
2023-08-31 16:55:20 +01:00
|
|
|
"""This function goes into a lexicographic sort function - but where?!"""
|
|
|
|
return str(lbe.slug) # now that slugs are tripid such as 2023-07-30b
|
2021-04-28 02:43:09 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
if not request.method == "POST":
|
|
|
|
return render(request, "controlPanel.html", {"expeditions": Expedition.objects.all(), "jobs_completed": ""})
|
2021-04-21 22:09:20 +01:00
|
|
|
else:
|
2023-08-31 16:55:20 +01:00
|
|
|
# print(f"Logbook export {request.POST}")
|
2023-01-30 19:04:36 +00:00
|
|
|
|
|
|
|
year = request.POST["year"]
|
2023-08-31 16:55:20 +01:00
|
|
|
filename = "logbook-new-format.html"
|
2021-04-28 02:43:09 +01:00
|
|
|
|
2023-08-31 16:55:20 +01:00
|
|
|
writelogbook(year, filename)
|
|
|
|
#response = HttpResponse(content_type="text/html")
|
|
|
|
#response["Content-Disposition"] = "attachment; filename=" + filename
|
2021-04-28 02:43:09 +01:00
|
|
|
completed = f'Logbook exported to <a href="/years/{filename}">{filename}</a>'
|
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
return render(
|
|
|
|
request, "controlPanel.html", {"expeditions": Expedition.objects.all(), "jobs_completed": [completed]}
|
|
|
|
)
|