Skip to content

Commit

Permalink
Fix Django version comparisons. Fixes #530
Browse files Browse the repository at this point in the history
  • Loading branch information
darklow committed Aug 15, 2016
1 parent 7d0dd51 commit c88852a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
5 changes: 2 additions & 3 deletions suit/templates/admin/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
{% endif %}

{% block content %}
{% suit_django_version as django_version %}
<div id="content-main" class="inner-two-columns">

<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post"
Expand All @@ -100,7 +99,7 @@ <h4 class="italic-title">{% trans 'tools'|capfirst %}</h4>
<ul class="box menu-box">
{% block object-tools-items %}
<li>
{% if django_version < 1.9 %}
{% if '1.9'|django_version_lt %}
<a href="history/" class="historylink">
{% else %}
{% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
Expand All @@ -116,7 +115,7 @@ <h4 class="italic-title">{% trans 'tools'|capfirst %}</h4>

{% if has_add_permission %}
<li>
{% if django_version < 1.9 %}
{% if '1.9'|django_version_lt %}
<a href="../add/">
{% else %}
<a href="{% url opts|admin_urlname:'add' %}">
Expand Down
3 changes: 1 addition & 2 deletions suit/templates/admin/edit_inline/stacked.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{% load i18n admin_static admin_urls suit_tags %}
{% suit_django_version as django_version %}
<div class="inline-group {{ inline_admin_formset.opts.suit_classes }}" id="{{ inline_admin_formset.formset.prefix }}-group">
<h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
{{ inline_admin_formset.formset.management_form }}
{{ inline_admin_formset.formset.non_form_errors }}

{% for inline_admin_form in inline_admin_formset %}<div class="inline-related{% if forloop.last %} empty-form last-related{% endif %}" id="{{ inline_admin_formset.formset.prefix }}-{% if not forloop.last %}{{ forloop.counter0 }}{% else %}empty{% endif %}">
<h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b>&nbsp;<span class="inline_label">{% if inline_admin_form.original %}
{% if django_version > 1.7 and inline_admin_form.model_admin.show_change_link and inline_admin_form.model_admin.has_registered_model %} <a href="{% url inline_admin_form.model_admin.opts|admin_urlname:'change' inline_admin_form.original.pk|admin_urlquote %}" class="inlinechangelink">{{ inline_admin_form.original }}</a>
{% if '1.8'|django_version_gte and inline_admin_form.model_admin.show_change_link and inline_admin_form.model_admin.has_registered_model %} <a href="{% url inline_admin_form.model_admin.opts|admin_urlname:'change' inline_admin_form.original.pk|admin_urlquote %}" class="inlinechangelink">{{ inline_admin_form.original }}</a>
{% else %}
{{ inline_admin_form.original }}
{% endif %}
Expand Down
5 changes: 2 additions & 3 deletions suit/templates/admin/edit_inline/tabular.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% load i18n admin_static admin_modify suit_tags admin_urls %}
{% suit_django_version as django_version %}
<div class="inline-group {{ inline_admin_formset.opts.suit_classes }}" id="{{ inline_admin_formset.formset.prefix }}-group">
<div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
{{ inline_admin_formset.formset.management_form }}
Expand All @@ -16,7 +15,7 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
{% endif %}
{% endfor %}
{% if inline_admin_formset.opts.show_change_link and inline_admin_formset.opts.has_registered_model %}<th class="text-center">{% trans "Change" %}</th>{% endif %}
{% if django_version > 1.7 and inline_admin_formset.formset.can_delete %}<th>{% trans "Delete?" %}</th>{% endif %}
{% if '1.7'|django_version_gt and inline_admin_formset.formset.can_delete %}<th>{% trans "Delete?" %}</th>{% endif %}
</tr></thead>

<tbody>
Expand Down Expand Up @@ -62,7 +61,7 @@ <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
{% endfor %}
{% endfor %}
{% endfor %}
{% if django_version > 1.7 and inline_admin_form.model_admin.show_change_link and inline_admin_form.model_admin.has_registered_model %}
{% if '1.8'|django_version_gte and inline_admin_form.model_admin.show_change_link and inline_admin_form.model_admin.has_registered_model %}
<td class="text-center">
{% if inline_admin_form.original %}
<p><a href="{% url inline_admin_form.model_admin.opts|admin_urlname:'change' inline_admin_form.original.pk|admin_urlquote %}" class="inlinechangelink">{% trans "Change" %}</a></p>
Expand Down
5 changes: 2 additions & 3 deletions suit/templates/admin/page_submit_line.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{% load i18n admin_urls suit_tags %}
{% suit_django_version as django_version %}
<div class="submit-row clearfix">
{% if show_save %}<button type="submit" class="btn btn-high btn-info" name="_save">{% trans 'Save' %}</button>{% endif %}
{% if show_save_and_continue %}<button type="submit" name="_continue" class=" btn btn-high">{% trans 'Save and continue editing' %}</button>{% endif %}
{% if show_save_as_new %}<button type="submit" name="_saveasnew" class="btn">{% trans 'Save as new' %}</button>{%endif%}
{% if show_save_and_add_another %}<button type="submit" name="_addanother" class="btn">{% trans 'Save and add another' %}</button>{% endif %}

