Tuesday, June 1, 2010

Re: Is there a way to add operations to an entity admin form?

here is how I added the action to create new items in the Addressbook
(Rubrica) to the normal change list existing admin page:

in admin.py

class RubricaAdmin(admin.ModelAdmin):
list_display = ('cognome', 'nome', 'numero_cellulare',)
list_filter = ('cognome', 'nome', 'numero_cellulare',)
search_fields = ('cognome', 'nome',)

@csrf_protect_m
def changelist_view(self, request, extra_context=None):
import re
ret = super(RubricaAdmin, self).changelist_view(request, extra_context)
ret.content = re.sub('(?<=\d )rubrìca', 'voci', ret.content)

#note it is "importa_csv/" and not "/importa_csv/"
ret.content = ret.content.replace('<a href="add/" class="addlink">',
'<a href="importa_csv/" class="addlink">Importa csv</a></li><li><a
href="add/" class="addlink">')
return ret

def importa_csv(self, request, queryset):
pass

class Media:
css = {'all': ('habble-admin.css',)}


in view.py

# -*- coding: utf-8 -*-

from django.http import HttpResponse
from django.shortcuts import render_to_response
from cruscotto.sms.models import Rubrica
from cruscotto.sms.forms import *

from django.utils.translation import ugettext_lazy as _
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
from django import template

# Create your views here.

def importa_csv(request):
opts = Rubrica._meta
app_label = opts.app_label
queryset = Rubrica.objects.all()
form = FormCSV_0()

#modifiable_objects = []
#perms_needed = set()
#i = 0
#for obj in queryset:
#if request.user.has_perm('sms.change_rubrica'):
#modifiable_objects.append(obj)
#else:
#perms_needed += 'sms.change_rubrica'

context = {
"title": _("Importa CSV"),
"object_name": force_unicode(opts.verbose_name),
#"deletable_objects": modifiable_objects,
'queryset': queryset,
#"perms_lacking": perms_needed,
'has_change_permission': request.user.has_perm('sms.change_rubrica'),
"opts": opts,
#"root_path": self.admin_site.root_path,
"app_label": app_label,
#'action_checkbox_name': admin.ACTION_CHECKBOX_NAME,
'form': form.as_table(),
'form_url': '{url_scheme}://{h_p}/chpwd/'.format(url_scheme=request.META['wsgi.url_scheme'],
h_p=request.META['HTTP_HOST']),
}

return render_to_response('importa_csv.html', context,
context_instance=template.RequestContext(request))

in urls.py before the admin row:
(r'^admin/sms/rubrica/importa_csv/', 'sms.views.importa_csv'), #
nest inside admin interface! this is important for the ../ links in
the template to work


the template:
{% extends "admin/base_site.html" %}
{% load i18n admin_modify adminmedia %}

{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="../../../jsi18n/"></script>
{{ media }}
{% endblock %}

{% block extrastyle %}{{ block.super }}<link rel="stylesheet"
type="text/css" href="{% admin_media_prefix %}css/forms.css" />{%
endblock %}

{% block coltype %}{% if ordered_objects %}colMS{% else %}colM{% endif
%}{% endblock %}

{% block bodyclass %}{{ opts.app_label }}-{{ opts.object_name.lower }}
change-form{% endblock %}

{% block breadcrumbs %}{% if not is_popup %}
<div class="breadcrumbs">
<a href="../../../">{% trans "Home" %}</a> &rsaquo;
<a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo;
{% if has_change_permission %}<a href="../">{{
opts.verbose_name_plural|capfirst }}</a>{% else %}{{
opts.verbose_name_plural|capfirst }}{% endif %}<!-- &rsaquo;
{% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{
original|truncatewords:"18" }}{% endif %}-->
</div>
{% endif %}{% endblock %}

{% block content %}
<div id="content-main">
{{ form }}
</div>
{% endblock %}


On Tue, Jun 1, 2010 at 11:07, gderazon <gderazon@gmail.com> wrote:
> I would like to add my own custom operations in addition to standard
> "save and add another", "save" etc'
> Is it supported?
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment