summaryrefslogtreecommitdiffstats
path: root/core/views/wallets_edit.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@gmail.com>2024-08-04 10:39:18 +0300
committerPhilip Sargent <philip.sargent@gmail.com>2024-08-04 10:39:18 +0300
commite97d60a1c47118530a95f4506909aa95bd4ef042 (patch)
tree895f40727bfd80c0b4070eeaf27fba3504c5be53 /core/views/wallets_edit.py
parent3bfb082e83a804665cd951bda0fba2176551954e (diff)
downloadtroggle-e97d60a1c47118530a95f4506909aa95bd4ef042.tar.gz
troggle-e97d60a1c47118530a95f4506909aa95bd4ef042.tar.bz2
troggle-e97d60a1c47118530a95f4506909aa95bd4ef042.zip
check pre exisiting scan files for proposed new wallet
Diffstat (limited to 'core/views/wallets_edit.py')
-rw-r--r--core/views/wallets_edit.py45
1 files 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 recent wallet.
- This assumes we are still in the same year as the most 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