Skip to content

Commit

Permalink
sampleproject, tests working with Django 1.8-1.10
Browse files Browse the repository at this point in the history
Dropped a few deprecation shims too.
  • Loading branch information
David Krisch committed Oct 13, 2016
1 parent df176ab commit bb3a835
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
27 changes: 7 additions & 20 deletions modelclone/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
except ImportError:
# django < 1.7
from django.contrib.admin.util import unquote
from django.conf.urls import patterns, url
from django.conf.urls import url
from django.utils.encoding import force_text
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy as lazy
Expand Down Expand Up @@ -46,18 +46,11 @@ def get_urls(self):
self.model._meta.app_label,
getattr(self.model._meta, 'module_name', getattr(self.model._meta, 'model_name', '')))

if VERSION[1] < 9:
new_urlpatterns = patterns('',
url(r'^(.+)/clone/$',
self.admin_site.admin_view(self.clone_view),
name=url_name)
)
else:
new_urlpatterns = patterns('',
url(r'^(.+)/change/clone/$',
self.admin_site.admin_view(self.clone_view),
name=url_name)
)
new_urlpatterns = [
url(r'^(.+)/change/clone/$',
self.admin_site.admin_view(self.clone_view),
name=url_name)
]

original_urlpatterns = super(ClonableModelAdmin, self).get_urls()

Expand Down Expand Up @@ -141,13 +134,7 @@ def clone_view(self, request, object_id, form_url='', extra_context=None):
prefix = "%s-%s" % (prefix, prefixes[prefix])
initial = []

# Django 1.8 Patch
if hasattr(inline, 'queryset'):
get_queryset = inline.queryset
else:
get_queryset = inline.get_queryset

queryset = get_queryset(request).filter(
queryset = inline.get_queryset(request).filter(
**{FormSet.fk.name: original_obj})
for obj in queryset:
initial.append(model_to_dict(obj, exclude=[obj._meta.pk.name,
Expand Down
28 changes: 20 additions & 8 deletions sampleproject/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os.path

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = ()
MANAGERS = ADMINS
Expand Down Expand Up @@ -34,10 +33,26 @@
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
SECRET_KEY = '8bz7r&amp;h5oy7p&amp;mmb@7d776)zch^miyhgk=tzcdc2s#gtq_ob*-'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'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',
Expand All @@ -47,9 +62,6 @@
)
ROOT_URLCONF = 'sampleproject.urls'
WSGI_APPLICATION = 'sampleproject.wsgi.application'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)

INSTALLED_APPS = (
'django.contrib.auth',
Expand Down
7 changes: 3 additions & 4 deletions sampleproject/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from django.conf.urls import patterns, include, url
from django.conf.urls import include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
)
]
34 changes: 18 additions & 16 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import urlparse

import django
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.admin import site as default_admin_site
from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -73,23 +74,18 @@ def setUp(self):
def test_clone_view_is_wrapped_as_admin_view(self):
model = mock.Mock()
admin_site = mock.Mock()
admin_site.admin_view.return_value = '<wrapped clone view>'
# Fake ClonableModelAdmin.clone_view
the_view = lambda a, b, c: HttpResponse()
admin_site.admin_view.return_value = the_view

model_admin = ClonableModelAdmin(model, admin_site)
clone_view_urlpattern = model_admin.get_urls()[0]

if django.VERSION >= (1, 8):
assert '<wrapped clone view>' == clone_view_urlpattern._callback_str
else:
assert '<wrapped clone view>' == clone_view_urlpattern.callback

assert the_view == clone_view_urlpattern.callback

def test_clone_view_url_name(self):
post_id = self.post.id
if django.VERSION[1] < 9:
expected_url = '/admin/posts/post/{0}/clone/'.format(post_id)
else:
expected_url = '/admin/posts/post/{0}/change/clone/'.format(post_id)
expected_url = '/admin/posts/post/{0}/change/clone/'.format(post_id)

assert reverse('admin:posts_post_clone', args=(post_id,)) == expected_url

Expand Down Expand Up @@ -313,19 +309,25 @@ def test_clone_should_keep_file_path_from_original_object(self):
image = select_element(response, '.field-image p.file-upload a')
document = select_element(response, '.field-document p.file-upload a')

assert '/media/images/img.jpg' == image.get('href')
assert '/media/documents/file.txt' == document.get('href')

if django.VERSION[1] == 10:
assert '/media/images/tests/files/img.jpg' == image.get('href')
assert '/media/documents/tests/files/file.txt' == document.get('href')
else:
assert '/media/images/img.jpg' == image.get('href')
assert '/media/documents/file.txt' == document.get('href')

def test_clone_should_keep_file_path_from_original_object_on_submit(self):
response = self.app.get(self.multimedia_url, user='admin')
response.form.submit()

multimedia = Multimedia.objects.latest('id')

assert 'images/img.jpg' == str(multimedia.image)
assert 'documents/file.txt' == str(multimedia.document)

if django.VERSION[1] == 10:
assert 'images/tests/files/img.jpg' == str(multimedia.image)
assert 'documents/tests/files/file.txt' == str(multimedia.document)
else:
assert 'images/img.jpg' == str(multimedia.image)
assert 'documents/file.txt' == str(multimedia.document)

def test_clone_should_override_file_from_original_object_on_submit_if_new_file_was_chosen(self):
response = self.app.get(self.multimedia_url, user='admin')
Expand Down
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ deps =
django>=1.8,<1.9
{[base]deps}


[testenv:py27-django19]
basepython = python2.7
deps =
django>=1.9,<1.10
{[base]deps}

[testenv:py27-django110]
basepython = python2.7
deps =
django>=1.10,<1.11
{[base]deps}

0 comments on commit bb3a835

Please sign in to comment.