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
|
import os, stat
import re
import datetime
from pathlib import Path
from urllib.parse import urljoin, unquote as urlunquote
from urllib.request import urlopen
from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponse
from troggle.core.models.survex import Wallet, SingleScan, SurvexBlock
from troggle.core.models.caves import GetCaveLookup
from troggle.core.views.expo import getmimetype
#import parsers.surveys
'''one of these views serves files as binary blobs, and simply set the mime type based on the file extension,
as does the urls.py dispatcher which sends them here. Here they should actually have the filetype checked
by looking inside the file before being served.
need to check if inavlid query string is invalid, or produces multiple replies
and render a user-friendly error page.
Note that datewallet(), caveifywallet() etc do NOT save the object to the db. They are ephemeral, just for the page rendering of the
manywallets dict.
'''
def populatewallet(w):
'''Copy survex data here just for display, not permanently
'''
# {% for personrole in wallet.survexblock.survexpersonrole_set.all %}
# {% if personrole.personexpedition %}
# <a href="{{personrole.personexpedition.get_absolute_url}}">{{personrole.personname}}</a>
# {% else %}
# {{personrole.personname}}
# {% endif %}
# {% endfor %}
survexpeople = []
blocks = SurvexBlock.objects.filter(scanswallet = w)
for b in blocks:
for personrole in b.survexpersonrole_set.all():
survexpeople.append(personrole.personname)
w.people = list(set(survexpeople)) # remove duplicates
def datewallet(w, earliest):
blocks = SurvexBlock.objects.filter(scanswallet = w)
for b in blocks:
if b.date:
if b.date < earliest:
earliest = b.date
w.date = earliest
def caveifywallet(w):
blocks = SurvexBlock.objects.filter(scanswallet = w)
for b in blocks:
# NB b.cave is not populated by parser. Use b.survexfile.cave instead, or we could parse b.survexpath
if b.survexfile.cave:
w.cave = b.survexfile.cave # just gets the last one, randomly
print(w.cave)
def walletslistyear(request, year):
'''Page which displays a list of all the wallets in a specific year - TO BE WRITTEN
'''
if year < 1976 or year > 2050:
return render(request, 'errors/generic.html', {'message': 'Year out of range. Must be between 1976 and 2050'})
else:
year = str(year)
#return render(request, 'errors/generic.html', {'message': 'This page logic not implemented yet'})
earliest = datetime.datetime.now().date()
manywallets = []
wallets = Wallet.objects.all()
for w in wallets:
if year == w.year():
print(w.year(), w)
manywallets.append(w)
else:
print("NOT WANTED",year, w.year())
continue
wp = w.people()
if not wp: # an -empty list
populatewallet(w)
else:
if len(wp) == 1:
nobody = wp[0].lower()
if nobody == 'unknown' or nobody == 'nobody' or nobody == ' ':
populatewallet(w)
if not w.date():
datewallet(w, earliest)
c = w.cave()
if not c:
caveifywallet(w)
return render(request, 'yearwallets.html', { 'manywallets':manywallets, 'settings': settings, 'year': year})
def cavewallets(request, caveid):
'''Returns all the wallets for just one cave
'''
Gcavelookup = GetCaveLookup()
if caveid in Gcavelookup:
cave = Gcavelookup[caveid]
else:
return render(request,'errors/badslug.html', {'badslug': caveid})
earliest = datetime.datetime.now().date()
# remove duplication. SOrting is done in the template
wallets = set(Wallet.objects.filter(survexblock__survexfile__cave=cave)) # NB a filtered set
manywallets = list(wallets)
for w in manywallets:
wp = w.people()
if not wp: # an -empty list
populatewallet(w)
else:
if len(wp) == 1:
nobody = wp[0].lower()
if nobody == 'unknown' or nobody == 'nobody' or nobody == ' ':
populatewallet(w)
if not w.date():
datewallet(w, earliest)
c = w.cave()
if not c:
caveifywallet(w)
return render(request, 'cavewallets.html', { 'manywallets':manywallets, 'settings': settings, 'cave': cave})
def oldwallet(request, path):
'''Now called only for non-standard wallet structures for pre-2000 wallets
'''
# print([ s.walletname for s in Wallet.objects.all() ])
print(f'! - oldwallet path:{path}')
try:
wallet = Wallet.objects.get(walletname=urlunquote(path))
return render(request, 'wallet_old.html', { 'wallet':wallet, 'settings': settings })
except:
message = f'Scan folder error or not found \'{path}\' .'
return render(request, 'errors/generic.html', {'message': message})
def walletindex(request, path):
'''All years: special 'wallet' for scanned index pages
'''
# print([ s.walletname for s in Wallet.objects.all() ])
print(f'! - walletindex path:{path}')
try:
wallet = Wallet.objects.get(walletname=urlunquote(path))
return render(request, 'walletindex.html', { 'wallet':wallet, 'settings': settings })
except:
message = f'Scan folder error or not found \'{path}\' .'
return render(request, 'errors/generic.html', {'message': message})
def scansingle(request, path, file):
'''sends a single binary file to the user for display - browser decides how using mimetype
'''
try:
wallet = Wallet.objects.get(walletname=urlunquote(path))
singlescan = SingleScan.objects.get(wallet=wallet, name=file)
print(" - scansingle {}:{}:{}:".format(path, file, getmimetype(file)))
return HttpResponse(content=open(singlescan.ffile,"rb"), content_type=getmimetype(file)) # any type of image
except:
message = f'Scan folder or scan item error or not found \'{path}\' and \'{file}\'.'
return render(request, 'errors/generic.html', {'message': message})
def allscans(request):
'''Returns all the wallets in the system, we would like to use
the Django queryset SQL optimisation https://docs.djangoproject.com/en/3.2/ref/models/querysets/#prefetch-related
to get the related singlescan and survexblock objects but that requires rewriting this to do the query on those, not on
the wallets
'''
manywallets = Wallet.objects.all() # NB all of them
# manywallets = Wallet.objects.all().prefetch_related('singlescan') fails as the link is defined on 'singlescan' not on 'wallet'
return render(request, 'manywallets.html', { 'manywallets':manywallets, 'settings': settings })
|