1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
import re, os
from django import forms
from django.conf import settings
from django.urls import reverse
from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.template import Context, loader
from django.core.files.storage import FileSystemStorage, default_storage
import troggle.parsers.imports
from troggle.core.models.troggle import Expedition, Person, PersonExpedition
from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip
from .login import login_required_if_public
from troggle.core.forms import UploadFileForm, SimpleUploadFileForm
'''Utility functions and code to serve the control panel and individual user's
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)
'''
todo = '''
- Check that the logbookdownloader works by testing with a round trip.
- 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.
- But how does this interact with troggle/logbooksdump.py ?
- deleted newfile() - check on deleted UploadFileForm using the editfile.html template which is about re-submitting
a LBE aka TripReport
'''
def todos(request, module):
'''produces todo text from module
We should automate this to find all those strings automatically
'''
from troggle.core.TESTS.tests import todo as tests
from troggle.core.views.logbooks import todo as viewlogbooks
from troggle.parsers.caves import todo as parserscaves
from troggle.parsers.logbooks import todo as parserslogbooks
from troggle.parsers.survex import todo as parserssurvex
from troggle.core.models.caves import todo as modelcaves
from troggle.core.forms import todo as forms
from troggle.core.templatetags.wiki_markup import todo as wiki
tododict = {'views/other': todo,
'tests': tests,
'views/logbooks': viewlogbooks,
'parsers/caves': parserscaves,
'parsers/logbooks': parserslogbooks,
'parsers/survex': parserssurvex,
'core/models/caves': modelcaves,
'core/forms': forms,
'core/templatetags/wiki_markup': wiki}
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
https://blog.juanwolf.fr/posts/programming/how-to-create-404-page-django/
'''
context = RequestContext(request)
#context['caves'] = Cave.objects.all()
return render(request, ('404.html', context.flatten()))
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.
if request.user.is_authenticated:
return render(request,'tasks.html')
expeditions = Expedition.objects.order_by("-year")
logbookentry = LogbookEntry
cave = Cave
#from django.contrib.admin.templatetags import log
return render(request,'frontpage.html', locals())
def controlpanel(request):
jobs_completed=[]
if request.method=='POST':
if request.user.is_superuser: # expoadmin is both .is_staff and ._is_superuser
# 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_dwgfiles()
import_survexblks()
import_survexpos()
else:
if request.user.is_authenticated: #The user is logged in, but is not a superuser.
return render(request,'controlPanel.html', {'caves':Cave.objects.all(),'error':'You must be a superuser to use that feature.'})
else:
return HttpResponseRedirect(reverse('auth_login'))
return render(request,'controlPanel.html', {'caves':Cave.objects.all(),'expeditions':Expedition.objects.all(),'jobs_completed':jobs_completed})
def downloadlogbook(request,year=None,extension=None,queryset=None):
'''Constructs, from the database, a complete HTML (or TXT) formatted logbook - but TEXT ONLY
for the current year. Formats available are HTML2005 or 2008text
There are no images stored in the database, so this is only a tool for a first pass, tobe followed by
extensive hand-editing.
'''
if year:
current_expedition=Expedition.objects.get(year=year)
logbook_entries=LogbookEntry.objects.filter(expedition=current_expedition)
filename='logbook'+year
elif queryset:
logbook_entries=queryset
filename='logbook'
else:
response = HttpResponse(content_type='text/plain')
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']
if extension =='txt':
response = HttpResponse(content_type='text/plain')
style='2008'
elif extension == 'html':
response = HttpResponse(content_type='text/html')
style='2005'
else:
response = HttpResponse(content_type='text/html')
style='2005'
template='logbook'+style+'style.'+extension
response['Content-Disposition'] = 'attachment; filename='+filename+'.'+extension
t=loader.get_template(template)
c={'logbook_entries':logbook_entries}
response.write(t.render(c))
return response
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:
cave=Cave.objects.get(name=request.GET['cave_id'])
from export import toqms
response = HttpResponse(content_type='text/csv')
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.'}",
content_type="application/json")
def eyecandy(request):
return
def ajax_QM_number(request):
res=""
if request.method=='POST':
cave=Cave.objects.get(id=request.POST['cave'])
print(cave)
exp=Expedition.objects.get(pk=request.POST['year'])
print(exp)
res=cave.new_QM_number(exp.year)
return HttpResponse(res)
@login_required_if_public
def scanupload(request, year='2050'):
print(f'! - FORM scanupload - start')
if request.method == 'POST':
form = SimpleUploadFileForm(request.POST,request.FILES)
if form.is_valid():
#form.save() # comment out so nothing saved in MEDIA_ROOT/fileuploads
f = request.FILES["simplefile"]
w = request.POST["title"]
print(f'! - FORM scanupload uploaded {f.name}')
fs = FileSystemStorage(os.path.join(settings.SURVEY_SCANS, year, w))
actual_saved = fs.save(f.name, content=f) # name may chnage to avoid clash
# INSERT check if name is changed, to allow user to abort and rename - or lets do a chaecjk anyway.
print(f'! - FORM scanupload {actual_saved}')
form = SimpleUploadFileForm()
return render(request, 'scanuploadform.html', {'form': form,'filesaved': True, 'actual_saved': actual_saved})
else:
form = SimpleUploadFileForm()
return render(request, 'scanuploadform.html', {'form':form,})
|