Permalink
Browse files

Stacked inlines and cleanup

  • Loading branch information...
1 parent 9324a54 commit 0ea996deaab783b5f8730ca104b596467310a991 @KristianOellegaard KristianOellegaard committed Oct 17, 2011
View
2 nani/admin.py
@@ -553,7 +553,7 @@ def queryset(self, request):
class TranslatableStackedInline(TranslatableInlineModelAdmin):
- template = 'admin/edit_inline/stacked.html'
+ template = 'admin/nani/edit_inline/stacked.html'
class TranslatableTabularInline(TranslatableInlineModelAdmin):
template = 'admin/nani/edit_inline/tabular.html'
View
2 nani/exceptions.py
@@ -1 +1,3 @@
+from django.db.models.fields import FieldDoesNotExist
+
class WrongManager(Exception): pass
View
6 nani/forms.py
@@ -21,8 +21,12 @@ def __new__(cls, name, bases, attrs):
"""
fields = []
exclude = []
+ fieldsets = []
if "Meta" in attrs:
meta = attrs["Meta"]
+ if getattr(meta, "fieldsets", False):
+ fieldsets = meta.fieldsets
+ meta.fieldsets = []
if getattr(meta, "fields", False):
fields = meta.fields
meta.fields = []
@@ -42,6 +46,8 @@ def __new__(cls, name, bases, attrs):
new_class.Meta.fields = fields
if exclude:
new_class.Meta.exclude = exclude
+ if fieldsets:
+ new_class.Meta.fieldsets = fieldsets
# End 1.3 fix
if not getattr(new_class, "Meta", None):
View
83 nani/templates/admin/nani/edit_inline/stacked.html
@@ -0,0 +1,83 @@
+{% load i18n adminmedia %}
+<div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
+ <h2>{{ inline_admin_formset.opts.verbose_name_plural|title }}</h2>
+ {% include "admin/nani/includes/translation_tabs.html" %}
+{{ 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 %}{{ inline_admin_form.original }}{% else %}#{{ forloop.counter }}{% endif %}</span>
+ {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
+ {% if inline_admin_formset.formset.can_delete and inline_admin_form.original %}<span class="delete">{{ inline_admin_form.deletion_field.field }} {{ inline_admin_form.deletion_field.label_tag }}</span>{% endif %}
+ </h3>
+ {% if inline_admin_form.form.non_field_errors %}{{ inline_admin_form.form.non_field_errors }}{% endif %}
+ {% for fieldset in inline_admin_form %}
+ {% include "admin/includes/fieldset.html" %}
+ {% endfor %}
+ {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
+ {{ inline_admin_form.fk_field.field }}
+</div>{% endfor %}
+</div>
+
+<script type="text/javascript">
+(function($) {
+ $(document).ready(function() {
+ var rows = "#{{ inline_admin_formset.formset.prefix }}-group .inline-related";
+ var updateInlineLabel = function(row) {
+ $(rows).find(".inline_label").each(function(i) {
+ var count = i + 1;
+ $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
+ });
+ }
+ var reinitDateTimeShortCuts = function() {
+ // Reinitialize the calendar and clock widgets by force, yuck.
+ if (typeof DateTimeShortcuts != "undefined") {
+ $(".datetimeshortcuts").remove();
+ DateTimeShortcuts.init();
+ }
+ }
+ var updateSelectFilter = function() {
+ // If any SelectFilter widgets were added, instantiate a new instance.
+ if (typeof SelectFilter != "undefined"){
+ $(".selectfilter").each(function(index, value){
+ var namearr = value.name.split('-');
+ SelectFilter.init(value.id, namearr[namearr.length-1], false, "{% admin_media_prefix %}");
+ });
+ $(".selectfilterstacked").each(function(index, value){
+ var namearr = value.name.split('-');
+ SelectFilter.init(value.id, namearr[namearr.length-1], true, "{% admin_media_prefix %}");
+ });
+ }
+ }
+ var initPrepopulatedFields = function(row) {
+ row.find('.prepopulated_field').each(function() {
+ var field = $(this);
+ var input = field.find('input, select, textarea');
+ var dependency_list = input.data('dependency_list') || [];
+ var dependencies = [];
+ $.each(dependency_list, function(i, field_name) {
+ dependencies.push('#' + row.find(field_name).find('input, select, textarea').attr('id'));
+ });
+ if (dependencies.length) {
+ input.prepopulate(dependencies, input.attr('maxlength'));
+ }
+ });
+ }
+ $(rows).formset({
+ prefix: "{{ inline_admin_formset.formset.prefix }}",
+ addText: "{% blocktrans with inline_admin_formset.opts.verbose_name|title as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}",
+ formCssClass: "dynamic-{{ inline_admin_formset.formset.prefix }}",
+ deleteCssClass: "inline-deletelink",
+ deleteText: "{% trans "Remove" %}",
+ emptyCssClass: "empty-form",
+ removed: updateInlineLabel,
+ added: (function(row) {
+ initPrepopulatedFields(row);
+ reinitDateTimeShortCuts();
+ updateSelectFilter();
+ updateInlineLabel(row);
+ })
+ });
+ });
+})(django.jQuery);
+</script>
View
10 nani/templates/admin/nani/edit_inline/tabular.html
@@ -6,15 +6,7 @@
<a name="{{ inline_admin_formset.opts.verbose_name|slugify }}"></a>
<h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
{{ inline_admin_formset.formset.non_form_errors }}
- <div class="nani-language-tabs">
- {% for url,name,code,status in language_tabs %}
- {% if status == 'current' %}
- <span class="current">{{ name }}{% if current_is_translated and allow_deletion %}<a class="deletelink" href="./delete-translation/{{ code }}/" title="{% trans 'Delete Translation' %}">&nbsp;</a>{% endif %}</span>
- {% else %}
- <span class="{{ status }}"><a href="{{ url }}#{{ inline_admin_formset.opts.verbose_name|slugify }}">{{ name }}</a> {% if status == 'available' and allow_deletion %}<a class="deletelink" href="./delete-translation/{{ code }}/" title="{% trans 'Delete Translation' %}">&nbsp;</a>{% endif %}</span>
- {% endif %}
- {% endfor %}
- </div>
+ {% include "admin/nani/includes/translation_tabs.html" %}
<table>
<thead><tr>
{% for field in inline_admin_formset.fields %}
View
11 nani/templates/admin/nani/includes/translation_tabs.html
@@ -0,0 +1,11 @@
+{% load i18n %}
+<div class="nani-language-tabs">
+<a name="{{ inline_admin_formset.opts.verbose_name|slugify }}"></a>
+ {% for url,name,code,status in language_tabs %}
+ {% if status == 'current' %}
+ <span class="current">{{ name }}{% if current_is_translated and allow_deletion %}<a class="deletelink" href="./delete-translation/{{ code }}/" title="{% trans 'Delete Translation' %}">&nbsp;</a>{% endif %}</span>
+ {% else %}
+ <span class="{{ status }}"><a href="{{ url }}#{{ inline_admin_formset.opts.verbose_name|slugify }}">{{ name }}</a> {% if status == 'available' and allow_deletion %}<a class="deletelink" href="./delete-translation/{{ code }}/" title="{% trans 'Delete Translation' %}">&nbsp;</a>{% endif %}</span>
+ {% endif %}
+ {% endfor %}
+</div>

0 comments on commit 0ea996d

Please sign in to comment.