# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from functools import update_wrapper
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.db import transaction
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext as ugt
try:
from django.contrib.admin.utils import unquote, lookup_field, display_for_field, get_deleted_objects
from django.contrib.admin.utils import flatten_fieldsets
except ImportError:
from django.contrib.admin.util import unquote, lookup_field, display_for_field, get_deleted_objects
from django.contrib.admin.util import flatten_fieldsets
from django.http import HttpResponseRedirect
from django.views.decorators.csrf import csrf_protect
from django.utils.decorators import method_decorator
from django.contrib import messages
try:
from django.conf.urls import url, patterns
except ImportError:
from django.conf.urls import url
csrf_protect_m = method_decorator(csrf_protect)
try:
atomic = transaction.atomic
except AttributeError: # django >= 1.6
atomic = transaction.commit_on_success
## just for compatibility with older software
from jmb.jadmin.options import JumboModelAdmin as ExtendibleModelAdmin
# GET parameter for the URL to return to after change/add views. This lets the
# admin save the state of the changelist page through the change/add page.
#RETURN_GET_PARAM = '_return_to'
def set_active(modeladmin, request, queryset):
rows_updated = 0
for obj in queryset:
obj.status = 1
obj.save()
rows_updated += 1
if rows_updated == 1:
message_bit = ugt("1 object was")
else:
message_bit = ugt("%(row)s objects were") % {'row': rows_updated}
modeladmin.message_user(request, ugt("%(msg)s successfully actived.") %
{'msg': message_bit})
set_active.short_description = _("Set %(verbose_name_plural)s as active")
admin.site.add_action(set_active)
def set_disactive(modeladmin, request, queryset):
rows_updated = 0
for obj in queryset:
obj.status = 0
obj.save()
rows_updated += 1
if rows_updated == 1:
message_bit = ugt("1 object was")
else:
message_bit = ugt("%(row)s objects were") % {'row': rows_updated}
modeladmin.message_user(request, ugt("%(msg)s successfully disactived.") %
{'msg': message_bit})
set_disactive.short_description = _("Set %(verbose_name_plural)s as disactive")
admin.site.add_action(set_disactive)
def clone_action(modeladmin, request, queryset):
from jmb.core.db.utils import clone
verbose_name = ugt(modeladmin.model._meta.verbose_name)
verbose_name_plural = ugt(modeladmin.model._meta.verbose_name_plural)
cloned = 0
not_cloned = 0
for obj in queryset:
# TODO: da trasformare in funzione
cloned_obj, result = clone(obj)
# TODO: da fixare in funzione di cosa restituisce la funzione
if result != True:
not_cloned += 1
messages.error(request, _("%(name)s not cloned: %(id)d") % {
'name': verbose_name,
'id': obj.pk
}
)
continue
else:
cloned += 1
if not_cloned:
name = verbose_name
if not_cloned > 1:
name = verbose_name_plural
messages.error(request, _("%(name)s not cloned: %(number)s") %
{'name': name, 'number': not_cloned})
if cloned:
name = verbose_name
if cloned > 1:
name = verbose_name_plural
messages.info(request, _("%(name)s cloned: %(number)s") % {'name': name, 'number': cloned})
else:
name = verbose_name_plural
messages.error(request, _("%(name)s cloned: %(number)s") % {'name': name, 'number': cloned})
clone_action.short_description = _("Clone selected %(verbose_name_plural)s")
#admin.site.add_action(clone_action)
[docs]class SettingsAdmin(admin.ModelAdmin):
fieldsets = (
(_('general admin'), {'fields': (('name', 'type'), 'value',
'description')}),
)
list_display = ('name', 'value', 'type', 'description')
list_display_links = ('name', )
list_per_page = 30
[docs]class OrderedModelAdmin(admin.ModelAdmin):
def _view_name(self, name):
info = self.model._meta.app_label, self.model._meta.module_name, name
return '%s_%s_%s' % info
def _wrap(self, view):
def wrapper(*args, **kwargs):
return self.admin_site.admin_view(view)(*args, **kwargs)
return update_wrapper(wrapper, view)
def get_urls(self):
urls = super(OrderedModelAdmin, self).get_urls()
my_urls = [
url(r'^(.+)/up/$',
self._wrap(self.up),
name=self._view_name('up')),
url(r'^(.+)/down/$',
self._wrap(self.down),
name=self._view_name('down')),
]
if django.VERSION[:2] < (1, 8):
my_urls = patterns(*my_urls)
return my_urls + urls
def up(self, request, id):
node = self.model._default_manager.get(pk=id)
node.up()
try:
redirect_to = request.META['HTTP_REFERER']
except:
redirect_to = '../../'
return HttpResponseRedirect(redirect_to)
def down(self, request, id):
node = self.model._default_manager.get(pk=id)
node.down()
try:
redirect_to = request.META['HTTP_REFERER']
except:
redirect_to = '../../'
return HttpResponseRedirect(redirect_to)
def move_actions(self, node):
info = self.admin_site.name, self.model._meta.app_label, self.model._meta.module_name
data = []
if not node.is_first(): # up node
data.append('<a href="%s" class="nodes-up">%s</a>' %
(reverse('%s:%s_%s_up' % info, node.id), _('up')))
if not node.is_last() and not node.is_first():
data.append('<span style="font-weight:normal"> | </span>')
if not node.is_last(): # down node
data.append('<a href="%s" class="nodes-down">%s</a>' %
(reverse('%s:%s_%s_down' % info, node.id), _('down')))
return ''.join(data)
move_actions.short_description = _('move')
move_actions.allow_tags = True