Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/1.1'
Browse files Browse the repository at this point in the history
* release/1.1:
  updates CHANGES
  fixes button ordering
  @action now has 'exclude_if_adding' to hide a button in add mode
  updtes README
  • Loading branch information
saxix committed Jul 11, 2016
2 parents 1afe3aa + de081f6 commit 97871ae
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGES
@@ -1,3 +1,10 @@
Release 1.1
-----------
* @action now has 'exclude_if_adding' to hide a button in add mode
* fixes button ordering
* update templates to django admin 1.9 and grappelli
* disable @action button if object not saved

Release 1.0
-----------
* drop suppport for django<1.7
Expand Down
8 changes: 4 additions & 4 deletions README.rst
Expand Up @@ -104,8 +104,8 @@ Links
:target: https://coveralls.io/r/saxix/django-admin-extra-urls
.. |python| image:: https://pypip.in/py_versions/django-admin-extra-urls/badge.svg
:target: https://pypi.python.org/pypi/django-admin-extra-urls/
.. |python| image:: https://pypip.in/py_versions/admin-extra-urls/badge.svg
:target: https://pypi.python.org/pypi/admin-extra-urls/
:alt: Supported Python versions
.. |pypi| image:: https://pypip.in/version/admin-extra-urls/badge.svg?text=version
Expand All @@ -116,8 +116,8 @@ Links
:target: https://pypi.python.org/pypi/admin-extra-urls/
:alt: License
.. image:: https://pypip.in/wheel/django-admin-extra-urls/badge.svg
:target: https://pypi.python.org/pypi/django-admin-extra-urls/
.. image:: https://pypip.in/wheel/admin-extra-urls/badge.svg
:target: https://pypi.python.org/pypi/admin-extra-urls/
:alt: Wheel Status
.. |travis| image:: https://travis-ci.org/saxix/django-admin-extra-urls.svg?branch=develop
Expand Down
2 changes: 1 addition & 1 deletion src/admin_extra_urls/__init__.py
Expand Up @@ -3,7 +3,7 @@
import os

NAME = 'admin-extra-urls'
VERSION = __version__ = (1, 0, 0, 'final', 0)
VERSION = __version__ = (1, 1, 0, 'final', 0)
__author__ = 'sax'


Expand Down
27 changes: 17 additions & 10 deletions src/admin_extra_urls/extras.py
Expand Up @@ -5,6 +5,7 @@

import six

from django.conf import settings
from django.conf.urls import url
from django.contrib.admin.templatetags.admin_urls import admin_urlname
from django.core.urlresolvers import reverse
Expand All @@ -18,12 +19,13 @@ def labelize(label):
class ExtraUrlConfigException(RuntimeError):
pass

IS_GRAPPELLI_INSTALLED = 'grappelli' in settings.INSTALLED_APPS

opts = namedtuple('UrlOptions', 'path,label,icon,perm,order,css_class,visible')


def link(path=None, label=None, icon='', permission=None,
css_class="btn btn-success", order=999, visible=True):
css_class="btn btn-success", order=999, visible=True, **kwargs):
"""
decorator to mark ModelAdmin method as 'url' links.
Expand Down Expand Up @@ -59,7 +61,8 @@ def _inner(self, *args, **kwargs):


def action(path=None, label=None, icon='', permission=None,
css_class="btn btn-success", order=999, visible=True):
css_class="btn btn-success", order=999, visible=True,
exclude_if_adding=False, **kwargs):
"""
decorator to mark ModelAdmin method as 'url' action.
Expand Down Expand Up @@ -105,17 +108,21 @@ class ExtraUrlMixin(object):
"""
Allow to add new 'url' to the standard ModelAdmin
"""
change_list_template = 'admin_extra_urls/change_list.html'
change_form_template = 'admin_extra_urls/change_form.html'
if IS_GRAPPELLI_INSTALLED:
change_list_template = 'admin_extra_urls/grappelli/change_list.html'
change_form_template = 'admin_extra_urls/grappelli/change_form.html'
else:
change_list_template = 'admin_extra_urls/change_list.html'
change_form_template = 'admin_extra_urls/change_form.html'

def __init__(self, model, admin_site):
self.extra_buttons = []
self.extra_detail_buttons = []
super(ExtraUrlMixin, self).__init__(model, admin_site)

def get_urls(self):
self.extra_buttons = []
self.extra_detail_buttons = []
extra_buttons = []
extra_detail_buttons = []
extra_urls = {}
for c in inspect.getmro(self.__class__):
for method_name, method in six.iteritems(c.__dict__):
Expand All @@ -141,16 +148,16 @@ def wrapper(*args, **kwargs):
isdetail, method_name, options = entry
info[2] = method_name
if isdetail:
self.extra_detail_buttons.append([method_name, options])
extra_detail_buttons.append([method_name, options])
uri = r'^%s/(?P<pk>.*)/$' % options.path
else:
uri = r'^%s/$' % options.path
self.extra_buttons.append([method_name, options])
extra_buttons.append([method_name, options])

extras.append(url(uri,
wrap(getattr(self, method_name)),
name='{}_{}_{}'.format(*info)))
self.extra_buttons = sorted(self.extra_buttons, key=lambda d: d[-1])
self.extra_detail_buttons = sorted(self.extra_detail_buttons, key=lambda d: d[-1])
self.extra_buttons = sorted(extra_buttons, key=lambda d: d[-1].order)
self.extra_detail_buttons = sorted(extra_detail_buttons, key=lambda d: d[-1].order)

return extras + original
Expand Up @@ -3,11 +3,11 @@

{% block object-tools %}

