Add file selector to snapshots
This commit is contained in:
parent
996f6a9f18
commit
e11749f187
6 changed files with 76 additions and 5 deletions
|
@ -1,9 +1,11 @@
|
|||
import pathlib
|
||||
from datetime import date
|
||||
import uuid
|
||||
from django.db import models
|
||||
from django.forms import ModelForm
|
||||
from django.core.validators import FileExtensionValidator
|
||||
from django.utils.translation import gettext as _
|
||||
from django.core.files.storage import Storage
|
||||
|
||||
|
||||
class Category(models.Model):
|
||||
|
@ -90,15 +92,20 @@ class TransactionForm(ModelForm):
|
|||
]
|
||||
|
||||
|
||||
def invoice_path(instance, filename):
|
||||
return pathlib.Path("invoices", str(instance.id), filename)
|
||||
|
||||
|
||||
class Invoice(models.Model):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
name = models.CharField(
|
||||
max_length=256, default=_("Invoice"), verbose_name=_("Name")
|
||||
)
|
||||
file = models.FileField(
|
||||
upload_to="invoices/",
|
||||
upload_to=invoice_path,
|
||||
validators=[FileExtensionValidator(["pdf"])],
|
||||
verbose_name=_("File"),
|
||||
max_length=128,
|
||||
)
|
||||
transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)
|
||||
|
||||
|
@ -123,6 +130,10 @@ class InvoiceForm(ModelForm):
|
|||
fields = ["name", "file"]
|
||||
|
||||
|
||||
def snapshot_path(instance, filename):
|
||||
return pathlib.Path("snapshots", str(instance.id)).with_suffix(".pdf")
|
||||
|
||||
|
||||
class Snapshot(models.Model):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
date = models.DateField(default=date.today, unique=True, verbose_name=_("Date"))
|
||||
|
@ -135,12 +146,22 @@ class Snapshot(models.Model):
|
|||
diff = models.DecimalField(
|
||||
max_digits=12, decimal_places=2, editable=False, blank=True, null=True
|
||||
)
|
||||
file = models.FileField(
|
||||
upload_to=snapshot_path,
|
||||
validators=[FileExtensionValidator(["pdf"])],
|
||||
verbose_name=_("File"),
|
||||
max_length=256,
|
||||
blank=True,
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return f"{_('Snapshot')} {self.date}"
|
||||
|
||||
def save(self, *args, only_super=False, **kwargs):
|
||||
if not only_super:
|
||||
_prever = Snapshot.objects.get(id=self.id)
|
||||
if _prever.file and _prever.file != self.file:
|
||||
pathlib.Path(_prever.file.path).unlink(missing_ok=True)
|
||||
_prev = (
|
||||
self.__class__.objects.order_by("-date")
|
||||
.exclude(id=self.id)
|
||||
|
@ -188,6 +209,7 @@ class Snapshot(models.Model):
|
|||
_next.save(only_super=True)
|
||||
|
||||
def delete(self, *args, only_super=False, **kwargs):
|
||||
self.file.delete()
|
||||
if not only_super:
|
||||
try:
|
||||
_next = self.__class__.objects.get(previous=self)
|
||||
|
@ -244,4 +266,4 @@ class SnapshotForm(ModelForm):
|
|||
|
||||
class Meta:
|
||||
model = Snapshot
|
||||
fields = ["date", "value"]
|
||||
fields = ["date", "value", "file"]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue