Skip to content

Django 4.0 and 4.1 support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
42 changes: 24 additions & 18 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
language: python

python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "3.10"

sudo: false

env:
- TOX_ENV=py27-django1.8
- TOX_ENV=py27-django1.9
- TOX_ENV=py27-django1.10
- TOX_ENV=py27-django1.11
- TOX_ENV=py34-django1.8
- TOX_ENV=py34-django1.9
- TOX_ENV=py34-django1.10
- TOX_ENV=py35-django1.8
- TOX_ENV=py35-django1.9
- TOX_ENV=py35-django1.10
- TOX_ENV=py35-django1.11
- TOX_ENV=py36-django3.0.14
- TOX_ENV=py36-django3.1.14
- TOX_ENV=py36-django3.2.18
- TOX_ENV=py37-django3.0.14
- TOX_ENV=py37-django3.1.14
- TOX_ENV=py37-django3.2.18
- TOX_ENV=py38-django3.0.14
- TOX_ENV=py38-django3.1.14
- TOX_ENV=py38-django3.2.18
- TOX_ENV=py39-django3.0.14
- TOX_ENV=py39-django3.1.14
- TOX_ENV=py39-django3.2.18
- TOX_ENV=py38-django4.0.14
- TOX_ENV=py38-django4.1.7
- TOX_ENV=py39-django4.0.10
- TOX_ENV=py39-django4.1.7
- TOX_ENV=py310-django4.0.10
- TOX_ENV=py310-django4.1.7


matrix:
fast_finish: true

before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- sleep 6 # give xvfb some time to start

install:
- pip install tox
- pip install "virtualenv<14.0.0"

script:
- tox -e $TOX_ENV
4 changes: 3 additions & 1 deletion admin_async_upload/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import fnmatch
import tempfile

from django.contrib.contenttypes.models import ContentType
from django.core.files import File
from django.utils.functional import cached_property

Expand Down Expand Up @@ -39,7 +40,8 @@ def chunk_storage(self):

@property
def storage_filename(self):
return self.resumable_storage.full_filename(self.filename, self.upload_to)
instance = self.field.model.objects.filter(pk=self.params.get("instance_id")).first()
return self.resumable_storage.full_filename(self.filename, self.upload_to, instance=instance)

@property
def upload_to(self):
Expand Down
11 changes: 7 additions & 4 deletions admin_async_upload/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.core.files.storage import get_storage_class

from django.conf import settings
from django.utils.encoding import force_text, force_str
from django.utils.encoding import force_str


class ResumableStorage(object):
Expand Down Expand Up @@ -40,7 +40,10 @@ def get_persistent_storage(self, *args, **kwargs):
storage_class = get_storage_class(self.persistent_storage_class_name)
return storage_class(*args, **kwargs)

