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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
import re, os
from pathlib import Path
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
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*
from troggle.core.models.troggle import Expedition, Person, PersonExpedition
from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip
from .auth import login_required_if_public
'''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?!)
'''
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.
-Do GIT stuff for upload forms
-Move upload forms to proper file locations
'''
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.core.views.survex import todo as viewsurvex
from troggle.core.views.drawings import todo as viewdrawings
from troggle.parsers.caves import todo as parserscaves
from troggle.parsers.logbooks import todo as parserslogbooks
from troggle.parsers.drawings import todo as parsersdrawings
from troggle.parsers.survex import todo as parserssurvex
from troggle.core.models.caves import todo as modelcaves
from troggle.core.middleware import todo as middleware
from troggle.core.forms import todo as forms
tododict = {'views/other': todo,
'tests': tests,
'views/logbooks': viewlogbooks,
'views/survex': viewsurvex,
'views/drawings': viewdrawings,
'parsers/caves': parserscaves,
'parsers/logbooks': parserslogbooks,
'parsers/drawings': parsersdrawings,
'parsers/survex': parserssurvex,
'core/models/caves': modelcaves,
'core/middleware': middleware,
'core/forms': forms}
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())
@login_required_if_public
def controlpanel(request):
'''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
'''
jobs_completed=[]
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):
import_caves()
jobs_completed.append('Caves')
if request.POST.get("import_people", False):
import_people()
jobs_completed.append('People')
if request.POST.get("import_surveyscans", False):
import_surveyscans()
jobs_completed.append('Scans')
if request.POST.get("import_logbooks", False):
import_logbooks()
jobs_completed.append('Logbooks')
if request.POST.get("import_QMs", False):
import_QMs()
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})
else:
return render(request,'controlPanel.html', {'expeditions':Expedition.objects.all(),'jobs_completed':jobs_completed})
def exportlogbook(request,year=None,extension=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, to be followed by
extensive hand-editing.
NEED TO ADD IN THE MATERIAL WHIHC IS NOT IN ANY LBE ! e.g. front matter.
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':""})
else:
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!
#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]})
class FilesForm(forms.Form): # not a model-form, just a form-form
uploadfiles = forms.FileField()
@login_required_if_public
def scanupload(request, wallet=None):
'''Upload scanned image files into a wallet on /expofiles
This does NOT use a Django model linked to a Django form. Just a simple Django form.
'''
filesaved = False
actual_saved = []
# print(f'! - FORM scanupload - start {wallet}')
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]
if int(year) < 1977:
year = "1977"
if int(year) > 2050:
year = "2050"
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}'
context = {'year': year, 'prev': prev, 'next': next, 'prevy': prevy, 'nexty': nexty}
wallet = wallet.replace(':','#')
dirpath = Path(settings.SURVEY_SCANS, year, wallet)
form = FilesForm()
if request.method == 'POST':
form = FilesForm(request.POST,request.FILES)
if form.is_valid():
f = request.FILES["uploadfiles"]
multiple = request.FILES.getlist('uploadfiles')
fs = FileSystemStorage(os.path.join(settings.SURVEY_SCANS, year, wallet))
actual_saved = []
if multiple:
for f in multiple:
actual_saved.append( fs.save(f.name, content=f) )
# print(f'! - FORM scanupload multiple {actual_saved}')
filesaved = True
files = []
dirs = []
# print(f'! - FORM scanupload - start {wallet} {dirpath}')
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)')
else:
files = sorted(files)
if dirs:
dirs = sorted(dirs)
return render(request, 'scanuploadform.html',
{'form': form, 'wallet': wallet, **context, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved})
@login_required_if_public
def dwgupload(request, folder=None):
'''Upload DRAWING files (tunnel or therion) into the upload folder in :drawings:
This does NOT use a Django model linked to a Django form. Just a simple Django form.
'''
def dwgvalid(name):
if name in [ '.gitignore', '.hgignore', ]:
return False
if Path(name).suffix in ['.xml', '.th', '.th2', '', '.svg', '.jpg']:
return True
return False
filesaved = False
actual_saved = []
doesnotexist = ''
#print(f'! - FORM dwgupload - start "{folder}"')
if folder is None:
folder = "" # improve this later
dirpath = Path(settings.DRAWINGS_DATA)
urlfile = '/dwgdataraw'
urldir = '/dwgupload'
else:
dirpath = Path(settings.DRAWINGS_DATA, folder)
urlfile = Path('/dwgdataraw/') / folder
urldir = Path('/dwgupload/') / folder
form = FilesForm()
if request.method == 'POST':
form = FilesForm(request.POST,request.FILES)
if form.is_valid():
f = request.FILES["uploadfiles"]
multiple = request.FILES.getlist('uploadfiles')
fs = FileSystemStorage(os.path.join(settings.DRAWINGS_DATA, folder))
actual_saved = []
if multiple:
for f in multiple:
if dwgvalid(f.name):
actual_saved.append( fs.save(f.name, content=f) )
# print(f'! - FORM dwgupload multiple {actual_saved}')
filesaved = True
# DO GIT STUFF & load into Database too
# parsers.surveys.SetTunnelfileInfo(dwgfile) # commented out
# dwgfile.save()
files = []
dirs = []
#print(f'! - FORM dwgupload - start {folder} \n"{dirpath}" \n"{dirpath.parent}" \n"{dirpath.exists()}"')
try:
for f in dirpath.iterdir():
if f.is_dir():
if f.name not in ['.git' ]:
dirs.append(f.name)
continue
if f.is_file():
if dwgvalid(f.name):
files.append(f.name)
continue
except FileNotFoundError:
doesnotexist = True
if files:
files = sorted(files)
if dirs:
dirs = sorted(dirs)
return render(request, 'dwguploadform.html',
{'form': form, 'doesnotexist': doesnotexist, 'urlfile': urlfile, 'urldir': urldir,'folder': folder, 'files': files, 'dirs': dirs, 'filesaved': filesaved, 'actual_saved': actual_saved})
|