Fix migration; fix invoice saving

This commit is contained in:
Edgar P. Burkhart 2022-12-31 18:44:03 +01:00
parent 2550c52a61
commit 748f9e5fb6
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
3 changed files with 11 additions and 16 deletions

View file

@ -13,7 +13,7 @@ def move_files(apps, schema_editor):
Snapshot = apps.get_model("main", "Snapshot")
for invoice in Invoice.objects.all():
print(invoice.pk)
print(f"Invoice {invoice.pk}")
if invoice.file is None:
invoice.delete()
continue
@ -26,15 +26,12 @@ def move_files(apps, schema_editor):
print(f"!!! Path {initial_path} does not exist")
invoice.delete()
continue
_name = main.models.get_path(invoice, None)
new_path = pathlib.Path(settings.MEDIA_ROOT, _name)
new_path.parent.mkdir(mode=0o750, parents=True, exist_ok=True)
initial_path.rename(new_path)
invoice.file.name = str(_name)
invoice.save()
prev_file = invoice.file.path
invoice.file.save(main.models.get_path(invoice, None), invoice.file)
pathlib.Path(prev_file).unlink()
for snapshot in Snapshot.objects.filter(file__isnull=False):
print(snapshot.pk)
print(f"Snapshot {snapshot.pk}")
try:
initial_path = pathlib.Path(snapshot.file.path)
except ValueError:
@ -46,12 +43,9 @@ def move_files(apps, schema_editor):
snapshot.file = None
snapshot.save()
continue
_name = main.models.get_path(snapshot, None)
new_path = pathlib.Path(settings.MEDIA_ROOT, _name)
new_path.parent.mkdir(mode=0o750, parents=True, exist_ok=True)
initial_path.rename(new_path)
snapshot.file.name = str(_name)
snapshot.save()
prev_file = snapshot.file.path
snapshot.file.save(main.models.get_path(snapshot, None), snapshot.file)
pathlib.Path(prev_file).unlink()
class Migration(migrations.Migration):