def full_filename(self, filename, upload_to):
dirname = force_text(datetime.datetime.now().strftime(force_str(upload_to)))
filename = posixpath.join(dirname, filename)
def full_filename(self, filename, upload_to, instance=None):
if callable(upload_to):
filename = upload_to(instance, filename)
else:
dirname = force_str(datetime.datetime.now().strftime(force_str(upload_to)))
filename = posixpath.join(dirname, filename)
return self.get_persistent_storage().generate_filename(filename)
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
query: {
csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val(),
field_name: '{{ field_name }}', {# FIXME: this probably should be checked at run time for added inlines #}
content_type_id: '{{ content_type_id }}' {# FIXME: this probably should be checked at run time for added inlines #}
content_type_id: '{{ content_type_id }}', {# FIXME: this probably should be checked at run time for added inlines #}
instance_id: '{{ instance_id }}'
},
simultaneousUploads: {{ simultaneous_uploads }}, //3 is better, 1 is used for local testing;
});
Expand Down
4 changes: 2 additions & 2 deletions admin_async_upload/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf.urls import url
from django.urls import path

from . import views

urlpatterns = [
url(r'^upload/$', views.admin_resumable, name='admin_resumable'),
path('upload/', views.admin_resumable, name='admin_resumable'),
]
2 changes: 1 addition & 1 deletion admin_async_upload/validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from admin_async_upload.storage import ResumableStorage
from os.path import splitext
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _


class StorageFileValidator(object):
Expand Down
7 changes: 5 additions & 2 deletions admin_async_upload/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from django.template import loader
from django.templatetags.static import static
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy
from django.utils.translation import gettext_lazy

from admin_async_upload.storage import ResumableStorage


class ResumableBaseWidget(FileInput):
template_name = 'admin_resumable/admin_file_input.html'
clear_checkbox_label = ugettext_lazy('Clear')
clear_checkbox_label = gettext_lazy('Clear')

def render(self, name, value, attrs=None, **kwargs):
persistent_storage = ResumableStorage().get_persistent_storage()
Expand All @@ -33,6 +33,8 @@ def render(self, name, value, attrs=None, **kwargs):
simultaneous_uploads = getattr(settings, 'ADMIN_SIMULTANEOUS_UPLOADS', 3)

content_type_id = ContentType.objects.get_for_model(self.attrs['model']).id
instance = self.attrs.get('instance')
instance_id = instance.pk if instance else None

context = {
'name': name,
Expand All @@ -42,6 +44,7 @@ def render(self, name, value, attrs=None, **kwargs):
'show_thumb': show_thumb,
'field_name': self.attrs['field_name'],
'content_type_id': content_type_id,
'instance_id': instance_id,
'file_url': file_url,
'file_name': file_name,
'simultaneous_uploads': simultaneous_uploads,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
install_requires=[
'Django>=1.8',
'Django>=3.0.14',
],
tests_require=[
'pytest-django',
Expand Down
94 changes: 45 additions & 49 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import pytest
import os
from selenium import webdriver
from pyvirtualdisplay import Display

browsers = {
'firefox': webdriver.Firefox,
"firefox": webdriver.Firefox,
#'PhantomJS': webdriver.PhantomJS,
#'chrome': webdriver.Chrome,
}

browser_options = {"firefox": webdriver.FirefoxOptions()}

@pytest.fixture(scope='session',
params=browsers.keys())
browser_options["firefox"].add_argument("--headless")


@pytest.fixture(scope="session", params=browsers.keys())
def driver(request):
display = Display(visible=0, size=(1024, 768))
display.start()
b = browsers[request.param]()
b = browsers[request.param](options=browser_options[request.param])

request.addfinalizer(lambda *args: b.quit())

Expand All @@ -30,66 +30,62 @@ def pytest_configure():
DEBUG=False,
DEBUG_PROPAGATE_EXCEPTIONS=True,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
}
"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}
},
SITE_ID=1,
SECRET_KEY='not very secret in tests',
SECRET_KEY="not very secret in tests",
USE_I18N=True,
USE_L10N=True,
STATIC_URL='/static/',
ROOT_URLCONF='tests.urls',
STATIC_URL="/static/",
ROOT_URLCONF="tests.urls",
TEMPLATE_LOADERS=(
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
),
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.contrib.auth.context_processors.auth",
"django.template.context_processors.debug",
"django.template.context_processors.i18n",
"django.template.context_processors.media",
"django.template.context_processors.static",
"django.template.context_processors.tz",
"django.contrib.messages.context_processors.messages",
],
},
},
],
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
MIDDLEWARE=(
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
),
INSTALLED_APPS=(
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',

'admin_async_upload',
'tests',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.messages",
"django.contrib.staticfiles",
"admin_async_upload",
"tests",
),
PASSWORD_HASHERS=(
'django.contrib.auth.hashers.MD5PasswordHasher',
),
MEDIA_ROOT=os.path.join(os.path.dirname(__file__), 'media')
PASSWORD_HASHERS=("django.contrib.auth.hashers.MD5PasswordHasher",),
MEDIA_ROOT=os.path.join(os.path.dirname(__file__), "media"),
)
try:
import django

django.setup()
except AttributeError:
pass
Loading