Add stash backups to database transfers in the webui
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
from flask import Flask, redirect, abort, render_template, request
|
from flask import Flask, redirect, abort, render_template, request
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
import psutil
|
import psutil
|
||||||
from d2warehouse.item import Item
|
from d2warehouse.item import Item
|
||||||
@@ -109,7 +111,7 @@ def stash_store_items(stash_name: str):
|
|||||||
item = stash.tabs[tab_idx].items[item_idx]
|
item = stash.tabs[tab_idx].items[item_idx]
|
||||||
items.append((tab_idx, item))
|
items.append((tab_idx, item))
|
||||||
|
|
||||||
# TODO: create backups
|
backup_stash(stash_name)
|
||||||
|
|
||||||
for tab_idx, item in items:
|
for tab_idx, item in items:
|
||||||
stash.tabs[tab_idx].remove(item)
|
stash.tabs[tab_idx].remove(item)
|
||||||
@@ -140,6 +142,16 @@ def list_storage(stash_name: str):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def backup_stash(stash_name: str) -> None:
|
||||||
|
stash_path = save_path() / STASH_FILES[stash_name]
|
||||||
|
backup_path = save_path() / "backups"
|
||||||
|
if not backup_path.exists():
|
||||||
|
backup_path.mkdir(parents=True)
|
||||||
|
ts = datetime.now().strftime("%Y-%m-%d_%H.%M.%S.%f")[:-3]
|
||||||
|
backup_path /= f"{ts}_{STASH_FILES[stash_name]}"
|
||||||
|
shutil.copy(stash_path, backup_path)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/storage/<stash_name>/take", methods=["POST"])
|
@app.route("/storage/<stash_name>/take", methods=["POST"])
|
||||||
def storage_take_items(stash_name: str):
|
def storage_take_items(stash_name: str):
|
||||||
if stash_name not in STASH_FILES or stash_name not in DB_FILES:
|
if stash_name not in STASH_FILES or stash_name not in DB_FILES:
|
||||||
@@ -157,12 +169,15 @@ def storage_take_items(stash_name: str):
|
|||||||
|
|
||||||
stash_data = stash_path.read_bytes()
|
stash_data = stash_path.read_bytes()
|
||||||
stash = parse_stash(stash_data)
|
stash = parse_stash(stash_data)
|
||||||
|
backup_stash(stash_name)
|
||||||
# TODO: create backups
|
|
||||||
|
|
||||||
# Write items to temporary stash file
|
# Write items to temporary stash file
|
||||||
db = get_stash_db(stash_name)
|
db = get_stash_db(stash_name)
|
||||||
ids = [y.group(1) for x in request.form.keys() if (y := re.match(r"item_(\d+)", x))]
|
ids = [
|
||||||
|
int(y.group(1))
|
||||||
|
for x in request.form.keys()
|
||||||
|
if (y := re.match(r"item_(\d+)", x))
|
||||||
|
]
|
||||||
for id in ids:
|
for id in ids:
|
||||||
item = Item.load_from_db(id, db=db)
|
item = Item.load_from_db(id, db=db)
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user