{% if show_delete_link %}
{% if django_version < 1.9 %}
{% if '1.9'|django_version_lt %}
<a href="delete/" class="text-error deletelink">{% trans "Delete" %}</a>
{% else %}
<a href="{% url opts|admin_urlname:'delete' original.pk|admin_urlquote %}" class="text-error deletelink">{% trans "Delete" %}</a>
Expand All @@ -17,7 +16,7 @@
{% if show_delete_link %}
<br>
{% endif %}
{% if django_version < 1.9 %}
{% if '1.9'|django_version_lt %}
<a href="delete-translation/?language={{ language }}" class="text-error deletelink delete-translation">{% blocktrans with language_name|lower as language %}Delete {{ language }} translation{% endblocktrans %}</a>
{% else %}
<a href="{% url opts|admin_urlname:'delete' original.pk|admin_urlquote %}-translation/?language={{ language }}" class="text-error deletelink delete-translation">{% blocktrans with language_name|lower as language %}Delete {{ language }} translation{% endblocktrans %}</a>
Expand Down
3 changes: 1 addition & 2 deletions suit/templates/admin/submit_line.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{% load i18n admin_urls suit_tags %}
{% suit_django_version as django_version %}
<div class="submit-row clearfix">
{% if show_save %}<button type="submit" class="btn btn-high btn-info" name="_save">{% trans 'Save' %}</button>{% endif %}
{% if show_save_and_continue %}<button type="submit" name="_continue" class=" btn btn-high">{% trans 'Save and continue editing' %}</button>{% endif %}
{% if show_save_as_new %}<button type="submit" name="_saveasnew" class="btn">{% trans 'Save as new' %}</button>{%endif%}
{% if show_save_and_add_another %}<button type="submit" name="_addanother" class="btn">{% trans 'Save and add another' %}</button>{% endif %}

{% if show_delete_link %}
{% if django_version < 1.9 %}
{% if '1.9'|django_version_lt %}
<a href="delete/" class="text-error deletelink">{% trans "Delete" %}</a>
{% else %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
Expand Down
30 changes: 28 additions & 2 deletions suit/templatetags/suit_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def field_contents_foreign_linked(admin_field):

if not hasattr(admin_field.model_admin,
'linked_readonly_fields') or fieldname not in admin_field \
.model_admin \
.linked_readonly_fields:
.model_admin \
.linked_readonly_fields:
return displayed

try:
Expand Down Expand Up @@ -100,6 +100,30 @@ def suit_django_version():
return django_version


@register.filter
def django_version_lt(string):
return django_version < str_to_version(string)


@register.filter
def django_version_lte(string):
return django_version <= str_to_version(string)


@register.filter
def django_version_gt(string):
return django_version > str_to_version(string)


@register.filter
def django_version_gte(string):
return django_version >= str_to_version(string)


def str_to_version(string):
return tuple([int(s) for s in string.split('.')])


if django_version < (1, 9):
# Add empty tags to avoid Django template errors if < Django 1.9
@register.simple_tag
Expand All @@ -109,6 +133,8 @@ def add_preserved_filters(*args, **kwargs):
if django_version < (1, 5):
# Add admin_urlquote filter to support Django 1.4
from django.contrib.admin.util import quote


@register.filter
def admin_urlquote(value):
return quote(value)
4 changes: 4 additions & 0 deletions suit/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from suit import utils
from django.test import TestCase

from suit.templatetags.suit_tags import str_to_version


class UtilsTestCase(TestCase):
def get_args(self):
Expand All @@ -16,3 +18,5 @@ def test_value_by_version(self):
args = [utils.django_major_version(), 'a']
self.assertEqual(utils.value_by_version(args), 'a')

def test_str_to_version(self):
return self.assertEqual(str_to_version('1.10.2'), (1, 10, 2))

0 comments on commit c88852a

Please sign in to comment.