Skip to content

Commit

Permalink
delete stale files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeriox committed Oct 15, 2024
1 parent ec832bd commit bba2c7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 6 additions & 2 deletions ephios/plugins/files/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, *args, **kwargs):

def clean_file(self):
file_size = self.cleaned_data["file"].size
used, free = settings.GET_USERCONTENT_QUOTA()
_used, free = settings.GET_USERCONTENT_QUOTA()
if file_size > free:
raise ValidationError(
_("The file is too large. There are only %(quota)s available."),
Expand All @@ -31,7 +31,11 @@ def clean_file(self):

def save(self, commit=True):
self.instance.uploader = self.request.user
return super().save(commit)
result = super().save(commit)
# deleting the old file before commiting the new one results in no file being stored
if "file" in self.changed_data and "file" in self.initial:
self.initial["file"].delete(save=False)
return result


class EventAttachedDocumentForm(BasePluginFormMixin, Form):
Expand Down
12 changes: 11 additions & 1 deletion ephios/plugins/files/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.core.validators import FileExtensionValidator
from django.db import models
from django.db.transaction import on_commit
from django.dispatch import receiver
from django.utils.translation import gettext_lazy as _

from ephios.core.models import Event, UserProfile
Expand All @@ -21,9 +23,17 @@ def __str__(self):
return str(self.title)


@receiver(models.signals.post_delete, sender=Document)
def delete_stale_file(sender, instance, using, **kwargs):
def run_on_commit():
instance.file.delete(save=False)

on_commit(run_on_commit, using)


register_model_for_logging(
Document,
ModelFieldsLogConfig(
unlogged_fields=["id", "file", "updated_at"],
unlogged_fields=["id", "file", "uploader", "updated_at"],
),
)

0 comments on commit bba2c7d

Please sign in to comment.