Skip to content
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

Fix empty Welcome username for custom user model #644

Open
wants to merge 5 commits into
base: develop
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
2 changes: 1 addition & 1 deletion suit/templates/admin/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
{% trans 'Welcome,' %}
<strong>
{% filter force_escape %}
{% firstof user.first_name user.username %}{% endfilter %}</strong>.
{% firstof user.first_name user.get_username %}{% endfilter %}</strong>.
{% endblock %}
<span class="user-links">
{% block userlinks %}
Expand Down
14 changes: 11 additions & 3 deletions suit/templatetags/suit_menu.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import django
from django import template
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
Expand All @@ -16,9 +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)
@simple_tag(takes_context=True)
def get_menu(context, request):
"""
:type request: HttpRequest
Expand Down
18 changes: 14 additions & 4 deletions suit/templatetags/suit_tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import itertools

import django
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
Expand All @@ -17,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')
Expand Down Expand Up @@ -81,12 +91,12 @@ def suit_bc(*args):
return utils.value_by_version(args)


@register.assignment_tag
@simple_tag
def suit_bc_value(*args):
return utils.value_by_version(args)


@register.assignment_tag
@simple_tag
def admin_extra_filters(cl):
""" Return the dict of used filters which is not included
in list_filters form """
Expand All @@ -95,7 +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
@simple_tag
def suit_django_version():
return django_version

Expand Down
5 changes: 4 additions & 1 deletion suit/tests/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 19 additions & 7 deletions suit/tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Django settings for testproject project.
import django

DEBUG = True
TEMPLATE_DEBUG = DEBUG
Expand Down Expand Up @@ -27,13 +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',
)
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 = ()
Expand Down
5 changes: 4 additions & 1 deletion suit/tests/templates/form_tabs.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion suit/tests/templatetags/suit_list.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion suit/tests/templatetags/suit_menu.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion suit/tests/templatetags/suit_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion suit/tests/urls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
)
except ImportError: # Django 1.10+
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', admin.site.urls),
]
48 changes: 37 additions & 11 deletions suit/tests/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -69,10 +69,16 @@ def test_SuitDateWidget_with_existing_placeholder_attr(self):
self.assertEqual('p', sdw.attrs['placeholder'])

def get_SuitDateWidget_output(self):
return '<div class="input-append suit-date"><input class="vDateField ' \
'input-small " name="sdw" placeholder="Date" ' \
'size="10" type="text" /><span class="add-on"><i ' \
'class="icon-calendar"></i></span></div>'
if django.VERSION >= (2, 0, 0, 'final', 0):
return '<div class="input-append suit-date"><input class="vDateField ' \
'input-small " name="sdw" placeholder="Date" ' \
'size="10" type="text" value="" /><span class="add-on"><i ' \
'class="icon-calendar"></i></span></div>'
else:
return '<div class="input-append suit-date"><input class="vDateField ' \
'input-small " name="sdw" placeholder="Date" ' \
'size="10" type="text" /><span class="add-on"><i ' \
'class="icon-calendar"></i></span></div>'

def test_SuitDateWidget_output(self):
sdw = SuitDateWidget(attrs={'placeholder': 'Date'})
Expand All @@ -97,10 +103,16 @@ def test_SuitTimeWidget_with_existing_placeholder_attr(self):
self.assertEqual('p', sdw.attrs['placeholder'])

def get_SuitTimeWidget_output(self):
return '<div class="input-append suit-date suit-time"><input ' \
'class="vTimeField input-small " name="sdw" ' \
'placeholder="Time" size="8" type="text" /><span ' \
'class="add-on"><i class="icon-time"></i></span></div>'
if django.VERSION >= (2, 0, 0, 'final', 0):
return '<div class="input-append suit-date suit-time"><input ' \
'class="vTimeField input-small " name="sdw" ' \
'placeholder="Time" size="8" type="text" value="" /><span ' \
'class="add-on"><i class="icon-time"></i></span></div>'
else:
return '<div class="input-append suit-date suit-time"><input ' \
'class="vTimeField input-small " name="sdw" ' \
'placeholder="Time" size="8" type="text" /><span ' \
'class="add-on"><i class="icon-time"></i></span></div>'

def test_SuitTimeWidget_output(self):
sdw = SuitTimeWidget(attrs={'placeholder': 'Time'})
Expand All @@ -109,11 +121,25 @@ def test_SuitTimeWidget_output(self):
self.get_SuitTimeWidget_output(),
output)

def get_SuitDateWidget_output2(self):
return '<input class="vDateField ' \
'input-small " name="sdw" placeholder="Date" ' \
'size="10" type="text" />'

def get_SuitTimeWidget_output2(self):
return '<input ' \
'class="vTimeField input-small " name="sdw" ' \
'placeholder="Time" size="8" type="text" />'

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')
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:
dwo = self.get_SuitDateWidget_output().replace('sdw', 'sdw_0')
two = self.get_SuitTimeWidget_output().replace('sdw', 'sdw_1')
self.assertHTMLEqual(output, '<div class="datetime">%s %s</div>' %
(dwo, two))

Expand Down
12 changes: 9 additions & 3 deletions suit/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div class="datetime">%s %s</div>'
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 = '<div class="datetime">%s</div>'
return mark_safe(out_tpl % html)

# def format_output(self, rendered_widgets):
# out_tpl = '<div class="datetime">%s %s</div>'
# return mark_safe(out_tpl % (rendered_widgets[0], rendered_widgets[1]))


def _make_attrs(attrs, defaults=None, classes=None):
Expand Down