From e97d60a1c47118530a95f4506909aa95bd4ef042 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Sun, 4 Aug 2024 10:39:18 +0300 Subject: [PATCH] check pre exisiting scan files for proposed new wallet --- core/views/wallets_edit.py | 45 ++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/core/views/wallets_edit.py b/core/views/wallets_edit.py index c863dce..266d377 100644 --- a/core/views/wallets_edit.py +++ b/core/views/wallets_edit.py @@ -306,20 +306,46 @@ def walletedit(request, path=None): filesaved = False actual_saved = [] + def no_surveyscans(year, next): + """Detect if a folder of scans exists, even if no wallet Object has been created + """ + id = f"{year}#{next:02d}" + dirpath = Path(settings.SCANS_ROOT, year, id) + if not dirpath.is_dir(): + return True + + # if the folder exists, but has nothing in it, we treat it as available + if len(os.listdir(dirpath)) == 0: + return True + else: + return False + def get_next_empty(): """Gets the next most number for a new wallet just after the most recent one in the - db. But if it has no date set, then ignore it as it was only just created + db. But if it has no date set, then ignore it as it was only just created - is this a race condition? - This assumes we are still in the same year as the most wallet. + This assumes we are still in the same year as the most recent wallet. + + This just looks at the wallets in the db initially, then + checks the sub folders ofexpofiles/surveyscans/[year]/ """ latest = Wallet.objects.filter(walletname__startswith="20",walletdate__isnull=False).latest('walletname') # last VALID wallet - # print(f"latest is {latest}") + # print(f"==latest wallet number is {latest}") next = int(latest.walletname[5:]) + 1 - return f"{latest.walletname[:4]}:{next:02d}" - + year = latest.walletname[:4] + id = f"{year}:{next:02d}" + if no_surveyscans(year, next): + # print(f"==No scanned files found for {year=} # {next=}") + return id + else: + walletname=id.replace(':','#') + # print(f"==making wallet {walletname}") + make_wallet(walletname, date=True) + return get_next_empty() # recursive call + def get_last_wallet(): last = Wallet.objects.all().order_by('walletyear').last() - # print(f"last wallet {last}") + # print(f"==last wallet updated {last}") return last def are_we_next_year(): @@ -397,7 +423,7 @@ def walletedit(request, path=None): recent_year = recent_name[:4] recent_number = recent_name[5:] - print(f"---identify_most_recent_wallet: {recent_year=} {recent_number=}") + # print(f"---identify_most_recent_wallet: {recent_year=} {recent_number=}") return recent_year, recent_number def create_nav_links(wallet): @@ -462,12 +488,13 @@ def walletedit(request, path=None): """We need a wallet Object so that the django template stuff can find the files BUT we must restrict this to logged-in users otherwise spiderbots get at the hidden Submit button and create zillions of the buggers""" - # print(f"Making new wallet {walletname}") + # print(f"== make_wallet() Making new wallet {walletname}") try: w, created = Wallet.objects.get_or_create(walletname=walletname) - # print(f"--- Wallet string {walletname}, wallet object {w} created new?: {created}") + print(f"--- Wallet string {walletname}, wallet object {w} created new?: {created}") if date: w.walletdate = datetime.datetime.now() + w.save() if created: w.fpath = Path(settings.SCANS_ROOT, walletname[0:4], walletname) _ = w.year() # sets the walletyear property as a side-effect