Compare commits
2 commits
374caf8a56
...
748f9e5fb6
Author | SHA1 | Date | |
---|---|---|---|
748f9e5fb6 | |||
2550c52a61 |
3 changed files with 61 additions and 3 deletions
57
nummi/main/migrations/0023_auto_20221231_1741.py
Normal file
57
nummi/main/migrations/0023_auto_20221231_1741.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
# Generated by Django 4.1.4 on 2022-12-31 16:41
|
||||
|
||||
import pathlib
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations
|
||||
|
||||
import main.models
|
||||
|
||||
|
||||
def move_files(apps, schema_editor):
|
||||
Invoice = apps.get_model("main", "Invoice")
|
||||
Snapshot = apps.get_model("main", "Snapshot")
|
||||
|
||||
for invoice in Invoice.objects.all():
|
||||
print(f"Invoice {invoice.pk}")
|
||||
if invoice.file is None:
|
||||
invoice.delete()
|
||||
continue
|
||||
try:
|
||||
initial_path = pathlib.Path(invoice.file.path)
|
||||
except ValueError:
|
||||
invoice.delete()
|
||||
continue
|
||||
if invoice.file is None or not initial_path.exists():
|
||||
print(f"!!! Path {initial_path} does not exist")
|
||||
invoice.delete()
|
||||
continue
|
||||
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(f"Snapshot {snapshot.pk}")
|
||||
try:
|
||||
initial_path = pathlib.Path(snapshot.file.path)
|
||||
except ValueError:
|
||||
snapshot.file = None
|
||||
snapshot.save()
|
||||
continue
|
||||
if not initial_path.exists():
|
||||
print(f"!!! Path {initial_path} does not exist")
|
||||
snapshot.file = None
|
||||
snapshot.save()
|
||||
continue
|
||||
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):
|
||||
|
||||
dependencies = [
|
||||
("main", "0022_alter_account_icon_alter_category_icon"),
|
||||
]
|
||||
|
||||
operations = [migrations.RunPython(move_files)]
|
|
@ -34,7 +34,7 @@ class CustomModel(UserModel):
|
|||
def get_path(instance, filename):
|
||||
return pathlib.Path(
|
||||
"user",
|
||||
str(instance.user.get_username()),
|
||||
str(instance.user.username),
|
||||
instance._meta.model_name,
|
||||
str(instance.pk),
|
||||
).with_suffix(".pdf")
|
||||
|
@ -279,11 +279,12 @@ class Invoice(CustomModel):
|
|||
Transaction, on_delete=models.CASCADE, editable=False
|
||||
)
|
||||
|
||||
def save(self):
|
||||
def save(self, *args, **kwargs):
|
||||
if Invoice.objects.filter(id=self.id).exists():
|
||||
_prever = Invoice.objects.get(id=self.id)
|
||||
if _prever.file and _prever.file != self.file:
|
||||
pathlib.Path(_prever.file.path).unlink(missing_ok=True)
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
if hasattr(self, "transaction"):
|
||||
|
|
|
@ -340,7 +340,7 @@ class SearchView(TransactionListView):
|
|||
return super().get_context_data(**kwargs) | {"search": self.kwargs["search"]}
|
||||
|
||||
|
||||
class MediaView(View):
|
||||
class MediaView(LoginRequiredMixin, View):
|
||||
def get(self, request, *args, **kwargs):
|
||||
_username = kwargs.get("username")
|
||||
_path = kwargs.get("path")
|
||||
|
|
Loading…
Reference in a new issue