Skip to content

Commit

Permalink
fix: File.objects.only("id") query when deleting a user (django-cms#1357
Browse files Browse the repository at this point in the history
)

* Fix: Failing js test

* Fix Flie.objects.only("id") query

refs django-cms#1290

* Add test for deleting users

---------

Co-authored-by: William Lindholm <[email protected]>
  • Loading branch information
fsbraun and wiltso authored Jun 15, 2023
1 parent 35d4b8e commit e920e55
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
CHANGELOG
=========

unreleased
==========

* Fix File.objects.only() query required for deleting user who own files.

2.2.5 (2023-06-11)
==================

Expand Down
10 changes: 10 additions & 0 deletions filer/models/filemodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from django.utils.translation import gettext_lazy as _

from polymorphic.managers import PolymorphicManager
from polymorphic.query import PolymorphicQuerySet
from polymorphic.models import PolymorphicModel

from .. import settings as filer_settings
Expand All @@ -21,7 +22,16 @@
from .foldermodels import Folder


class FileQuerySet(PolymorphicQuerySet):
def only(self, *fields):
fields = set(fields)
fields.update(["_file_size", "sha1", "is_public"])
return super().only(*fields)


class FileManager(PolymorphicManager):
queryset_class = FileQuerySet

def find_all_duplicates(self):
r = {}
for file_obj in self.all():
Expand Down
25 changes: 23 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ def tearDown(self):
for f in File.objects.all():
f.delete()

def create_filer_image(self):
def create_filer_image(self, owner=None):
if owner is None:
owner = self.superuser
file_obj = DjangoFile(open(self.filename, 'rb'), name=self.image_name)
image = Image.objects.create(owner=self.superuser,
image = Image.objects.create(owner=owner,
original_filename=self.image_name,
file=file_obj)
return image
Expand Down Expand Up @@ -307,3 +309,22 @@ def test_canonical_url_settings(self):
image.save()
canonical = image.canonical_url
self.assertTrue(canonical.startswith('/filer/test-path/'))

def test_delete_user_who_owns_file(self):
"""Tests if deleting users who own files works"""
from django.contrib.auth.models import User

user = User.objects.create(
username="test",
password="top-secret",
email="[email protected]",
is_staff=True,
)
image = self.create_filer_image(user)

user.delete()

# User needs to be gone
self.assertEqual(len(User.objects.filter(username="test")), 0)
# Image remains w/o owner
self.assertIsNone(File.objects.get(pk=image.pk).owner)

0 comments on commit e920e55

Please sign in to comment.