diff options
Diffstat (limited to 'core/views/scans.py')
-rw-r--r-- | core/views/scans.py | 165 |
1 files changed, 147 insertions, 18 deletions
diff --git a/core/views/scans.py b/core/views/scans.py index 770b925..0ffdb22 100644 --- a/core/views/scans.py +++ b/core/views/scans.py @@ -1,5 +1,6 @@ import os, stat import re +import datetime from pathlib import Path from urllib.parse import urljoin, unquote as urlunquote from urllib.request import urlopen @@ -8,9 +9,12 @@ 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.models.survex import Wallet, SingleScan, SurvexBlock +from troggle.core.models.troggle import Person from troggle.core.models.caves import GetCaveLookup from troggle.core.views.expo import getmimetype +#from troggle.parsers.people import GetPersonExpeditionNameLookup + #import parsers.surveys '''one of these views serves files as binary blobs, and simply set the mime type based on the file extension, @@ -19,8 +23,148 @@ 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 + ''' + survexpeople = [] + blocks = SurvexBlock.objects.filter(scanswallet = w) + for b in blocks: + for personrole in b.survexpersonrole_set.all(): + survexpeople.append(personrole.personname) + w.persons = list(set(survexpeople)) + +def datewallet(w, earliest): + first = earliest + blocks = SurvexBlock.objects.filter(scanswallet = w) + for b in blocks: + if b.date: + if b.date < first: + first = b.date + if first == earliest: + # no date found + w.date = None + else: + w.date = first + +def caveifywallet(w): + '''Gets the cave from the list of survex files, + only selects one of them though. Only used for display. + ''' + 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. SHould make this a list or many:many ideally + +def fillblankpeople(w): + 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) + +def fillblankothers(w): + earliest = datetime.datetime.now().date() + if not w.date(): + datewallet(w, earliest) + + c = w.cave() + if not c: + caveifywallet(w) + + +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 + 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 + return manywallets + + 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}"'}) + + manywallets = tickspersonwallet(p) + + return render(request, 'personwallets.html', { 'manywallets':manywallets, 'settings': settings, 'person': p}) + + +def walletslistyear(request, year): + '''Page which displays a list of all the wallets in a specific year + ''' + def ticksyearwallet(year): + manywallets = [] + wallets = Wallet.objects.all() + for w in wallets: + + if year == w.year(): + manywallets.append(w) + fillblankpeople(w) + fillblankothers(w) + w.ticks = w.get_ticks() # the complaints in colour form + else: + continue + + return manywallets + + 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'}) + + manywallets = ticksyearwallet(year) + 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}) + + # 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: + fillblankpeople(w) + fillblankothers(w) + w.ticks = w.get_ticks() # the complaints in colour form + 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 ''' @@ -59,28 +203,13 @@ def scansingle(request, path, file): return render(request, 'errors/generic.html', {'message': message}) -def allwallets(request): +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() + 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 }) -def cavewallets(request, cave_id): - '''Returns all the wallets for just one cave, - ''' - - Gcavelookup = GetCaveLookup() - if cave_id in Gcavelookup: - cave = Gcavelookup[cave_id] - else: - return render(request,'errors/badslug.html', {'badslug': cave_id}) - - # remove duplication. SOrting is done in the template - wallets = set(Wallet.objects.filter(survexblock__survexfile__cave=cave)) - manywallets = list(wallets) - - return render(request, 'cavewallets.html', { 'manywallets':manywallets, 'settings': settings, 'cave': cave}) |