2020-05-28 02:20:50 +01:00
|
|
|
import re
|
|
|
|
|
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
|
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
|
|
|
|
|
2020-06-16 16:07:36 +01:00
|
|
|
import troggle.parsers.imports
|
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
|
2020-06-04 23:00:58 +01:00
|
|
|
from troggle.core.forms import UploadFileForm
|
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).
|
|
|
|
|
|
|
|
Also has code to download a logbook in a choice of formats (why?!) and to
|
|
|
|
download all QMs (not working)
|
2021-04-21 19:08:42 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
todo = '''Delete the newfile & TripForm code once we have a proper file-upload system working.
|
|
|
|
meanwhile keep it as an example to consider.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def todos(request, module):
|
|
|
|
'''produces todo text from module
|
|
|
|
'''
|
|
|
|
from troggle.core.TESTS.tests import todo as tests
|
|
|
|
from troggle.core.views.logbooks import todo as viewlogbooks
|
|
|
|
from troggle.core.forms import todo as forms
|
|
|
|
print(f'TODO - {tests}')
|
|
|
|
tododict = {'views/other': todo,
|
|
|
|
'tests': tests,
|
|
|
|
'views/logbooks': viewlogbooks,
|
|
|
|
'core/forms': forms}
|
|
|
|
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-21 19:08:42 +01:00
|
|
|
def controlpanel(request):
|
2011-07-11 02:10:22 +01:00
|
|
|
jobs_completed=[]
|
|
|
|
if request.method=='POST':
|
2021-04-07 21:53:17 +01:00
|
|
|
if request.user.is_superuser: # expoadmin is both .is_staff and ._is_superuser
|
2020-06-16 16:07:36 +01:00
|
|
|
# NONE of this works now that databaseReset (now parsers.imports) has been so extensively rewritten
|
|
|
|
reinit_db()
|
|
|
|
import_caves()
|
|
|
|
import_people()
|
|
|
|
import_surveyscans()
|
|
|
|
import_logbooks()
|
|
|
|
import_QMs()
|
|
|
|
import_tunnelfiles()
|
|
|
|
import_survexblks()
|
|
|
|
import_survexpos()
|
2011-07-11 02:10:22 +01:00
|
|
|
else:
|
2021-04-07 21:53:17 +01:00
|
|
|
if request.user.is_authenticated: #The user is logged in, but is not a superuser.
|
2019-03-30 17:02:07 +00:00
|
|
|
return render(request,'controlPanel.html', {'caves':Cave.objects.all(),'error':'You must be a superuser to use that feature.'})
|
2011-07-11 02:10:22 +01:00
|
|
|
else:
|
|
|
|
return HttpResponseRedirect(reverse('auth_login'))
|
|
|
|
|
2019-03-30 17:02:07 +00:00
|
|
|
return render(request,'controlPanel.html', {'caves':Cave.objects.all(),'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-21 19:08:42 +01:00
|
|
|
def downloadlogbook(request,year=None,extension=None,queryset=None):
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
if year:
|
2019-02-24 13:03:34 +00:00
|
|
|
current_expedition=Expedition.objects.get(year=year)
|
|
|
|
logbook_entries=LogbookEntry.objects.filter(expedition=current_expedition)
|
2011-07-11 02:10:22 +01:00
|
|
|
filename='logbook'+year
|
|
|
|
elif queryset:
|
|
|
|
logbook_entries=queryset
|
|
|
|
filename='logbook'
|
|
|
|
else:
|
2019-03-30 13:58:38 +00:00
|
|
|
response = HttpResponse(content_type='text/plain')
|
2011-07-11 02:10:22 +01:00
|
|
|
return response(r"Error: Logbook downloader doesn't know what year you want")
|
|
|
|
|
|
|
|
if 'year' in request.GET:
|
|
|
|
year=request.GET['year']
|
|
|
|
if 'extension' in request.GET:
|
|
|
|
extension=request.GET['extension']
|
2019-03-30 13:58:38 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
if extension =='txt':
|
2019-03-30 13:58:38 +00:00
|
|
|
response = HttpResponse(content_type='text/plain')
|
2011-07-11 02:10:22 +01:00
|
|
|
style='2008'
|
|
|
|
elif extension == 'html':
|
2019-03-30 13:58:38 +00:00
|
|
|
response = HttpResponse(content_type='text/html')
|
2011-07-11 02:10:22 +01:00
|
|
|
style='2005'
|
|
|
|
|
|
|
|
template='logbook'+style+'style.'+extension
|
|
|
|
response['Content-Disposition'] = 'attachment; filename='+filename+'.'+extension
|
|
|
|
t=loader.get_template(template)
|
|
|
|
c=Context({'logbook_entries':logbook_entries})
|
|
|
|
response.write(t.render(c))
|
|
|
|
return response
|
2020-07-20 18:31:50 +01:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
def downloadQMs(request):
|
|
|
|
# Note to self: use get_cave method for the below
|
|
|
|
if request.method=='GET':
|
|
|
|
try:
|
|
|
|
cave=Cave.objects.get(kataster_number=request.GET['cave_id'])
|
|
|
|
except Cave.DoesNotExist:
|
2019-03-30 13:58:38 +00:00
|
|
|
cave=Cave.objects.get(name=request.GET['cave_id'])
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
from export import toqms
|
|
|
|
|
2019-03-30 13:58:38 +00:00
|
|
|
response = HttpResponse(content_type='text/csv')
|
2011-07-11 02:10:22 +01:00
|
|
|
response['Content-Disposition'] = 'attachment; filename=qm.csv'
|
|
|
|
toqms.writeQmTable(response,cave)
|
|
|
|
return response
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def eyecandy(request):
|
|
|
|
return
|
|
|
|
|
|
|
|
def ajax_QM_number(request):
|
2020-06-04 23:00:58 +01:00
|
|
|
res=""
|
2011-07-11 02:10:22 +01:00
|
|
|
if request.method=='POST':
|
|
|
|
cave=Cave.objects.get(id=request.POST['cave'])
|
2019-03-30 13:58:38 +00:00
|
|
|
print(cave)
|
2011-07-11 02:10:22 +01:00
|
|
|
exp=Expedition.objects.get(pk=request.POST['year'])
|
2019-03-30 13:58:38 +00:00
|
|
|
print(exp)
|
2011-07-11 02:10:22 +01:00
|
|
|
res=cave.new_QM_number(exp.year)
|
|
|
|
|
|
|
|
return HttpResponse(res)
|
|
|
|
|
|
|
|
@login_required_if_public
|
2021-04-21 19:08:42 +01:00
|
|
|
def newfile(request, pslug = None):
|
2021-04-20 22:58:41 +01:00
|
|
|
''' not known quite what this was for or where it fits in - original 2006 troggle idea never finished?
|
|
|
|
'''
|
2020-06-04 23:00:58 +01:00
|
|
|
if pslug:
|
|
|
|
previousfile = LogbookEntry.objects.get(slug = pslug, date = previousdate, expedition = expedition)
|
2011-07-11 02:10:22 +01:00
|
|
|
if request.method == 'POST': # If the form has been submitted...
|
|
|
|
tripForm = TripForm(request.POST) # A form bound to the POST data
|
2020-06-04 23:00:58 +01:00
|
|
|
personTripFormSet = PersonTripFormSet(request.POST)
|
|
|
|
if tripForm.is_valid() and personTripFormSet.is_valid(): # All validation rules pass
|
|
|
|
dateStr = tripForm.cleaned_data["date"].strftime("%Y-%m-%d")
|
|
|
|
directory = os.path.join(settings.EXPOWEB,
|
|
|
|
"years",
|
|
|
|
expedition.year,
|
|
|
|
"autologbook")
|
|
|
|
filename = os.path.join(directory,
|
|
|
|
dateStr + "." + slugify(tripForm.cleaned_data["title"])[:50] + ".html")
|
|
|
|
if not os.path.isdir(directory):
|
|
|
|
os.mkdir(directory)
|
|
|
|
if pslug and pdate:
|
|
|
|
delLogbookEntry(previouslbe)
|
|
|
|
f = open(filename, "w")
|
|
|
|
template = loader.get_template('dataformat/logbookentry.html')
|
|
|
|
context = Context({'trip': tripForm.cleaned_data,
|
|
|
|
'persons': personTripFormSet.cleaned_data,
|
|
|
|
'date': dateStr,
|
|
|
|
'expeditionyear': expeditionyear})
|
|
|
|
f.write(template.render(context))
|
|
|
|
f.close()
|
|
|
|
print(logbookparsers.parseAutoLogBookEntry(filename))
|
|
|
|
return HttpResponseRedirect(reverse('expedition', args=[expedition.year])) # Redirect after POST
|
2011-07-11 02:10:22 +01:00
|
|
|
else:
|
|
|
|
if pslug:
|
2020-06-04 23:00:58 +01:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
fileform = UploadFileForm() # An unbound form
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2021-03-29 02:06:19 +01:00
|
|
|
return render(request, 'editfile.html', {'fileForm': fileform, })
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2021-04-20 22:58:41 +01:00
|
|
|
|