Compare commits
1 commit
748f9e5fb6
...
374caf8a56
Author | SHA1 | Date | |
---|---|---|---|
374caf8a56 |
3 changed files with 28 additions and 36 deletions
|
@ -11,41 +11,34 @@ import main.models
|
||||||
def move_files(apps, schema_editor):
|
def move_files(apps, schema_editor):
|
||||||
Invoice = apps.get_model("main", "Invoice")
|
Invoice = apps.get_model("main", "Invoice")
|
||||||
Snapshot = apps.get_model("main", "Snapshot")
|
Snapshot = apps.get_model("main", "Snapshot")
|
||||||
|
db_alias = schema_editor.connection.alias
|
||||||
|
|
||||||
for invoice in Invoice.objects.all():
|
for invoice in Invoice.objects.using(db_alias).all():
|
||||||
print(f"Invoice {invoice.pk}")
|
initial_path = pathlib.Path(invoice.file.path)
|
||||||
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():
|
if not initial_path.exists():
|
||||||
print(f"!!! Path {initial_path} does not exist")
|
invoice.file = None
|
||||||
snapshot.file = None
|
invoice.save()
|
||||||
snapshot.save()
|
return
|
||||||
continue
|
_name = main.models.get_path(invoice, None)
|
||||||
prev_file = snapshot.file.path
|
print(settings.MEDIA_ROOT)
|
||||||
snapshot.file.save(main.models.get_path(snapshot, None), snapshot.file)
|
new_path = pathlib.Path(settings.MEDIA_ROOT, _name)
|
||||||
pathlib.Path(prev_file).unlink()
|
new_path.parent.mkdir(mode=0o750, parents=True, exist_ok=True)
|
||||||
|
initial_path.rename(new_path)
|
||||||
|
invoice.file.name = str(_name)
|
||||||
|
invoice.save()
|
||||||
|
|
||||||
|
for snapshot in Snapshot.objects.using(db_alias).filter(file__isnull=False):
|
||||||
|
initial_path = pathlib.Path(snapshot.file.path)
|
||||||
|
if not initial_path.exists():
|
||||||
|
invoice.file = None
|
||||||
|
invoice.save()
|
||||||
|
return
|
||||||
|
_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()
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
|
@ -279,12 +279,11 @@ class Invoice(CustomModel):
|
||||||
Transaction, on_delete=models.CASCADE, editable=False
|
Transaction, on_delete=models.CASCADE, editable=False
|
||||||
)
|
)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self):
|
||||||
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"):
|
||||||
|
|
|
@ -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(LoginRequiredMixin, View):
|
class MediaView(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")
|
||||||
|
|
Loading…
Reference in a new issue