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

View file

@ -279,11 +279,12 @@ class Invoice(CustomModel):
Transaction, on_delete=models.CASCADE, editable=False Transaction, on_delete=models.CASCADE, editable=False
) )
def save(self): def save(self, *args, **kwargs):
if Invoice.objects.filter(id=self.id).exists(): if Invoice.objects.filter(id=self.id).exists():
_prever = Invoice.objects.get(id=self.id) _prever = Invoice.objects.get(id=self.id)
if _prever.file and _prever.file != self.file: if _prever.file and _prever.file != self.file:
pathlib.Path(_prever.file.path).unlink(missing_ok=True) pathlib.Path(_prever.file.path).unlink(missing_ok=True)
super().save(*args, **kwargs)
def __str__(self): def __str__(self):
if hasattr(self, "transaction"): if hasattr(self, "transaction"):

View file

@ -340,7 +340,7 @@ class SearchView(TransactionListView):
return super().get_context_data(**kwargs) | {"search": self.kwargs["search"]} return super().get_context_data(**kwargs) | {"search": self.kwargs["search"]}
class MediaView(View): class MediaView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
_username = kwargs.get("username") _username = kwargs.get("username")
_path = kwargs.get("path") _path = kwargs.get("path")