2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-29 21:31:54 +00:00
troggle/core/views/scans.py

69 lines
3.1 KiB
Python
Raw Normal View History

2021-05-03 20:36:29 +01:00
import os, stat
import re
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
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.
'''
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})
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 })
2021-05-03 20:36:29 +01:00
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 allwallets(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
'''
2021-05-03 20:36:29 +01:00
manywallets = Wallet.objects.all()
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 })