2021-05-03 20:36:29 +01:00
|
|
|
import os, stat
|
|
|
|
import re
|
2022-07-29 18:55:19 +01:00
|
|
|
import datetime
|
2021-05-03 20:36:29 +01:00
|
|
|
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
|
|
|
|
|
2022-07-29 18:55:19 +01:00
|
|
|
from troggle.core.models.survex import Wallet, SingleScan, SurvexBlock
|
2022-09-19 19:54:32 +01:00
|
|
|
from troggle.core.models.troggle import Person, Expedition
|
2022-07-21 08:32:11 +01:00
|
|
|
from troggle.core.models.caves import GetCaveLookup
|
2021-05-03 20:36:29 +01:00
|
|
|
from troggle.core.views.expo import getmimetype
|
2022-07-31 16:58:46 +01:00
|
|
|
#from troggle.parsers.people import GetPersonExpeditionNameLookup
|
|
|
|
|
2021-05-03 20:36:29 +01:00
|
|
|
#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.
|
2022-07-30 23:02:02 +01:00
|
|
|
|
|
|
|
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.
|
2022-08-24 17:08:08 +01:00
|
|
|
|
|
|
|
TODO
|
|
|
|
cave for a wallet - just gets the last one, randomly. SHould make this a list or many:many ideally
|
|
|
|
|
|
|
|
add this file in to the todo list thinggy.
|
2021-05-03 20:36:29 +01:00
|
|
|
'''
|
2022-07-27 21:22:46 +01:00
|
|
|
|
2022-07-29 18:55:19 +01:00
|
|
|
def populatewallet(w):
|
|
|
|
'''Copy survex data here just for display, not permanently
|
|
|
|
'''
|
|
|
|
survexpeople = []
|
|
|
|
blocks = SurvexBlock.objects.filter(scanswallet = w)
|
|
|
|
for b in blocks:
|
|
|
|
for personrole in b.survexpersonrole_set.all():
|
|
|
|
survexpeople.append(personrole.personname)
|
2022-07-31 16:58:46 +01:00
|
|
|
w.persons = list(set(survexpeople))
|
2022-07-29 18:55:19 +01:00
|
|
|
|
|
|
|
def datewallet(w, earliest):
|
2022-07-31 16:58:46 +01:00
|
|
|
first = earliest
|
2022-07-29 18:55:19 +01:00
|
|
|
blocks = SurvexBlock.objects.filter(scanswallet = w)
|
2022-07-30 23:02:02 +01:00
|
|
|
for b in blocks:
|
|
|
|
if b.date:
|
2022-07-31 16:58:46 +01:00
|
|
|
if b.date < first:
|
|
|
|
first = b.date
|
|
|
|
if first == earliest:
|
|
|
|
# no date found
|
|
|
|
w.date = None
|
|
|
|
else:
|
2022-08-30 18:46:17 +01:00
|
|
|
w.date = first.isoformat()
|
2022-07-29 18:55:19 +01:00
|
|
|
|
|
|
|
def caveifywallet(w):
|
2022-08-01 13:55:20 +01:00
|
|
|
'''Gets the cave from the list of survex files,
|
|
|
|
only selects one of them though. Only used for display.
|
|
|
|
'''
|
2022-08-24 16:28:15 +01:00
|
|
|
blocknames = []
|
2022-07-29 18:55:19 +01:00
|
|
|
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:
|
2022-08-01 13:55:20 +01:00
|
|
|
w.cave = b.survexfile.cave # just gets the last one, randomly. SHould make this a list or many:many ideally
|
|
|
|
|
2022-08-24 16:28:15 +01:00
|
|
|
if b.name:
|
|
|
|
blocknames.append(b.name)
|
|
|
|
|
|
|
|
if w.name():
|
|
|
|
w.displaynames = [w.name()]
|
|
|
|
else:
|
|
|
|
w.displaynames = blocknames
|
|
|
|
|
2022-08-01 13:55:20 +01:00
|
|
|
def fillblankpeople(w):
|
2022-08-25 15:29:57 +01:00
|
|
|
# this isn't working..? why?
|
2022-08-01 13:55:20 +01:00
|
|
|
wp = w.people()
|
|
|
|
if not wp: # an -empty list
|
|
|
|
populatewallet(w)
|
|
|
|
else:
|
2022-08-25 15:29:57 +01:00
|
|
|
w.persons = wp
|
2022-08-01 13:55:20 +01:00
|
|
|
if len(wp) == 1:
|
|
|
|
nobody = wp[0].lower()
|
|
|
|
if nobody == 'unknown' or nobody == 'nobody' or nobody == ' ':
|
|
|
|
populatewallet(w)
|
|
|
|
|
|
|
|
def fillblankothers(w):
|
|
|
|
earliest = datetime.datetime.now().date()
|
|
|
|
if not w.date():
|
|
|
|
datewallet(w, earliest)
|
|
|
|
|
|
|
|
c = w.cave()
|
|
|
|
if not c:
|
|
|
|
caveifywallet(w)
|
2022-07-29 18:55:19 +01:00
|
|
|
|
2022-08-31 10:09:07 +01:00
|
|
|
def fixsurvextick(w, ticks):
|
|
|
|
ticks["S"] = w.fixsurvextick(ticks["S"])
|
|
|
|
|
|
|
|
|
2022-07-31 16:58:46 +01:00
|
|
|
def walletslistperson(request, first_name, last_name):
|
|
|
|
'''Page which displays a list of all the wallets for a specific person
|
|
|
|
HORRIBLE linear search through everything. Index and do SQL query properly
|
|
|
|
'''
|
|
|
|
# This is where we face having to re-do everything to do with names properly, rather than the horrible series of hacks over 20 years..
|
|
|
|
#GetPersonExpeditionNameLookup
|
2022-08-01 13:55:20 +01:00
|
|
|
def tickspersonwallet(p):
|
|
|
|
manywallets = []
|
|
|
|
wallets = Wallet.objects.all()
|
|
|
|
for w in wallets:
|
|
|
|
w.persons = w.people() # ephemeral attribute for web page
|
|
|
|
fillblankpeople(w)
|
|
|
|
if w.persons:
|
|
|
|
if p.fullname in w.persons:
|
|
|
|
manywallets.append(w)
|
|
|
|
fillblankothers(w)
|
|
|
|
w.ticks = w.get_ticks() # the complaints in colour form
|
2022-08-31 10:09:07 +01:00
|
|
|
fixsurvextick(w, w.ticks)
|
2022-08-01 13:55:20 +01:00
|
|
|
return manywallets
|
2022-07-31 16:58:46 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
if last_name:
|
|
|
|
p = Person.objects.get(fullname= f'{first_name} {last_name}')
|
|
|
|
else:
|
|
|
|
# speciall Wookey-hack
|
|
|
|
p = Person.objects.get(first_name= f'{first_name}')
|
|
|
|
except:
|
|
|
|
#raise
|
|
|
|
return render(request, 'errors/generic.html', {'message': f'Unrecognised name of a expo person: "{first_name} {last_name}"'})
|
2022-08-01 00:50:19 +01:00
|
|
|
|
2022-08-01 13:55:20 +01:00
|
|
|
manywallets = tickspersonwallet(p)
|
2022-08-01 00:50:19 +01:00
|
|
|
|
2022-07-31 16:58:46 +01:00
|
|
|
return render(request, 'personwallets.html', { 'manywallets':manywallets, 'settings': settings, 'person': p})
|
2022-07-27 21:22:46 +01:00
|
|
|
|
2022-08-01 13:55:20 +01:00
|
|
|
|
2022-07-27 21:22:46 +01:00
|
|
|
def walletslistyear(request, year):
|
2022-07-31 16:58:46 +01:00
|
|
|
'''Page which displays a list of all the wallets in a specific year
|
2022-07-27 21:22:46 +01:00
|
|
|
'''
|
2022-08-01 13:55:20 +01:00
|
|
|
def ticksyearwallet(year):
|
|
|
|
manywallets = []
|
|
|
|
wallets = Wallet.objects.all()
|
|
|
|
for w in wallets:
|
|
|
|
|
|
|
|
if year == w.year():
|
|
|
|
manywallets.append(w)
|
|
|
|
fillblankpeople(w)
|
|
|
|
fillblankothers(w)
|
2022-08-31 10:09:07 +01:00
|
|
|
w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc
|
|
|
|
fixsurvextick(w, w.ticks)
|
2022-08-01 13:55:20 +01:00
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
return manywallets
|
|
|
|
|
2022-07-27 21:22:46 +01:00
|
|
|
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)
|
2022-07-30 23:02:02 +01:00
|
|
|
#return render(request, 'errors/generic.html', {'message': 'This page logic not implemented yet'})
|
|
|
|
|
2022-08-01 13:55:20 +01:00
|
|
|
manywallets = ticksyearwallet(year)
|
2022-09-19 19:54:32 +01:00
|
|
|
expeditions = Expedition.objects.all()
|
|
|
|
expedition = Expedition.objects.filter(year=year)
|
|
|
|
return render(request, 'yearwallets.html', { 'manywallets':manywallets, 'settings': settings, 'year': year, 'expeditions': expeditions, 'expedition': expedition})
|
2022-07-30 23:02:02 +01:00
|
|
|
|
2022-07-27 21:22:46 +01:00
|
|
|
|
2022-07-27 23:48:22 +01:00
|
|
|
|
|
|
|
def cavewallets(request, caveid):
|
|
|
|
'''Returns all the wallets for just one cave
|
2022-07-27 21:22:46 +01:00
|
|
|
'''
|
2022-07-27 23:48:22 +01:00
|
|
|
Gcavelookup = GetCaveLookup()
|
|
|
|
if caveid in Gcavelookup:
|
|
|
|
cave = Gcavelookup[caveid]
|
|
|
|
else:
|
|
|
|
return render(request,'errors/badslug.html', {'badslug': caveid})
|
|
|
|
|
|
|
|
# remove duplication. SOrting is done in the template
|
|
|
|
wallets = set(Wallet.objects.filter(survexblock__survexfile__cave=cave)) # NB a filtered set
|
|
|
|
manywallets = list(wallets)
|
2022-07-29 18:55:19 +01:00
|
|
|
|
|
|
|
for w in manywallets:
|
2022-08-01 13:55:20 +01:00
|
|
|
fillblankpeople(w)
|
|
|
|
fillblankothers(w)
|
2022-08-31 10:09:07 +01:00
|
|
|
w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc
|
|
|
|
fixsurvextick(w, w.ticks)
|
2022-07-27 23:48:22 +01:00
|
|
|
return render(request, 'cavewallets.html', { 'manywallets':manywallets, 'settings': settings, 'cave': cave})
|
2022-07-27 21:22:46 +01:00
|
|
|
|
2022-08-01 13:55:20 +01:00
|
|
|
|
2022-03-18 11:28:35 +00:00
|
|
|
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}')
|
2021-05-03 20:36:29 +01:00
|
|
|
try:
|
|
|
|
wallet = Wallet.objects.get(walletname=urlunquote(path))
|
2022-03-18 11:28:35 +00:00
|
|
|
return render(request, 'wallet_old.html', { 'wallet':wallet, 'settings': settings })
|
2022-03-18 12:26:32 +00:00
|
|
|
except:
|
|
|
|
message = f'Scan folder error or not found \'{path}\' .'
|
|
|
|
return render(request, 'errors/generic.html', {'message': message})
|
|
|
|
|
2022-09-12 20:46:45 +01:00
|
|
|
# def walletindex(request, path):
|
|
|
|
# '''All years: special 'wallet' for *scanned* index pages
|
|
|
|
# '''
|
|
|
|
# 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 (wallet) error or not found \'{path}\' .'
|
|
|
|
# return render(request, 'errors/generic.html', {'message': message})
|
2021-05-03 20:36:29 +01:00
|
|
|
|
|
|
|
def scansingle(request, path, file):
|
|
|
|
'''sends a single binary file to the user for display - browser decides how using mimetype
|
|
|
|
'''
|
2022-07-21 08:32:11 +01:00
|
|
|
|
2021-05-03 20:36:29 +01:00
|
|
|
try:
|
|
|
|
wallet = Wallet.objects.get(walletname=urlunquote(path))
|
|
|
|
singlescan = SingleScan.objects.get(wallet=wallet, name=file)
|
2022-07-21 08:32:11 +01:00
|
|
|
print(" - scansingle {}:{}:{}:".format(path, file, getmimetype(file)))
|
2021-05-03 20:36:29 +01:00
|
|
|
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})
|
|
|
|
|
|
|
|
|
2022-07-27 21:22:46 +01:00
|
|
|
def allscans(request):
|
2022-07-05 12:14:03 +01:00
|
|
|
'''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
|
|
|
|
'''
|
2022-07-27 23:48:22 +01:00
|
|
|
manywallets = Wallet.objects.all() # NB all of them
|
2022-07-05 12:14:03 +01:00
|
|
|
# manywallets = Wallet.objects.all().prefetch_related('singlescan') fails as the link is defined on 'singlescan' not on 'wallet'
|
2021-05-03 20:36:29 +01:00
|
|
|
return render(request, 'manywallets.html', { 'manywallets':manywallets, 'settings': settings })
|
2022-07-21 08:32:11 +01:00
|
|
|
|