Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
benzkji committed Apr 5, 2022
2 parents 60a4010 + d3cf6d4 commit f0e0563
Show file tree
Hide file tree
Showing 29 changed files with 228 additions and 159 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ root = true
end_of_line = lf
insert_final_newline = true

[*.py]
max_line_length = 80

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py,sass,scss}]
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI

on: [push, pull_request, ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.7, 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Setup Python ${{ matrix.python }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: Install Tox and any other packages
run: pip install tox tox-gh-actions
- name: Run Tox
# Run tox using the version of Python in `PATH`
# run: tox -e py${{ matrix.python }}-${{ matrix.django }}
run: tox

lint:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.9]
steps:
- uses: actions/checkout@v2
- name: Setup Python ${{ matrix.python }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
- name: Install flake8
run: pip install flake8
- name: Run flake8
# Run tox using the version of Python in `PATH`
# run: tox -e py${{ matrix.python }}-${{ matrix.django }}
run: flake8
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release on PYPI
on:
push:
branches: [main]

jobs:
build-and-publish:
runs-on: ubuntu-latest
if: github.repository == 'no-releases-yet/django-filer-addons'
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- name: Install build dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade setuptools wheel twine
- name: Build
run: |
python setup.py sdist
python setup.py bdist_wheel --universal
- name: Publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USER_BNZK }}
TWINE_PASSWORD: ${{ secrets.PYPI_PW_BNZK }}
run: twine upload dist/*
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# django-filer-addons-addons
# django-filer-addons


[![Build Status](https://travis-ci.org/rouxcode/django-filer-addons.svg "Build Status")](https://travis-ci.org/rouxcode/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/)
[![CI](https://github.com/rouxcode/django-filer-addons/actions/workflows/ci.yml/badge.svg)](https://github.com/rouxcode/django-filer-addons/actions/workflows/ci.yml)

[//]: # ([![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
Expand Down
3 changes: 0 additions & 3 deletions filer_addons/filer_gui/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from __future__ import unicode_literals

from django.contrib import admin

from .api import FilerGuiAdmin
from ..models import FilerGuiFile


admin.site.register(FilerGuiFile, FilerGuiAdmin)

7 changes: 4 additions & 3 deletions filer_addons/filer_gui/templatetags/filer_gui_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
# https://stackoverflow.com/questions/51152059/pillow-in-python-wont-let-me-open-image-exceeds-limit
import PIL.Image
PIL.Image.MAX_IMAGE_PIXELS = 933120000
from filer.models import Image

from .. import conf
from filer.models import Image # noqa

from .. import conf # noqa


register = template.Library()
Expand All @@ -22,7 +23,7 @@ def filer_gui_file_thumb(obj, context='change_list'):
thumbnail_options = {'size': conf.FIELD_THUMB_SIZE}
try:
return thumbnailer.get_thumbnail(thumbnail_options).url
except (InvalidImageFormatError, FileNotFoundError) as e:
except (InvalidImageFormatError, FileNotFoundError):
pass
if obj.file and obj.file.path.endswith('.pdf'):
return '/static/filer/icons/file-pdf.svg'
Expand Down
7 changes: 5 additions & 2 deletions filer_addons/filer_signals/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import unicode_literals
import os

from django.core.exceptions import ObjectDoesNotExist

from . import conf # noqa need the settings

from django.db.models.signals import pre_save, post_save
Expand Down Expand Up @@ -165,11 +167,12 @@ def filer_prevent_rename_orphans(sender, instance, **kwargs):
"""
if not conf.FILER_ADDONS_REPLACE_FIX:
return
# Delete old file(s) when updating the file via: admin > advanced > replace file
# Delete old file(s) when updating the file via:
# admin > advanced > replace file
try:
from_db = File.objects.get(id=instance.id)
if from_db.file != instance.file:
from_db.file.delete(save=False)
except:
except ObjectDoesNotExist:
pass
return
12 changes: 6 additions & 6 deletions filer_addons/filer_utils/management/commands/filer_addons.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from collections import OrderedDict

import filer_addons
from filer_addons.filer_utils.management.commands.subcommands.import_existing_files import ImportExistingFilesCommand
from filer_addons.filer_utils.management.commands.subcommands.resolve_duplicates import ResolveDuplicatesCommand
from filer_addons.filer_utils.management.commands.subcommands.stats import StatsCommand
from filer_addons.filer_utils.management.commands.subcommands.import_existing_files import ImportExistingFilesCommand # noqa
from filer_addons.filer_utils.management.commands.subcommands.resolve_duplicates import ResolveDuplicatesCommand # noqa
from filer_addons.filer_utils.management.commands.subcommands.stats import \
StatsCommand
from .subcommands.base import SubcommandsCommand
from .subcommands.delete_thumbnails import DeleteThumbnailsCommand
from .subcommands.unused_files import UnusedFilesCommand
Expand All @@ -32,5 +31,6 @@ def get_version(self):
return filer_addons.__version__

def add_arguments(self, parser):
parser.add_argument('--version', action='version', version=self.get_version())
parser.add_argument('--version', action='version',
version=self.get_version())
super(Command, self).add_arguments(parser)
45 changes: 24 additions & 21 deletions filer_addons/filer_utils/management/commands/subcommands/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import os

from collections import OrderedDict

from django.core.management.base import BaseCommand, CommandParser
from django.core.management.color import no_style

from filer_addons.compat import DJANGO_2_0, DJANGO_2_2, DJANGO_3_0
from filer_addons.compat import DJANGO_2_0, DJANGO_2_2


def add_builtin_arguments(parser):
Expand All @@ -21,29 +19,34 @@ def add_builtin_arguments(parser):

# These are taking "as-is" from Django's management base
# management command.
parser.add_argument('-v', '--verbosity', action='store', dest='verbosity', default='1',
type=int, choices=[0, 1, 2, 3],
help='Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output')
parser.add_argument('-v', '--verbosity', action='store', dest='verbosity',
default='1',
type=int, choices=[0, 1, 2, 3],
help='Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output') # noqa
parser.add_argument('--settings',
help=(
'The Python path to a settings module, e.g. '
'"myproject.settings.main". If this isn\'t provided, the '
'DJANGO_SETTINGS_MODULE environment variable will be used.'
),
)
help=(
'The Python path to a settings module, e.g. '
'"myproject.settings.main". '
'If this isn\'t provided, the '
'DJANGO_SETTINGS_MODULE environment variable'
' will be used.'
),
)
parser.add_argument('--pythonpath',
help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".')
help='A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".') # noqa
parser.add_argument('--traceback', action='store_true',
help='Raise on CommandError exceptions')
parser.add_argument('--no-color', action='store_true', dest='no_color', default=False,
help="Don't colorize the command output.")
help='Raise on CommandError exceptions')
parser.add_argument('--no-color', action='store_true', dest='no_color',
default=False,
help="Don't colorize the command output.")
# if DJANGO_2_2 or DJANGO_3_0:
parser.add_argument('--force-color', action='store_true', dest='force_color', default=False,
help="Colorize the command output.")
parser.add_argument('--force-color', action='store_true',
dest='force_color', default=False,
help="Colorize the command output.")
if not DJANGO_2_2:
parser.add_argument('--skip-checks', action='store_true', dest='skip_checks', default=False,
help="Skip the checks.")

parser.add_argument('--skip-checks', action='store_true',
dest='skip_checks', default=False,
help="Skip the checks.")


class SubcommandsCommand(BaseCommand):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

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 import settings as filer_settings

from .base import SubcommandsCommand
Expand All @@ -23,13 +18,14 @@ class DeleteThumbnailsCommand(SubcommandsCommand):
def handle(self, *args, **options):
self._remove_thumbs(self.storage, 'public')
self.stdout.write("Only removing public thumbnails for now. Bye.")
# self._remove_thumbs(self.storage_private, 'private')

def _remove_thumbs(self, storage, public_private):
try:
thumb_prefix = filer_settings.FILER_STORAGES[public_private]['thumbnails']['THUMBNAIL_OPTIONS']['base_dir']
thumb_prefix = filer_settings.FILER_STORAGES[public_private]['thumbnails']['THUMBNAIL_OPTIONS']['base_dir'] # noqa
except KeyError:
self.stdout.write("No valid settings found ({} storage)! Aborting.".format(public_private))
self.stdout.write(
"No valid settings found ({} storage)! Aborting.".format(
public_private))
return
path = os.path.join(storage.location, thumb_prefix)
if os.path.isdir(path):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re

from filer.models import File, Image, Folder
from filer_addons.filer_utils.management.commands.subcommands.base import SubcommandsCommand
from filer_addons.filer_utils.management.commands.subcommands.base import SubcommandsCommand # noqa


def is_image(filename):
Expand All @@ -12,8 +12,9 @@ def is_image(filename):


# TODO: via setting
file_exclude_pattern = r'(_fb_thumb\.)|(_fancybox_thumb\.)|(_home_image\.)|(_left_col_small\.)' \
r'|(_partner\.)|(_people\.)|(_small\.)|(_thumbnail\.)'
file_exclude_pattern = r'(_fb_thumb\.)|(_fancybox_thumb\.)|(_home_image\.)' \
r'|(_left_col_small\.)' \
r'|(_partner\.)|(_people\.)|(_small\.)|(_thumbnail\.)'
file_exclude_pattern = r'_q85'

folder_exclude_pattern = r'(_big)|(_thumb)'
Expand Down Expand Up @@ -41,7 +42,7 @@ def handle(self, *args, **options):

from filer import settings as filer_settings
self.storage_public = filer_settings.FILER_PUBLICMEDIA_STORAGE
self.prefix_public = filer_settings.FILER_STORAGES['public']['main']['UPLOAD_TO_PREFIX']
self.prefix_public = filer_settings.FILER_STORAGES['public']['main']['UPLOAD_TO_PREFIX'] # noqa

def walk(absdir, reldir, db_folder):
print("walk %s" % db_folder)
Expand All @@ -54,8 +55,8 @@ def walk(absdir, reldir, db_folder):
if not len(matches):
filename_with_relpath = os.path.join(reldir, filename)
# media_root = os.path.join(self.storage_public.location)
# filename_with_abspath = os.path.join(media_root, filename_with_relpath)
# django_file = DjangoFile(open(filename_with_abspath, 'rb'), name=filename)
# filename_with_abspath = os.path.join(media_root, filename_with_relpath) # noqa
# django_file = DjangoFile(open(filename_with_abspath, 'rb'), name=filename) # noqa
file_cls = File
if is_image(filename):
file_cls = Image
Expand All @@ -69,8 +70,16 @@ def walk(absdir, reldir, db_folder):
kwargs = {}
if db_folder:
kwargs['parent'] = db_folder
new_folder, created = Folder.objects.get_or_create(name=child, **kwargs)
walk(os.path.join(absdir, child), os.path.join(reldir, child), new_folder)
new_folder, created = Folder.objects.get_or_create(
name=child, **kwargs
)
walk(
os.path.join(absdir, child),
os.path.join(reldir, child),
new_folder,
)

public_path = os.path.join(self.storage_public.location, self.prefix_public)
public_path = os.path.join(
self.storage_public.location, self.prefix_public
)
walk(public_path, self.prefix_public, None)
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

import os

from django.utils.translation import ugettext_lazy as _
Expand All @@ -18,15 +15,18 @@ class OrphanedFilesCommand(SubcommandsCommand):
command_name = 'orphaned_files'
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']
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, public=True):
storage = self.storage_public if public else self.storage_private
child_dirs, files = storage.listdir(absdir)
Expand All @@ -42,11 +42,13 @@ def walk(absdir, reldir, public=True):
except File.MultipleObjectsReturned:
pass
for child in child_dirs:
walk(os.path.join(absdir, child), os.path.join(reldir, child), public=public)
walk(os.path.join(absdir, child), os.path.join(reldir, child),
public=public)

public_path = os.path.join(self.storage_public.location, self.prefix_public)
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)
private_path = os.path.join(self.storage_private.location,
self.prefix_private)
if os.path.isdir(private_path):
walk(private_path, self.prefix_private, public=False)

Loading

0 comments on commit f0e0563

Please sign in to comment.