From a6b320e8eb2646ea359e1b85e10e04cf858c96be Mon Sep 17 00:00:00 2001 From: Nicholas Wolff Date: Wed, 18 Oct 2017 18:01:06 +0200 Subject: [PATCH 1/4] Fix empty Welcome username for custom user model A django custom user model does not always have a `username` attribute, but the `get_username()` method is present for all user implementations. --- suit/templates/admin/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/suit/templates/admin/base.html b/suit/templates/admin/base.html index ca917f88..738e6994 100644 --- a/suit/templates/admin/base.html +++ b/suit/templates/admin/base.html @@ -68,7 +68,7 @@ {% trans 'Welcome,' %} {% filter force_escape %} - {% firstof user.first_name user.username %}{% endfilter %}. + {% firstof user.first_name user.get_username %}{% endfilter %}. {% endblock %} {% block userlinks %} From 902df806e6e9a4a9420f26f9c2acd3c70e92f31e Mon Sep 17 00:00:00 2001 From: kidsdev Date: Sun, 3 Dec 2017 15:31:11 +0700 Subject: [PATCH 2/4] Fix Django 2.0 compatibility problems --- suit/templatetags/suit_menu.py | 8 ++++++-- suit/templatetags/suit_tags.py | 14 ++++++++++---- suit/tests/mixins.py | 5 ++++- suit/tests/settings.py | 16 +++++++++++++--- suit/tests/templates/form_tabs.py | 5 ++++- suit/tests/templatetags/suit_list.py | 5 ++++- suit/tests/templatetags/suit_menu.py | 5 ++++- suit/tests/templatetags/suit_tags.py | 2 +- suit/tests/urls/__init__.py | 2 +- suit/tests/widgets.py | 18 ++++++++++++++---- suit/widgets.py | 12 +++++++++--- 11 files changed, 70 insertions(+), 22 deletions(-) diff --git a/suit/templatetags/suit_menu.py b/suit/templatetags/suit_menu.py index 4ac1e639..02f7fecb 100644 --- a/suit/templatetags/suit_menu.py +++ b/suit/templatetags/suit_menu.py @@ -2,7 +2,10 @@ from django.contrib import admin from django.contrib.admin import AdminSite from django.http import HttpRequest -from django.core.urlresolvers import reverse, resolve +try: + from django.urls import reverse, resolve +except: + from django.core.urlresolvers import reverse, resolve try: from django.utils.six import string_types @@ -18,7 +21,8 @@ register = template.Library() -@register.assignment_tag(takes_context=True) +# @register.assignment_tag(takes_context=True) +@register.simple_tag(takes_context=True) def get_menu(context, request): """ :type request: HttpRequest diff --git a/suit/templatetags/suit_tags.py b/suit/templatetags/suit_tags.py index 1e0f97fb..32255313 100644 --- a/suit/templatetags/suit_tags.py +++ b/suit/templatetags/suit_tags.py @@ -1,7 +1,10 @@ import itertools from django import template from django.core.exceptions import ObjectDoesNotExist -from django.core.urlresolvers import NoReverseMatch, reverse +try: + from django.urls import NoReverseMatch, reverse +except: + from django.core.urlresolvers import NoReverseMatch, reverse from django.db.models import ForeignKey from django.template.defaulttags import NowNode from django.utils.safestring import mark_safe @@ -81,12 +84,14 @@ def suit_bc(*args): return utils.value_by_version(args) -@register.assignment_tag +# @register.assignment_tag +@register.simple_tag def suit_bc_value(*args): return utils.value_by_version(args) -@register.assignment_tag +# @register.assignment_tag +@register.simple_tag def admin_extra_filters(cl): """ Return the dict of used filters which is not included in list_filters form """ @@ -95,7 +100,8 @@ def admin_extra_filters(cl): return dict((k, v) for k, v in cl.params.items() if k not in used_parameters) -@register.assignment_tag +# @register.assignment_tag +@register.simple_tag def suit_django_version(): return django_version diff --git a/suit/tests/mixins.py b/suit/tests/mixins.py index d3daab6d..5380d078 100644 --- a/suit/tests/mixins.py +++ b/suit/tests/mixins.py @@ -2,7 +2,10 @@ from django.contrib.auth.models import User from django.core.management import CommandError from django.core.management import call_command -from django.core.urlresolvers import reverse +try: + from django.urls import reverse +except: + from django.core.urlresolvers import reverse from django.test import TestCase from random import randint diff --git a/suit/tests/settings.py b/suit/tests/settings.py index 64ce5a6c..0d133ab1 100644 --- a/suit/tests/settings.py +++ b/suit/tests/settings.py @@ -27,13 +27,23 @@ 'django.template.loaders.app_directories.Loader', ) -MIDDLEWARE_CLASSES = ( - 'django.middleware.common.CommonMiddleware', +# 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', +] ROOT_URLCONF = 'suit.tests.urls' TEMPLATE_DIRS = () diff --git a/suit/tests/templates/form_tabs.py b/suit/tests/templates/form_tabs.py index 402f98da..a69ce490 100644 --- a/suit/tests/templates/form_tabs.py +++ b/suit/tests/templates/form_tabs.py @@ -1,5 +1,8 @@ from django.contrib import admin -from django.core.urlresolvers import reverse +try: + from django.urls import reverse +except: + from django.core.urlresolvers import reverse from django.utils.translation import ugettext from suit.tests.mixins import ModelsTestCaseMixin, UserTestCaseMixin from suit.tests.models import Book, BookAdmin, test_app_label diff --git a/suit/tests/templatetags/suit_list.py b/suit/tests/templatetags/suit_list.py index 022834c5..53507602 100644 --- a/suit/tests/templatetags/suit_list.py +++ b/suit/tests/templatetags/suit_list.py @@ -1,6 +1,9 @@ from django.contrib.admin import ModelAdmin from django.contrib.admin.templatetags.admin_list import result_list -from django.core.urlresolvers import reverse +try: + from django.urls import reverse +except: + from django.core.urlresolvers import reverse from suit.templatetags.suit_list import paginator_number, paginator_info, \ pagination, suit_list_filter_select, headers_handler, dict_to_attrs, \ result_row_attrs, cells_handler diff --git a/suit/tests/templatetags/suit_menu.py b/suit/tests/templatetags/suit_menu.py index 3980786e..b5127202 100644 --- a/suit/tests/templatetags/suit_menu.py +++ b/suit/tests/templatetags/suit_menu.py @@ -1,6 +1,9 @@ from django.conf import settings from django.contrib.auth.models import Permission -from django.core.urlresolvers import reverse +try: + from django.urls import reverse +except: + from django.core.urlresolvers import reverse from suit.templatetags.suit_menu import get_menu from suit.tests.mixins import ModelsTestCaseMixin, UserTestCaseMixin from suit.tests.models import test_app_label diff --git a/suit/tests/templatetags/suit_tags.py b/suit/tests/templatetags/suit_tags.py index ba4958a3..737b79b2 100644 --- a/suit/tests/templatetags/suit_tags.py +++ b/suit/tests/templatetags/suit_tags.py @@ -21,7 +21,7 @@ def __str__(self): @python_2_unicode_compatible class City(models.Model): name = models.CharField(max_length=64) - country = models.ForeignKey(Country) + country = models.ForeignKey(Country, on_delete=models.CASCADE) def __str__(self): return self.name diff --git a/suit/tests/urls/__init__.py b/suit/tests/urls/__init__.py index ea0bd61d..7fbf8221 100644 --- a/suit/tests/urls/__init__.py +++ b/suit/tests/urls/__init__.py @@ -12,5 +12,5 @@ ) except ImportError: # Django 1.10+ urlpatterns = [ - url(r'^admin/', include(admin.site.urls)), + url(r'^admin/', admin.site.urls), ] diff --git a/suit/tests/widgets.py b/suit/tests/widgets.py index 40f1a5a2..2b3e7a2a 100644 --- a/suit/tests/widgets.py +++ b/suit/tests/widgets.py @@ -71,7 +71,7 @@ def test_SuitDateWidget_with_existing_placeholder_attr(self): def get_SuitDateWidget_output(self): return '
' def test_SuitDateWidget_output(self): @@ -99,7 +99,7 @@ def test_SuitTimeWidget_with_existing_placeholder_attr(self): def get_SuitTimeWidget_output(self): return '
' def test_SuitTimeWidget_output(self): @@ -109,11 +109,21 @@ def test_SuitTimeWidget_output(self): self.get_SuitTimeWidget_output(), output) + def get_SuitDateWidget_output2(self): + return '' + + def get_SuitTimeWidget_output2(self): + return '' + def test_SuitSplitDateTimeWidget(self): ssdtw = SuitSplitDateTimeWidget() output = ssdtw.render('sdw', '') - dwo = self.get_SuitDateWidget_output().replace('sdw', 'sdw_0') - two = self.get_SuitTimeWidget_output().replace('sdw', 'sdw_1') + dwo = self.get_SuitDateWidget_output2().replace('sdw', 'sdw_0') + two = self.get_SuitTimeWidget_output2().replace('sdw', 'sdw_1') self.assertHTMLEqual(output, '
%s %s
' % (dwo, two)) diff --git a/suit/widgets.py b/suit/widgets.py index ef301d8b..35bc8588 100644 --- a/suit/widgets.py +++ b/suit/widgets.py @@ -136,9 +136,15 @@ def __init__(self, attrs=None): widgets = [SuitDateWidget, SuitTimeWidget] forms.MultiWidget.__init__(self, widgets, attrs) - def format_output(self, rendered_widgets): - out_tpl = '
%s %s
' - return mark_safe(out_tpl % (rendered_widgets[0], rendered_widgets[1])) + def render(self, name, value, attrs=None): + html = super(SuitSplitDateTimeWidget, self).render(name, value, attrs) + + out_tpl = '
%s
' + return mark_safe(out_tpl % html) + + # def format_output(self, rendered_widgets): + # out_tpl = '
%s %s
' + # return mark_safe(out_tpl % (rendered_widgets[0], rendered_widgets[1])) def _make_attrs(attrs, defaults=None, classes=None): From b3a2c0caac7d728c037c527d9fd610709dd0a2e9 Mon Sep 17 00:00:00 2001 From: kidsdev Date: Sun, 3 Dec 2017 22:13:27 +0700 Subject: [PATCH 3/4] Fix Test for django < 2.0 --- suit/tests/widgets.py | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/suit/tests/widgets.py b/suit/tests/widgets.py index 2b3e7a2a..f17de7a5 100644 --- a/suit/tests/widgets.py +++ b/suit/tests/widgets.py @@ -4,7 +4,7 @@ AutosizedTextarea from django.utils.translation import ugettext as _ from django.contrib.admin.templatetags.admin_static import static - +import django class WidgetsTestCase(TestCase): def test_NumberInput(self): @@ -69,10 +69,16 @@ def test_SuitDateWidget_with_existing_placeholder_attr(self): self.assertEqual('p', sdw.attrs['placeholder']) def get_SuitDateWidget_output(self): - return '
' + if float(django.get_version()) >= float('2.0'): + return '
' + else: + return '
' def test_SuitDateWidget_output(self): sdw = SuitDateWidget(attrs={'placeholder': 'Date'}) @@ -97,10 +103,16 @@ def test_SuitTimeWidget_with_existing_placeholder_attr(self): self.assertEqual('p', sdw.attrs['placeholder']) def get_SuitTimeWidget_output(self): - return '
' + if float(django.get_version()) >= float('2.0'): + return '
' + else: + return '
' def test_SuitTimeWidget_output(self): sdw = SuitTimeWidget(attrs={'placeholder': 'Time'}) @@ -122,8 +134,12 @@ def get_SuitTimeWidget_output2(self): def test_SuitSplitDateTimeWidget(self): ssdtw = SuitSplitDateTimeWidget() output = ssdtw.render('sdw', '') - dwo = self.get_SuitDateWidget_output2().replace('sdw', 'sdw_0') - two = self.get_SuitTimeWidget_output2().replace('sdw', 'sdw_1') + if float(django.get_version()) >= float('2.0'): + dwo = self.get_SuitDateWidget_output2().replace('sdw', 'sdw_0') + two = self.get_SuitTimeWidget_output2().replace('sdw', 'sdw_1') + else: + dwo = self.get_SuitDateWidget_output().replace('sdw', 'sdw_0') + two = self.get_SuitTimeWidget_output().replace('sdw', 'sdw_1') self.assertHTMLEqual(output, '
%s %s
' % (dwo, two)) From 8d0bf3b004bfae4311a11f3c8a20b2d43b1d3b35 Mon Sep 17 00:00:00 2001 From: kidsdev Date: Sun, 3 Dec 2017 22:42:09 +0700 Subject: [PATCH 4/4] Fix Check django Version --- suit/templatetags/suit_menu.py | 10 +++++++--- suit/templatetags/suit_tags.py | 16 +++++++++------ suit/tests/settings.py | 36 ++++++++++++++++++---------------- suit/tests/widgets.py | 6 +++--- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/suit/templatetags/suit_menu.py b/suit/templatetags/suit_menu.py index 02f7fecb..fdac3fd1 100644 --- a/suit/templatetags/suit_menu.py +++ b/suit/templatetags/suit_menu.py @@ -1,7 +1,9 @@ +import django from django import template from django.contrib import admin from django.contrib.admin import AdminSite from django.http import HttpRequest + try: from django.urls import reverse, resolve except: @@ -19,10 +21,12 @@ from suit.config import get_config register = template.Library() +if django.VERSION >= (2, 0, 0, 'final', 0): + simple_tag = register.simple_tag +else: + simple_tag = register.assignment_tag - -# @register.assignment_tag(takes_context=True) -@register.simple_tag(takes_context=True) +@simple_tag(takes_context=True) def get_menu(context, request): """ :type request: HttpRequest diff --git a/suit/templatetags/suit_tags.py b/suit/templatetags/suit_tags.py index 32255313..d0732a0f 100644 --- a/suit/templatetags/suit_tags.py +++ b/suit/templatetags/suit_tags.py @@ -1,6 +1,9 @@ import itertools + +import django from django import template from django.core.exceptions import ObjectDoesNotExist + try: from django.urls import NoReverseMatch, reverse except: @@ -20,6 +23,10 @@ from django.contrib.admin.util import lookup_field register = template.Library() +if django.VERSION >= (2, 0, 0, 'final', 0): + simple_tag = register.simple_tag +else: + simple_tag = register.assignment_tag @register.filter(name='suit_conf') @@ -84,14 +91,12 @@ def suit_bc(*args): return utils.value_by_version(args) -# @register.assignment_tag -@register.simple_tag +@simple_tag def suit_bc_value(*args): return utils.value_by_version(args) -# @register.assignment_tag -@register.simple_tag +@simple_tag def admin_extra_filters(cl): """ Return the dict of used filters which is not included in list_filters form """ @@ -100,8 +105,7 @@ def admin_extra_filters(cl): return dict((k, v) for k, v in cl.params.items() if k not in used_parameters) -# @register.assignment_tag -@register.simple_tag +@simple_tag def suit_django_version(): return django_version diff --git a/suit/tests/settings.py b/suit/tests/settings.py index 0d133ab1..1aed44fb 100644 --- a/suit/tests/settings.py +++ b/suit/tests/settings.py @@ -1,4 +1,5 @@ # Django settings for testproject project. +import django DEBUG = True TEMPLATE_DEBUG = DEBUG @@ -27,23 +28,24 @@ 'django.template.loaders.app_directories.Loader', ) -# 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', -] +if django.VERSION >= (2, 0, 0, 'final', 0): + 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', + ] +else: + 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', + ) ROOT_URLCONF = 'suit.tests.urls' TEMPLATE_DIRS = () diff --git a/suit/tests/widgets.py b/suit/tests/widgets.py index f17de7a5..b5c9271c 100644 --- a/suit/tests/widgets.py +++ b/suit/tests/widgets.py @@ -69,7 +69,7 @@ def test_SuitDateWidget_with_existing_placeholder_attr(self): self.assertEqual('p', sdw.attrs['placeholder']) def get_SuitDateWidget_output(self): - if float(django.get_version()) >= float('2.0'): + if django.VERSION >= (2, 0, 0, 'final', 0): return '
= float('2.0'): + if django.VERSION >= (2, 0, 0, 'final', 0): return '
= float('2.0'): + if django.VERSION >= (2, 0, 0, 'final', 0): dwo = self.get_SuitDateWidget_output2().replace('sdw', 'sdw_0') two = self.get_SuitTimeWidget_output2().replace('sdw', 'sdw_1') else: