diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2023-01-30 19:04:36 +0000 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2023-01-30 19:04:36 +0000 |
commit | 7808005498b06d11775e83a614657fd6cbb476b8 (patch) | |
tree | 3c7b0bcb4518ba39548804082c2505949121354f /core/views/scans.py | |
parent | d06dd3d16678c3e222a261a12bcf2f147b3fe5f9 (diff) | |
download | troggle-7808005498b06d11775e83a614657fd6cbb476b8.tar.gz troggle-7808005498b06d11775e83a614657fd6cbb476b8.tar.bz2 troggle-7808005498b06d11775e83a614657fd6cbb476b8.zip |
ran 'black' to reformat all the core files
Diffstat (limited to 'core/views/scans.py')
-rw-r--r-- | core/views/scans.py | 237 |
1 files changed, 133 insertions, 104 deletions
diff --git a/core/views/scans.py b/core/views/scans.py index e2af7f8..e1db7e6 100644 --- a/core/views/scans.py +++ b/core/views/scans.py @@ -17,11 +17,11 @@ from troggle.core.models.survex import SingleScan, SurvexBlock, Wallet from troggle.core.models.troggle import DataIssue, Expedition, Person from troggle.core.views.expo import getmimetype -#from troggle.parsers.people import GetPersonExpeditionNameLookup +# from troggle.parsers.people import GetPersonExpeditionNameLookup -#import parsers.surveys +# import parsers.surveys -'''one of these views serves files as binary blobs, and simply set the mime type based on the file extension, +"""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. @@ -35,80 +35,87 @@ 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. -''' +""" + def populatewallet(w): - '''Copy survex data here just for display, not permanently - + """Copy survex data here just for display, not permanently + Only gets data from the survex file when it was parsed on import.. so doesn't work if there is no *ref value - ''' + """ survexpeople = [] - blocks = SurvexBlock.objects.filter(scanswallet = w) + blocks = SurvexBlock.objects.filter(scanswallet=w) for b in blocks: - for personrole in b.survexpersonrole_set.all(): + for personrole in b.survexpersonrole_set.all(): survexpeople.append(personrole.personname) - w.persons = list(set(survexpeople)) + w.persons = list(set(survexpeople)) + def datewallet(w, earliest): - '''Gets the date of the youngest survexblock associated with the wallet + """Gets the date of the youngest survexblock associated with the wallet REFACTOR this to do the whole date-getting task - ''' + """ first = earliest - blocks = SurvexBlock.objects.filter(scanswallet = w) + blocks = SurvexBlock.objects.filter(scanswallet=w) for b in blocks: if b.date: if b.date < first: - first = b.date + first = b.date if first == earliest: # no date found w.date = None else: w.date = first.isoformat() return w.date - + + def caveifywallet(w): - '''Gets the cave from the list of survex files, + """Gets the cave from the list of survex files, only selects one of them though. Only used for display. - ''' - #print(f' - Caveify {w=}') + """ + # print(f' - Caveify {w=}') blocknames = [] - blocks = SurvexBlock.objects.filter(scanswallet = w) - for b in blocks: + 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.caveobj = b.survexfile.cave # just gets the last one, randomly. SHould make this a list or many:many ideally + w.caveobj = ( + b.survexfile.cave + ) # just gets the last one, randomly. SHould make this a list or many:many ideally w.cave = w.caveobj if b.name: blocknames.append(b.name) - + if w.name(): w.displaynames = [w.name()] else: w.displaynames = blocknames - + + def fillblankpeople(w): - # this isn't working..? why? Because it needs a *ref and an import - wp = w.people() - w.persons = wp - if not wp: - populatewallet(w) - else: - if len(wp) == 1: - # print(f' - {wp=}') - nobody = wp[0].lower() - if nobody == 'unknown' or nobody == 'nobody' or nobody == ' ' or nobody == '': - # print(f' - {wp=} {nobody=}') - populatewallet(w) - + # this isn't working..? why? Because it needs a *ref and an import + wp = w.people() + w.persons = wp + if not wp: + populatewallet(w) + else: + if len(wp) == 1: + # print(f' - {wp=}') + nobody = wp[0].lower() + if nobody == "unknown" or nobody == "nobody" or nobody == " " or nobody == "": + # print(f' - {wp=} {nobody=}') + populatewallet(w) + + def fillblankothers(w): if not w.walletdate: earliest = datetime.datetime.now().date() - if not w.date(): # sets .walletdate as a side-effect, gets it from JSON - d =datewallet(w, earliest) # if nothing in JASON, it looks at the survex blocks + if not w.date(): # sets .walletdate as a side-effect, gets it from JSON + d = datewallet(w, earliest) # if nothing in JASON, it looks at the survex blocks w.walletdate = d w.save() - + Gcavelookup = GetCaveLookup() wcaveid = w.cave() @@ -118,58 +125,67 @@ def fillblankothers(w): if type(wcaveid) == list: for i in wcaveid: if i in Gcavelookup: - w.caveobj = Gcavelookup[i] # just sets it to the last one found. nasty. bug waiting to happen - #print(f' - Found cave object from id {wcaveid}') + w.caveobj = Gcavelookup[i] # just sets it to the last one found. nasty. bug waiting to happen + # print(f' - Found cave object from id {wcaveid}') else: if wcaveid in Gcavelookup: w.caveobj = Gcavelookup[wcaveid] else: - print(f' - Failed to find cave object from id {wcaveid}') + print(f" - Failed to find cave object from id {wcaveid}") + + +def fixsurvextick(w, ticks): + ticks["S"] = w.fixsurvextick(ticks["S"]) - -def fixsurvextick(w, ticks): - ticks["S"] = w.fixsurvextick(ticks["S"]) - def walletslistperson(request, first_name, last_name): - '''Page which displays a list of all the wallets for a specific person + """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 + # GetPersonExpeditionNameLookup def tickspersonwallet(p): manywallets = [] wallets = Wallet.objects.all() for w in wallets: - w.persons = w.people() # ephemeral attribute for web page + 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 + w.ticks = w.get_ticks() # the complaints in colour form fixsurvextick(w, w.ticks) return manywallets - - print(f"-walletslistperson") - + + print(f"-walletslistperson") + try: if last_name: - p = Person.objects.get(fullname= f'{first_name} {last_name}') - else: + p = Person.objects.get(fullname=f"{first_name} {last_name}") + else: # special Wookey-hack - p = Person.objects.get(first_name= f'{first_name}') + 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}"'}) - + # raise + return render( + request, + "errors/generic.html", + {"message": f'Unrecognised name of a expo person: "{first_name} {last_name}"'}, + ) + manywallets = tickspersonwallet(p) expeditions = Expedition.objects.all() - print(f"--") - return render(request, 'personwallets.html', { 'manywallets':manywallets, 'settings': settings, 'person': p, 'expeditions': expeditions}) + print(f"--") + return render( + request, + "personwallets.html", + {"manywallets": manywallets, "settings": settings, "person": p, "expeditions": expeditions}, + ) + def setwalletsdates(): - wallets = Wallet.objects.filter(walletdate=None) + wallets = Wallet.objects.filter(walletdate=None) print(f"undated wallets: {len(wallets)}") for w in wallets: w.walletdate = w.date() @@ -177,50 +193,60 @@ def setwalletsdates(): def walletslistyear(request, year): - '''Page which displays a list of all the wallets in a specific year. + """Page which displays a list of all the wallets in a specific year. We have a field .walletyear, which we set on import. - ''' + """ + def ticksyearwallet(year): manywallets = [] - wallets = Wallet.objects.filter(walletyear__year=year) + wallets = Wallet.objects.filter(walletyear__year=year) for w in wallets: manywallets.append(w) fillblankpeople(w) fillblankothers(w) - w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc + w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc fixsurvextick(w, w.ticks) return manywallets - print(f"-walletslistyear") + + print(f"-walletslistyear") if year < 1976 or year > 2050: - return render(request, 'errors/generic.html', {'message': 'Year out of range. Must be between 1976 and 2050'}) - - #return render(request, 'errors/generic.html', {'message': 'This page logic not implemented yet'}) - + return render(request, "errors/generic.html", {"message": "Year out of range. Must be between 1976 and 2050"}) + + # return render(request, 'errors/generic.html', {'message': 'This page logic not implemented yet'}) + year = str(year) manywallets = ticksyearwallet(year) expeditions = Expedition.objects.all() expedition = expeditions.filter(year=year) - print(f"--") - return render(request, 'yearwallets.html', { 'manywallets':manywallets, 'settings': settings, 'year': year, 'expeditions': expeditions, 'expedition': expedition}) - + print(f"--") + return render( + request, + "yearwallets.html", + { + "manywallets": manywallets, + "settings": settings, + "year": year, + "expeditions": expeditions, + "expedition": expedition, + }, + ) def cavewallets(request, caveid): - '''Returns all the wallets for just one cave - ''' - print(f"-cavewalletsl") + """Returns all the wallets for just one cave""" + print(f"-cavewalletsl") Gcavelookup = GetCaveLookup() if caveid in Gcavelookup: cave = Gcavelookup[caveid] else: - return render(request,'errors/badslug.html', {'badslug': caveid}) + return render(request, "errors/badslug.html", {"badslug": caveid}) # remove duplication. Sorting is done in the template # But this only gets wallets which have survex files attached.. wallets = set(Wallet.objects.filter(survexblock__survexfile__cave=cave)) - + # all the ones without a survexblock attached via a *ref, search for match in JSON zilchwallets = set(Wallet.objects.exclude(survexblock__survexfile__cave=cave)) for z in zilchwallets: @@ -235,35 +261,37 @@ def cavewallets(request, caveid): wurl = f"/scanupload/{z.walletname.replace('#',':')}" message = f" ! In {z.walletname} there is an unrecognised cave name '{zcaveid}' (out of {len(Gcavelookup):,} cave names and aliases)" print(message) - DataIssue.objects.update_or_create(parser='scans', message=message, url=wurl) + DataIssue.objects.update_or_create(parser="scans", message=message, url=wurl) manywallets = list(set(wallets)) for w in manywallets: fillblankpeople(w) - fillblankothers(w) - w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc + fillblankothers(w) + w.ticks = w.get_ticks() # the complaints in colour form, from the json file on disc fixsurvextick(w, w.ticks) expeditions = Expedition.objects.all() - print(f"--") - return render(request, 'cavewallets.html', { 'manywallets':manywallets, 'settings': settings, 'cave': cave, 'expeditions': expeditions}) + print(f"--") + return render( + request, + "cavewallets.html", + {"manywallets": manywallets, "settings": settings, "cave": cave, "expeditions": expeditions}, + ) def oldwallet(request, path): - '''Now called only for non-standard wallet structures for pre-2000 wallets - ''' + """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}') + print(f"! - oldwallet path:{path}") try: - wallet = Wallet.objects.get(walletname=urlunquote(path)) - return render(request, 'wallet_old.html', { 'wallet':wallet, 'settings': settings }) + 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}) + 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 - ''' + """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) @@ -271,25 +299,26 @@ def scansingle(request, path, file): if imagefile.is_file(): message = f" - scansingle {imagefile} {path}:{file}:{getmimetype(file)}:" print(message) - return HttpResponse(content=open(imagefile,"rb"), content_type=getmimetype(file)) # any type of image + return HttpResponse(content=open(imagefile, "rb"), content_type=getmimetype(file)) # any type of image else: - message = f'Scan folder file \'{imagefile}\' not found. {path=} {file=}' + message = f"Scan folder file '{imagefile}' not found. {path=} {file=}" print(message) - return render(request, 'errors/generic.html', {'message': message}) + return render(request, "errors/generic.html", {"message": message}) except: - message = f'Scan folder or scan item access error \'{path}\' and \'{file}\'.' - return render(request, 'errors/generic.html', {'message': message}) + message = f"Scan folder or scan item access error '{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 + """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() # NB all of them # manywallets = Wallet.objects.all().prefetch_related('singlescan') fails as the link is defined on 'singlescan' not on 'wallet' expeditions = Expedition.objects.all() - return render(request, 'manywallets.html', { 'manywallets':manywallets, 'settings': settings, 'expeditions': expeditions }) - + return render( + request, "manywallets.html", {"manywallets": manywallets, "settings": settings, "expeditions": expeditions} + ) |