Skip to content

Commit

Permalink
flake, stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
benzkji committed Oct 23, 2017
1 parent 9541459 commit 7da8498
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 73 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include LICENSE
include README.rst
include README.md
include PYPI.rst
global-exclude *.orig *.pyc *.log *.swp

Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# django-filer-addons-addons


[![Build Status](https://travis-ci.org/bnzk/django-filer-addons.svg "Build Status")](https://travis-ci.org/bnzk/django-filer-addons/)
[![PyPi Version](https://img.shields.io/pypi/v/django-filer-addons.svg "PyPi Version")](https://pypi.python.org/pypi/django-filer-addons/)
[![Licence](https://img.shields.io/pypi/l/django-filer-addons.svg "Licence")](https://pypi.python.org/pypi/django-filer-addons/)

various django-filer enhancements / bugfixes. Fair warning: parts of this package might not be
best practiced or very efficient (that said, can cause performance issues!), but are implemented
as we needed this functionality, and as filer development sometimes
seems to stall, it is was implemented just to make it work. Part of this package might even get merged
into filer (someday..).

Features
--------

This package provides sub applications, that can be installed individually, to only selecte the
needed features.

- `filer_addons.filer_gui` - an improved filer file and filer image field, a multiupload inline.
- `filer_addons.filer_signals` - can help avoid duplicates, will rename files on the filesystem if
said to do so, and can put unfiled files in a default folder, to prevent permission issues.
- `filer_addons.filer_utils` - various helpers: for now, generate folder/filenames for uploaded
files (without folder/8-char simple uuid4/use db folder/year/year-month)

Install
-------

TODO

Usage
-----

TODO

**filer_gui**

**filer_signals**

**filer_utils**
39 changes: 0 additions & 39 deletions README.rst

This file was deleted.

6 changes: 3 additions & 3 deletions filer_addons/filer_signals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ def check_rename(instance, old_name=None):
def filer_unfiled_to_folder(sender, instance, **kwargs):
"""
check if a file is unfiled, if yes, put into default folder.
ATTENTION: this signal must be registered before the duplicate detection signal => for when only
duplicates in the same folder need to be detected! (put in folder first, then detect duplicate)
ATTENTION: this signal must be registered before the duplicate detection
signal => for when only duplicates in the same folder need to be detected!
(put in folder first, then detect duplicate)
"""
if not settings.FILER_ADDONS_UNFILED_HANDLING.get('move_unfiled', None):
return
Expand Down Expand Up @@ -143,4 +144,3 @@ def filer_duplicates_and_rename(sender, instance, **kwargs):
(and original_filename) programmatically.
"""
check_rename(instance)

Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@
import os
import shutil

from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.core.files.storage import DefaultStorage
from filer.settings import FILER_STORAGES
# from django.core.files.storage import DefaultStorage
from filer import settings as filer_settings

from .base import SubcommandsCommand


# TODO: use filer storage for removing!??
# TODO: also remove private files?
class DeleteThumbnailsCommand(SubcommandsCommand):
help_string = _('Delete all generated thumbnailss.')
help_string = _('Delete all generated thumbnails.')
command_name = 'delete_thumbnails'
storage = DefaultStorage()
storage = filer_settings.FILER_PUBLICMEDIA_STORAGE
storage_private = filer_settings.FILER_PRIVATEMEDIA_STORAGE

def handle(self, *args, **options):
try:
thumb_prefix = FILER_STORAGES['public']['thumbnails']['THUMBNAIL_OPTIONS']['base_dir']
thumb_prefix = filer_settings.FILER_STORAGES['public']['thumbnails']['THUMBNAIL_OPTIONS']['base_dir']
except KeyError:
self.stdout.write("No valid settings found! Aborting.")
return
path = os.path.join(self.storage.location, thumb_prefix)
self.stdout.write("Removing: %s" % path)
shutil.rmtree(path)
if os.path.isdir(path):
self.stdout.write("Removing: %s" % path)
shutil.rmtree(path)
else:
self.stdout.write("Not a directory, exiting: %s" % path)
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import os

from django.utils.translation import ugettext_lazy as _
from django.core.files.storage import DefaultStorage
from filer.settings import FILER_STORAGES
from filer import settings as filer_settings

from .base import SubcommandsCommand

Expand All @@ -15,31 +14,35 @@ class OrphanedFilesCommand(SubcommandsCommand):
inspired and partly copied from:
https://github.com/divio/django-filer/pull/912
"""
help_string = _('List files that have no representation in the database. Public only for now')
help_string = _('List files that have no representation in the database.')
command_name = 'orphaned_files'
storage = DefaultStorage()
prefix = FILER_STORAGES['public']['main']['UPLOAD_TO_PREFIX']
storage_public = filer_settings.FILER_PUBLICMEDIA_STORAGE
storage_private = filer_settings.FILER_PRIVATEMEDIA_STORAGE
prefix_public = filer_settings.FILER_STORAGES['public']['main']['UPLOAD_TO_PREFIX']
prefix_private = filer_settings.FILER_STORAGES['private']['main']['UPLOAD_TO_PREFIX']

def add_arguments(self, parser):
parser.add_argument('--delete', action='store_true', dest='delete',
default=False, help='Delete them!')

def handle(self, *args, **options):
from filer.models.filemodels import File
def walk(absdir, reldir):
child_dirs, files = self.storage.listdir(absdir)
def walk(absdir, reldir, public=True):
storage = self.storage_public if public else self.storage_private
child_dirs, files = storage.listdir(absdir)
for filename in files:
relfilename = os.path.join(reldir, filename)
try:
File.objects.get(file=relfilename)
# self.stdout.write("existing: %s" % relfilename)
File.objects.get(file=relfilename, is_public=public)
except File.DoesNotExist:
absfilename = os.path.join(absdir, filename)
if options['delete']:
self.storage.delete(absfilename)
storage.delete(absfilename)
self.stdout.write(absfilename)

for child in child_dirs:
walk(os.path.join(absdir, child), os.path.join(reldir, child))
walk(os.path.join(absdir, child), os.path.join(reldir, child), public=public)

walk(os.path.join(self.storage.location, self.prefix), self.prefix)
public_path = os.path.join(self.storage_public.location, self.prefix_public)
walk(public_path, self.prefix_public, public=True)
private_path = os.path.join(self.storage_private.location, self.prefix_private)
walk(private_path, self.prefix_private, public=False)
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ def handle(self, *args, **options):
self.stdout.write(force_text(file))
if options['delete']:
file.delete()
self.stdout.write(str(amount))
self.stdout.write("-")
self.stdout.write("%s unused files found." % str(amount))
6 changes: 4 additions & 2 deletions filer_addons/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'filer_addons',
'filer_addons.filer_gui',
'filer_addons.filer_signals',
'filer_addons.filer_utils',
'filer_addons.tests.testapp',
)

Expand All @@ -82,7 +83,8 @@
# DJANGO FILER settings
# =============================================================================

FILER_IS_PUBLIC_DEFAULT = True
FILER_IS_PUBLIC_DEFAULT = False
FILER_ENABLE_PERMISSIONS = True
FILER_PAGINATE_BY = 200
FILER_ALLOW_REGULAR_USERS_TO_ADD_ROOT_FOLDERS = True
FILER_STORAGES = {
Expand All @@ -97,7 +99,7 @@
# 'ENGINE': 'filer.storage.PublicFileSystemStorage',
# 'OPTIONS': {},
'THUMBNAIL_OPTIONS': {
'base_dir': 'thumb',
'base_dir': 'thumbs',
},
},
},
Expand Down
48 changes: 48 additions & 0 deletions filer_addons/tests/test_management_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
from django.test import TestCase
from filer.tests import create_superuser
from filer.models import File, Folder

from filer_addons.tests.utils import create_django_file


class ManagementCommandsTests(TestCase):
def setUp(self):
self.superuser = create_superuser()
self.client.login(username='admin', password='secret')
self.folder = Folder.objects.create(name='test')
self.another_folder = Folder.objects.create(name='test')

def tearDown(self):
self.delete_files()
for folder in Folder.objects.all():
folder.delete()

def delete_files(self):
for f in File.objects.all():
f.delete()

def create_file(self, **kwargs):
"""
two files
kwargs size: tuple, img dimension
kwargs name: filename
:param kwargs:
:return:
"""
filename = 'file.jpg'
if kwargs.get('name', None):
filename = kwargs['name']
size = (50, 50, )
if kwargs.get('size', None):
size = kwargs['size']
django_file = create_django_file(filename=filename, size=size)
file_obj = File.objects.create(
owner=self.superuser,
original_filename=filename,
file=django_file,
)
file_obj.save()
return file_obj

# TODO: write tests!
4 changes: 2 additions & 2 deletions filer_addons/tests/test_signals_consistent_filenames.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import os
from django.test import TestCase, override_settings
from django.test import TestCase
from filer.tests import create_superuser
from filer.models import File, Folder

Expand Down Expand Up @@ -82,4 +82,4 @@ def test_has_correct_name_after_original_name_update(self):
original_name_only, original_suffix = os.path.splitext(file_obj.original_filename)
new_name_only, new_name_suffix = os.path.splitext(os.path.basename(file_obj.file.name))
self.assertTrue(new_name_only.startswith(original_name_only))
self.assertEquals(new_name_suffix, original_suffix)
self.assertEquals(new_name_suffix, original_suffix)
13 changes: 9 additions & 4 deletions filer_addons/tests/test_signals_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ def test_duplicate_detection_disabled(self):
FILER_ADDONS_DUPLICATE_HANDLING=DUPLICATE_HANDLING_ALL_FOLDERS_ALL_FILES
)
def test_duplicates_anywhere(self):
self.create_two_files(duplicates=True, different_name=True, different_folder=True)
self.create_two_files(
duplicates=True,
different_name=True,
different_folder=True,
)
self.assertEquals(File.objects.all().count(), 1)
# same again, still dups!
self.create_two_files(
Expand All @@ -159,7 +163,7 @@ def test_duplicates_greedy(self):
self.create_two_files(duplicates=True, different_name=True)
self.assertEquals(File.objects.all().count(), 2)
with self.settings(
FILER_ADDONS_DUPLICATE_HANDLING=DUPLICATE_HANDLING_ALL_FOLDERS_ALL_FILES_WITH_EXISTING
FILER_ADDONS_DUPLICATE_HANDLING=DUPLICATE_HANDLING_ALL_FOLDERS_ALL_FILES_WITH_EXISTING # noqa
):
self.create_two_files(duplicates=True, different_name=True)
self.assertEquals(File.objects.all().count(), 1)
Expand All @@ -169,13 +173,14 @@ def test_duplicates_greedy(self):
)
def test_duplicates_is_not_greedy(self):
"""
test that normal mode is not greedy: already existing duplicates will not be merged
test that normal mode is not greedy
already existing duplicates will not be merged
:return:
"""
self.create_two_files(duplicates=True, different_name=True)
self.assertEquals(File.objects.all().count(), 2)
with self.settings(
FILER_ADDONS_DUPLICATE_HANDLING=DUPLICATE_HANDLING_ALL_FOLDERS_ALL_FILES
FILER_ADDONS_DUPLICATE_HANDLING=DUPLICATE_HANDLING_ALL_FOLDERS_ALL_FILES # noqa
):
self.create_two_files(duplicates=True, different_name=True)
self.assertEquals(File.objects.all().count(), 2)
23 changes: 23 additions & 0 deletions filer_addons/tests/test_signals_unfiled_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from filer_addons.tests.utils import create_django_file


UNFILED_HANDLING_DISABLED = {
'move_unfiled': False,
}


class UnfiledHandlingTests(TestCase):
def setUp(self):
self.superuser = create_superuser()
Expand Down Expand Up @@ -49,3 +54,21 @@ def create_file(self, **kwargs):
file_obj.save()
return file_obj

def test_unfiled_to_folder(self):
"""
checks if it respects to be disabled
:return:
"""
file_obj = self.create_file()
self.assertNotEquals(file_obj.folder, None)

@override_settings(
FILER_ADDONS_UNFILED_HANDLING=UNFILED_HANDLING_DISABLED
)
def test_disabled_works(self):
"""
checks if it respects to be disabled
:return:
"""
file_obj = self.create_file()
self.assertEquals(file_obj.folder, None)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length=80
exclude = .git,node_modules/*,*/migrations/*,*/static/CACHE/*,*/south_migrations/*
exclude = .git,node_modules/*,.tox/*,*/migrations/*,*/static/CACHE/*,*/south_migrations/*

0 comments on commit 7da8498

Please sign in to comment.