Skip to content

Commit

Permalink
style cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnyrose committed Sep 12, 2024
1 parent ee66a80 commit c88e3b9
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ env/*
.vscode/*
README.html
*.txt
.venv/*
5 changes: 3 additions & 2 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[pytest]
DJANGO_SETTINGS_MODULE = test.settings
pythonpath = . src
addopts = -n auto -v --cov-report=term-missing --cov=django_cleanup --forked
addopts = -v --cov-report=term-missing --cov=django_cleanup
markers =
CleanupSelectedConfig: marks test as using the CleanupSelectedConfig app config
cleanup_selected_config: marks test as using the CleanupSelectedConfig app config
django_storage: change django storage backends
18 changes: 11 additions & 7 deletions src/django_cleanup/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def fields_dict_default():
return {}
FIELDS_FIELDS = defaultdict(fields_dict_default)
FIELDS_STORAGE = defaultdict(fields_dict_default)
DOTTED_PATH = '{klass.__module__}.{klass.__qualname__}'


# cache init ##
Expand Down Expand Up @@ -82,7 +81,7 @@ def fields_for_model_instance(instance, using=None):
deferred_fields = instance.get_deferred_fields()

for field_name in get_fields_for_model(model_name, exclude=deferred_fields):
fieldfile = getattr(instance, field_name, None)
fieldfile = getattr(instance, field_name)
yield field_name, fieldfile.__class__(using, fieldfile.field, fieldfile.name)


Expand All @@ -104,22 +103,26 @@ def get_field_storage(model_name, field_name):

def get_dotted_path(object_):
'''get the dotted path for an object'''
return DOTTED_PATH.format(klass=object_.__class__)
klass = object_.__class__
return f'{klass.__module__}.{klass.__qualname__}'


def get_model_name(model):
'''returns a unique model name'''
return '{opt.app_label}.{opt.model_name}'.format(opt=model._meta)
opt = model._meta
return f'{opt.app_label}.{opt.model_name}'


def get_mangled_ignore(model):
'''returns a mangled attribute name specific to the model for ignore functionality'''
return '_{opt.model_name}__{opt.app_label}_cleanup_ignore'.format(opt=model._meta)
opt = model._meta
return f'_{opt.model_name}__{opt.app_label}_cleanup_ignore'


def get_mangled_select(model):
'''returns a mangled attribute name specific to the model for select functionality'''
return '_{opt.model_name}__{opt.app_label}_cleanup_select'.format(opt=model._meta)
opt = model._meta
return f'_{opt.model_name}__{opt.app_label}_cleanup_select'


# booleans ##
Expand All @@ -132,7 +135,8 @@ def model_has_filefields(model_name):

def ignore_model(model, select_mode):
'''Check if a model should be ignored'''
return (not hasattr(model, get_mangled_select(model))) if select_mode else hasattr(model, get_mangled_ignore(model))
return ((not hasattr(model, get_mangled_select(model)))
if select_mode else hasattr(model, get_mangled_ignore(model)))


# instance functions ##
Expand Down
10 changes: 5 additions & 5 deletions src/django_cleanup/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ def run_on_commit():
def connect():
'''Connect signals to the cleanup models'''
for model in cache.cleanup_models():
key = '{{}}_django_cleanup_{}'.format(cache.get_model_name(model))
suffix = f'_django_cleanup_{cache.get_model_name(model)}'
post_init.connect(cache_original_post_init, sender=model,
dispatch_uid=key.format('post_init'))
dispatch_uid=f'post_init{suffix}')
pre_save.connect(fallback_pre_save, sender=model,
dispatch_uid=key.format('pre_save'))
dispatch_uid=f'pre_save{suffix}')
post_save.connect(delete_old_post_save, sender=model,
dispatch_uid=key.format('post_save'))
dispatch_uid=f'post_save{suffix}')
post_delete.connect(delete_all_post_delete, sender=model,
dispatch_uid=key.format('post_delete'))
dispatch_uid=f'post_delete{suffix}')
34 changes: 22 additions & 12 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import copy
import os
import shutil

from django.conf import settings
from django.conf import settings as django_settings
from django.db.models.signals import post_delete, post_init, post_save, pre_save

import pytest
Expand All @@ -11,29 +12,38 @@
from .testing_helpers import get_random_pic_name


pytest_plugins = ("test.pytest_plugin",)
pytest_plugins = ()

def pytest_collection_modifyitems(items):
for item in items:
item.add_marker(pytest.mark.django_db(transaction=True))


@pytest.fixture(autouse=True)
def setup_django_cleanup_state(request):
def setup_django_cleanup_state(request, settings):
for model in cache.cleanup_models():
key = '{{}}_django_cleanup_{}'.format(cache.get_model_name(model))
suffix = f'_django_cleanup_{cache.get_model_name(model)}'
post_init.disconnect(None, sender=model,
dispatch_uid=key.format('post_init'))
dispatch_uid=f'post_init{suffix}')
pre_save.disconnect(None, sender=model,
dispatch_uid=key.format('pre_save'))
dispatch_uid=f'pre_save{suffix}')
post_save.disconnect(None, sender=model,
dispatch_uid=key.format('post_save'))
dispatch_uid=f'post_save{suffix}')
post_delete.disconnect(None, sender=model,
dispatch_uid=key.format('post_delete'))
dispatch_uid=f'post_delete{suffix}')
cache.FIELDS.clear()
selectedConfig = any(m.name == 'CleanupSelectedConfig' for m in request.node.iter_markers())

cache.prepare(selectedConfig)
cache.prepare(request.node.get_closest_marker('cleanup_selected_config') is not None)
handlers.connect()

stroage_marker = request.node.get_closest_marker('django_storage')
if stroage_marker is not None:
storages = copy.deepcopy(settings.STORAGES)
for key, value in stroage_marker.kwargs.items():
storages[key]['BACKEND'] = value
settings.STORAGES = storages


@pytest.fixture(params=[settings.MEDIA_ROOT])
@pytest.fixture(params=[django_settings.MEDIA_ROOT])
def picture(request):
src = os.path.join(request.param, 'pic.jpg')
dst = os.path.join(request.param, get_random_pic_name())
Expand Down
9 changes: 0 additions & 9 deletions test/pytest_plugin/__init__.py

This file was deleted.

4 changes: 1 addition & 3 deletions test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ pytest
pytest-django
pytest-pythonpath
pytest-cov
pytest-xdist
pytest-forked
asgiref<3.7.0
asgiref
6 changes: 3 additions & 3 deletions test/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
except (django.core.exceptions.AppRegistryNotReady, django.core.exceptions.ImproperlyConfigured):
INSTALLED_APPS = INSTALLED_APPS + INSTALLED_APPS_INTEGRATION


MIDDLEWARE_CLASSES = []

SECRET_KEY = '123'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

## workaround: https://github.com/SmileyChris/easy-thumbnails/issues/641#issuecomment-2291098096
THUMBNAIL_DEFAULT_STORAGE_ALIAS = 'default'
Loading

0 comments on commit c88e3b9

Please sign in to comment.