mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2025-12-14 19:47:12 +00:00
Handling and fixing bad dates in JSON input
This commit is contained in:
@@ -2,6 +2,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import json
|
import json
|
||||||
import operator
|
import operator
|
||||||
|
import datetime
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
@@ -10,6 +11,7 @@ from django.db import models
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
# from troggle.core.models.troggle import DataIssue # circular import. Hmm
|
||||||
|
|
||||||
class SurvexDirectory(models.Model):
|
class SurvexDirectory(models.Model):
|
||||||
path = models.CharField(max_length=200)
|
path = models.CharField(max_length=200)
|
||||||
@@ -185,6 +187,7 @@ class Wallet(models.Model):
|
|||||||
fp = Path(self.fpath)
|
fp = Path(self.fpath)
|
||||||
wname = fp.name
|
wname = fp.name
|
||||||
wyear = fp.parent.name
|
wyear = fp.parent.name
|
||||||
|
wurl = f"/scanupload/{self.walletname}" # .replace('#', ':')
|
||||||
|
|
||||||
jsonfile = Path(settings.DRAWINGS_DATA, "walletjson") / wyear / wname / "contents.json"
|
jsonfile = Path(settings.DRAWINGS_DATA, "walletjson") / wyear / wname / "contents.json"
|
||||||
if not Path(jsonfile).is_file():
|
if not Path(jsonfile).is_file():
|
||||||
@@ -195,11 +198,30 @@ class Wallet(models.Model):
|
|||||||
try:
|
try:
|
||||||
waldata = json.load(json_f)
|
waldata = json.load(json_f)
|
||||||
except:
|
except:
|
||||||
wurl = f"/scanupload/{self.walletname}" # .replace('#', ':')
|
|
||||||
message = f"! {str(self.walletname)} Failed to load {jsonfile} JSON file"
|
message = f"! {str(self.walletname)} Failed to load {jsonfile} JSON file"
|
||||||
#print(message)
|
#print(message)
|
||||||
raise
|
raise
|
||||||
|
if waldata["date"]:
|
||||||
|
datestr = waldata["date"].replace('.','-')
|
||||||
|
try:
|
||||||
|
thisdate = datetime.date.fromisoformat(datestr)
|
||||||
|
except ValueError:
|
||||||
|
# probably a single digit day number. HACKUS MAXIMUS.
|
||||||
|
# clearly we need to fix this when we first import date strings..
|
||||||
|
datestr = datestr[:-1] + '0' + datestr[-1]
|
||||||
|
print(f' - {datestr=} ')
|
||||||
|
try:
|
||||||
|
thisdate = datetime.date.fromisoformat(datestr)
|
||||||
|
try:
|
||||||
|
waldata["date"] = thisdate.isoformat()
|
||||||
|
except:
|
||||||
|
message = f"! {str(self.walletname)} Date formatting failure {thisdate}. Failed to load from {jsonfile} JSON file"
|
||||||
|
from troggle.core.models.troggle import DataIssue
|
||||||
|
DataIssue.objects.update_or_create(parser='scans', message=message, url=wurl)
|
||||||
|
except:
|
||||||
|
message = f"! {str(self.walletname)} Date format not ISO {datestr}. Failed to load fro, {jsonfile} JSON file"
|
||||||
|
from troggle.core.models.troggle import DataIssue
|
||||||
|
DataIssue.objects.update_or_create(parser='scans', message=message, url=wurl)
|
||||||
return waldata
|
return waldata
|
||||||
|
|
||||||
def year(self):
|
def year(self):
|
||||||
|
|||||||
@@ -82,6 +82,9 @@ def fillblankpeople(w):
|
|||||||
# this isn't working..? why? Because it needs a *ref and an import
|
# this isn't working..? why? Because it needs a *ref and an import
|
||||||
wp = w.people()
|
wp = w.people()
|
||||||
w.persons = wp
|
w.persons = wp
|
||||||
|
if not wp:
|
||||||
|
populatewallet(w)
|
||||||
|
else:
|
||||||
if len(wp) == 1:
|
if len(wp) == 1:
|
||||||
# print(f' - {wp=}')
|
# print(f' - {wp=}')
|
||||||
nobody = wp[0].lower()
|
nobody = wp[0].lower()
|
||||||
|
|||||||
@@ -631,14 +631,24 @@ def scanupload(request, path=None):
|
|||||||
# clearly we need to fix this when we first import date strings..
|
# clearly we need to fix this when we first import date strings..
|
||||||
datestr = datestr[:-1] + '0' + datestr[-1]
|
datestr = datestr[:-1] + '0' + datestr[-1]
|
||||||
print(f' - {datestr=} ')
|
print(f' - {datestr=} ')
|
||||||
|
try:
|
||||||
samedate = datetime.date.fromisoformat(datestr)
|
samedate = datetime.date.fromisoformat(datestr)
|
||||||
|
except:
|
||||||
|
try:
|
||||||
|
samedate = datetime.date.fromisoformat(datestr[:10])
|
||||||
|
except:
|
||||||
|
samedate = None
|
||||||
|
|
||||||
thisexpo = Expedition.objects.get(year=int(year))
|
thisexpo = Expedition.objects.get(year=int(year))
|
||||||
|
if samedate:
|
||||||
expeditionday = thisexpo.get_expedition_day(samedate)
|
expeditionday = thisexpo.get_expedition_day(samedate)
|
||||||
#print(f' - {thisexpo=} {expeditionday=}')
|
#print(f' - {thisexpo=} {expeditionday=}')
|
||||||
svxothers = SurvexBlock.objects.filter(expeditionday=expeditionday)
|
svxothers = SurvexBlock.objects.filter(expeditionday=expeditionday)
|
||||||
#print(f' - {thisexpo=} {expeditionday=} {svxothers=}')
|
#print(f' - {thisexpo=} {expeditionday=} {svxothers=}')
|
||||||
trips = LogbookEntry.objects.filter(date=samedate)
|
trips = LogbookEntry.objects.filter(date=samedate)
|
||||||
|
else:
|
||||||
|
svxothers = None
|
||||||
|
trips = None
|
||||||
|
|
||||||
else:
|
else:
|
||||||
svxothers = None
|
svxothers = None
|
||||||
|
|||||||
Reference in New Issue
Block a user