2021-04-22 02:45:28 +01:00
|
|
|
import re, os
|
2021-04-28 02:43:09 +01:00
|
|
|
from pathlib import Path
|
2020-05-28 02:20:50 +01:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
from django import forms
|
2021-04-30 18:02:05 +01:00
|
|
|
|
2020-05-28 02:20:50 +01:00
|
|
|
from django.conf import settings
|
2020-06-18 21:50:16 +01:00
|
|
|
from django.urls import reverse
|
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
|
2019-03-30 17:02:07 +00:00
|
|
|
from django.shortcuts import render
|
2020-05-28 02:20:50 +01:00
|
|
|
from django.template import Context, loader
|
2021-04-22 02:45:28 +01:00
|
|
|
from django.core.files.storage import FileSystemStorage, default_storage
|
2020-05-28 02:20:50 +01:00
|
|
|
|
2021-04-27 20:44:24 +01:00
|
|
|
from troggle.parsers.imports import import_caves, import_people, import_surveyscans
|
|
|
|
from troggle.parsers.imports import import_logbooks, import_QMs, import_drawingsfiles, import_survex
|
|
|
|
# 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
|
2021-04-13 00:47:17 +01:00
|
|
|
from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip
|
2021-04-02 15:51:14 +01:00
|
|
|
from .login 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-27 15:38:20 +01:00
|
|
|
Also has code to download a logbook in a choice of formats (why?!)
|
2021-04-21 19:08:42 +01:00
|
|
|
'''
|
|
|
|
|
2021-04-23 11:43:25 +01:00
|
|
|
todo = '''
|
|
|
|
- Check that the logbookdownloader works by testing with a round trip.
|
2021-04-21 22:09:20 +01:00
|
|
|
|
2021-04-23 11:43:25 +01:00
|
|
|
- Use it to convert all older logbooks into the 2005-variant of HTML then we can
|
|
|
|
get rid of the parsers for older formats. There are no images stored in the database,
|
|
|
|
so this is only a tool for a first pass, to be followed by extensive hand-editing!
|
|
|
|
When we have done all the old logbooks, delete this function and the two templates.
|
2021-04-21 22:09:20 +01:00
|
|
|
|
2021-04-23 11:43:25 +01:00
|
|
|
- But how does this interact with troggle/logbooksdump.py ?
|
2021-04-21 19:08:42 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
def todos(request, module):
|
|
|
|
'''produces todo text from module
|
2021-04-21 22:09:20 +01:00
|
|
|
We should automate this to find all those strings automatically
|
2021-04-21 19:08:42 +01:00
|
|
|
'''
|
|
|
|
from troggle.core.TESTS.tests import todo as tests
|
|
|
|
from troggle.core.views.logbooks import todo as viewlogbooks
|
2021-04-27 00:32:01 +01:00
|
|
|
from troggle.parsers.caves import todo as parserscaves
|
2021-04-23 03:07:21 +01: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
|
|
|
|
from troggle.core.models.caves import todo as modelcaves
|
2021-04-30 23:21:38 +01:00
|
|
|
from troggle.core.middleware import todo as middleware
|
2021-04-21 19:08:42 +01:00
|
|
|
from troggle.core.forms import todo as forms
|
|
|
|
tododict = {'views/other': todo,
|
|
|
|
'tests': tests,
|
|
|
|
'views/logbooks': viewlogbooks,
|
2021-04-27 00:32:01 +01:00
|
|
|
'parsers/caves': parserscaves,
|
2021-04-23 03:07:21 +01:00
|
|
|
'parsers/logbooks': parserslogbooks,
|
2021-04-26 02:10:45 +01:00
|
|
|
'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()
|
2021-03-28 03:48:24 +01:00
|
|
|
return render(request, ('404.html', context.flatten()))
|
2021-03-25 16:15:58 +00:00
|
|
|
|
2021-03-27 18:22:07 +00:00
|
|
|
def frontpage(request):
|
2021-04-06 00:49:09 +01:00
|
|
|
'''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.
|
2021-03-27 18:22:07 +00:00
|
|
|
# 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:
|
2019-03-30 17:02:07 +00:00
|
|
|
return render(request,'tasks.html')
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
expeditions = Expedition.objects.order_by("-year")
|
|
|
|
logbookentry = LogbookEntry
|
|
|
|
cave = Cave
|
2020-07-20 18:31:50 +01:00
|
|
|
#from django.contrib.admin.templatetags import log
|
2019-03-30 17:02:07 +00:00
|
|
|
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):
|
2021-04-27 20:44:24 +01:00
|
|
|
'''This should be re-written to use ajax so that the user can see the progress
|
|
|
|
of the actions.
|
|
|
|
|
|
|
|
Also need to add reinit_db option
|
|
|
|
'''
|
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
|
|
|
|
|
|
|
|
2020-07-20 18:31:50 +01:00
|
|
|
|
2021-04-28 02:43:09 +01:00
|
|
|
def exportlogbook(request,year=None,extension=None):
|
2021-04-21 22:09:20 +01:00
|
|
|
'''Constructs, from the database, a complete HTML (or TXT) formatted logbook - but TEXT ONLY
|
|
|
|
for the current year. Formats available are HTML2005 or 2008text
|
|
|
|
|
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
|
2021-04-21 22:09:20 +01:00
|
|
|
extensive hand-editing.
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2021-04-28 02:43:09 +01:00
|
|
|
NEED TO ADD IN THE MATERIAL WHIHC IS NOT IN ANY LBE ! e.g. front matter.
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2021-04-28 02:43:09 +01:00
|
|
|
This 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':""})
|
2021-04-21 22:09:20 +01:00
|
|
|
else:
|
2021-04-28 02:43:09 +01:00
|
|
|
print(f'Logbook export {request.POST}')
|
|
|
|
|
|
|
|
if request.POST.get("year", '2016'):
|
|
|
|
year = request.POST['year']
|
|
|
|
if request.POST.get("extension", 'html'):
|
|
|
|
extension = request.POST['extension'] # e.g. html
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-04-28 02:43:09 +01:00
|
|
|
#print(f'Logbook has {len(logbook_entries)} entries in it.')
|
|
|
|
|
|
|
|
if extension =='txt':
|
|
|
|
response = HttpResponse(content_type='text/plain')
|
|
|
|
style='2008'
|
|
|
|
else :
|
|
|
|
extension == 'html'
|
|
|
|
response = HttpResponse(content_type='text/html')
|
|
|
|
style='2005'
|
|
|
|
|
|
|
|
filename='newlogbook.' + extension
|
|
|
|
template='logbook'+style+'style.'+extension
|
|
|
|
response['Content-Disposition'] = 'attachment; filename='+filename
|
|
|
|
t=loader.get_template(template)
|
|
|
|
logbookfile = (t.render({'logbook_entries':logbook_entries}))
|
|
|
|
|
|
|
|
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]})
|
2020-07-20 18:31:50 +01:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
def ajax_test(request):
|
|
|
|
post_text = request.POST['post_data']
|
|
|
|
return HttpResponse("{'response_text': '"+post_text+" recieved.'}",
|
2019-03-30 13:58:38 +00:00
|
|
|
content_type="application/json")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
|
2021-04-30 18:02:05 +01:00
|
|
|
|
|
|
|
class MyForm(forms.Form): # not a model-form
|
|
|
|
title = forms.CharField(max_length=20)
|
|
|
|
scanfiles = forms.FileField() # in MEDIA_FILES
|
|
|
|
|
2021-04-22 02:45:28 +01:00
|
|
|
@login_required_if_public
|
2021-04-30 03:44:53 +01:00
|
|
|
def scanupload(request, wallet=None):
|
|
|
|
'''Upload one scanned image file into a wallet on /expofiles
|
|
|
|
'''
|
|
|
|
filesaved = False
|
|
|
|
actual_saved = []
|
2021-04-30 21:32:53 +01:00
|
|
|
# print(f'! - FORM scanupload - start {wallet}')
|
2021-04-30 03:44:53 +01:00
|
|
|
if wallet is None:
|
|
|
|
wallet = "2021#01" # improve this later
|
|
|
|
if not re.match('(19|20)\d\d:\d\d', wallet):
|
|
|
|
wallet = "2021:01" # improve this later
|
|
|
|
|
|
|
|
year = wallet[:4]
|
|
|
|
nexty = f'{int(year)+1}'
|
|
|
|
prevy = f'{int(year)-1}'
|
|
|
|
wnumber = wallet[5:]
|
|
|
|
next = f'{int(wnumber)+1:02d}'
|
|
|
|
prev = f'{int(wnumber)-1:02d}'
|
|
|
|
|
|
|
|
if int(wnumber) == 0:
|
|
|
|
prev = f'{int(wnumber):02d}'
|
|
|
|
|
|
|
|
wallet = wallet.replace(':','#')
|
|
|
|
dirpath = Path(settings.SURVEY_SCANS, year, wallet)
|
|
|
|
|
2021-04-30 18:02:05 +01:00
|
|
|
form = MyForm()
|
|
|
|
|
2021-04-22 02:45:28 +01:00
|
|
|
if request.method == 'POST':
|
2021-04-30 18:02:05 +01:00
|
|
|
form = MyForm(request.POST,request.FILES)
|
2021-04-22 02:45:28 +01:00
|
|
|
if form.is_valid():
|
|
|
|
#form.save() # comment out so nothing saved in MEDIA_ROOT/fileuploads
|
2021-04-30 18:02:05 +01:00
|
|
|
f = request.FILES["scanfiles"]
|
2021-04-22 02:45:28 +01:00
|
|
|
w = request.POST["title"]
|
2021-04-30 18:02:05 +01:00
|
|
|
multiple = request.FILES.getlist('scanfiles')
|
2021-04-23 03:07:21 +01:00
|
|
|
fs = FileSystemStorage(os.path.join(settings.SURVEY_SCANS, year, w))
|
2021-04-22 02:45:28 +01:00
|
|
|
|
2021-04-30 03:44:53 +01:00
|
|
|
actual_saved = []
|
|
|
|
if multiple:
|
|
|
|
for f in multiple:
|
|
|
|
actual_saved.append( fs.save(f.name, content=f) )
|
2021-04-30 21:32:53 +01:00
|
|
|
# print(f'! - FORM scanupload multiple {actual_saved}')
|
2021-04-30 03:44:53 +01:00
|
|
|
filesaved = True
|
|
|
|
|
|
|
|
files = []
|
|
|
|
dirs = []
|
2021-04-30 21:32:53 +01:00
|
|
|
# print(f'! - FORM scanupload - start {wallet} {dirpath}')
|
2021-04-30 03:44:53 +01:00
|
|
|
try:
|
|
|
|
for f in dirpath.iterdir():
|
|
|
|
if f.is_dir():
|
|
|
|
dirs.append(f.name)
|
|
|
|
if f.is_file():
|
|
|
|
if f.name != 'contents.json' and f.name != 'walletindex.html':
|
|
|
|
files.append(f.name)
|
|
|
|
except FileNotFoundError:
|
|
|
|
files.append('(no wallet yet - would be created)')
|
|
|
|
if len(files) ==0 :
|
|
|
|
files.append('(no image files in wallet)')
|
|
|
|
|
2021-04-30 03:52:30 +01:00
|
|
|
|
|
|
|
return render(request, 'scanuploadform.html',
|
|
|
|
{'form': form, 'wallet': wallet, 'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved})
|