<div class="object-tools">
<ul class="object-tools">
{% block object-tools-items %}
{{ block.super }}
{% include "admin_extra_urls/includes/change_form_buttons.html" %}
{% endblock %}
</div>
</ul>

{% endblock %}
@@ -0,0 +1,13 @@
{% extends "admin/change_form.html" %}
{% load extra_urls i18n admin_static admin_list admin_urls %}

{% block object-tools %}

<ul class="object-tools grp-object-tools">
{% block object-tools-items %}
{{ block.super }}
{% include "admin_extra_urls/grappelli/includes/change_form_buttons.html" %}
{% endblock %}
</ul>

{% endblock %}
@@ -0,0 +1,19 @@
{% extends "admin/change_list.html" %}
{% load extra_urls i18n admin_static admin_list admin_urls %}

{% block object-tools %}

<ul class="object-tools grp-object-tools">
{% block object-tools-items %}
{% if has_add_permission %}
<li> <a href="{% url cl.opts|admin_urlname:'add' %}{% if is_popup %}?_popup=1{% endif %}"
class="btn btn-success grp-add-link grp-state-focus">
<i class="icon-plus-sign icon-white"></i>&nbsp;
{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
</a></li>
{% endif %}
{% include "admin_extra_urls/grappelli/includes/change_list_buttons.html" %}
{% endblock %}
</ul>

{% endblock %}
@@ -0,0 +1,40 @@
{% load extra_urls i18n admin_static admin_list admin_urls %}

{% for method_name, urlattrs in adminform.model_admin.extra_detail_buttons %}
{% has_permission urlattrs.perm as authorized %}
{% if authorized and urlattrs.visible%}
{% nlless %}
<li>{% if original.pk %}
<a id="btn-{{method_name}}"
href="{% url opts|admin_urlname:method_name original.pk %}{% if is_popup %}?_popup=1{% endif %}"
class="extra-link {{urlattrs.css_class}} {{urlattrs.method_name}}">
{% if icon %}
<i class="{{ urlattrs.icon }}"></i>&nbsp;
{% endif %}
{{ urlattrs.label }}
</a>
{% else %}
{% if not urlsattrs.exclude_if_adding %}
<a id="btn-{{method_name}}" href="#"
class="extra-link disabled {{urlattrs.css_class}} {{urlattrs.method_name}}">
{% if icon %}
<i class="{{ urlattrs.icon }}"></i>&nbsp;
{% endif %}
{{ urlattrs.label }}
</a>
{%endif %}
{% endif %}
</li>
{% endnlless %}
{% endif %}
{% endfor %}

<script>
grp.jQuery('.btn').click(function (e) {
if ($(this).hasClass("disabled")) {
e.preventDefault();
} else {
$(this).removeClass('btn-success').addClass('disabled');
}
});
</script>
@@ -0,0 +1,26 @@
{% load extra_urls i18n admin_static admin_list admin_urls %}

{% for method_name,urlattrs in cl.model_admin.extra_buttons %}
{% has_permission urlattrs.perm as authorized %}
{% if authorized and urlattrs.visible %}
{% spaceless %}
<li><a id="btn-{{method_name}}" href="{% url cl.opts|admin_urlname:method_name %}{% if is_popup %}?_popup=1{% endif %}"
class="extra-link {{urlattrs.css_class}} {{method_name}}">
{% if urlattrs.icon %}
<i class="{{ urlattrs.icon }} icon-white"></i>&nbsp;
{% endif %}
{{ urlattrs.label }}
</a></li>
{% endspaceless %}
{% endif %}
{% endfor %}

<script>
grp.jQuery('.btn').click(function(e) {
if ($(this).hasClass( "disabled" )){
e.preventDefault();
}else{
$(this).removeClass('btn-success').addClass('disabled');
}
});
</script>
Expand Up @@ -3,14 +3,15 @@
{% for method_name, urlattrs in adminform.model_admin.extra_detail_buttons %}
{% has_permission urlattrs.perm as authorized %}
{% if authorized and urlattrs.visible %}
{% nlless %}
<a id="btn-{{method_name}}" href="{% url opts|admin_urlname:method_name original.pk %}{% if is_popup %}?_popup=1{% endif %}"
{% nlless %}<li>
<a id="btn-{{method_name}}"
href="{% if original.pk %}{% url opts|admin_urlname:method_name original.pk %}{% if is_popup %}?_popup=1{% endif %}{%else%}#{%endif%}"
class="extra-link {{urlattrs.css_class}} {{urlattrs.method_name}}">
{% if icon %}
<i class="{{ urlattrs.icon }}"></i>&nbsp;
{% endif %}
{{ urlattrs.label }}
</a>
</a></li>
{% endnlless %}
{% endif %}
{% endfor %}
Expand Down
Expand Up @@ -3,14 +3,14 @@
{% for method_name,urlattrs in cl.model_admin.extra_buttons %}
{% has_permission urlattrs.perm as authorized %}
{% if authorized and urlattrs.visible %}
{% spaceless %}
{% spaceless %}<li>
<a id="btn-{{method_name}}" href="{% url cl.opts|admin_urlname:method_name %}{% if is_popup %}?_popup=1{% endif %}"
class="extra-link {{urlattrs.css_class}} {{method_name}}">
class="historylink extra-link {{urlattrs.css_class}} {{method_name}}">
{% if urlattrs.icon %}
<i class="{{ urlattrs.icon }} icon-white"></i>&nbsp;
{% endif %}
{{ urlattrs.label }}
</a>
</a></li>
{% endspaceless %}
{% endif %}
{% endfor %}
Expand Down

0 comments on commit 97871ae

Please sign in to comment.