diff --git a/djadmin2/models.py b/djadmin2/models.py index 9be9b6f..34e3b74 100644 --- a/djadmin2/models.py +++ b/djadmin2/models.py @@ -11,6 +11,8 @@ from django.conf.urls import patterns, url from django.contrib.auth import models as auth_app from django.db.models import get_models, signals +import extra_views + from djadmin2 import apiviews from djadmin2 import views @@ -248,6 +250,23 @@ class ModelAdmin2(BaseAdmin2): return self.get_api_urls(), None, None + +class Admin2Inline(extra_views.InlineFormSet): + """ + A simple extension of django-extra-view's InlineFormSet that + adds some useful functionality. + """ + + def construct_formset(self): + """ + Overrides construct_formset to attach the model class as + an attribute of the returned formset instance. + """ + formset = super(Admin2Inline, self).construct_formset() + formset.model = self.inline_model + return formset + + class ImmutableAdmin(object): """ Only __init__ allows setting of attributes diff --git a/djadmin2/templates/admin2/bootstrap/model_add_form.html b/djadmin2/templates/admin2/bootstrap/model_add_form.html deleted file mode 100644 index fe389bd..0000000 --- a/djadmin2/templates/admin2/bootstrap/model_add_form.html +++ /dev/null @@ -1,21 +0,0 @@ -{% extends "admin2/bootstrap/base.html" %} - -{% block title %}Add {{ model }}{% endblock %} - -{% block page_title %}Add {{ model }}{% endblock %} - -{% block content %} - -
- {% csrf_token %} - {{ form.as_p }} - - - {% for formset in inlines %} - {{ formset }} - {% endfor %} - - -
- -{% endblock content %} diff --git a/djadmin2/templates/admin2/bootstrap/model_edit_form.html b/djadmin2/templates/admin2/bootstrap/model_edit_form.html deleted file mode 100644 index 6bd652a..0000000 --- a/djadmin2/templates/admin2/bootstrap/model_edit_form.html +++ /dev/null @@ -1,23 +0,0 @@ -{% extends "admin2/bootstrap/base.html" %} - -{% block title %}Change {{ model }}{% endblock %} - -{% block page_title %}Change {{ model }}{% endblock %} - -{% block content %} - -
-
-
- {% csrf_token %} - {{ form.as_p }} - - {% for formset in inlines %} - {{ formset }} - {% endfor %} - -
-
-
- -{% endblock content %} diff --git a/djadmin2/templates/admin2/bootstrap/model_update_form.html b/djadmin2/templates/admin2/bootstrap/model_update_form.html index 4831caa..8609e06 100644 --- a/djadmin2/templates/admin2/bootstrap/model_update_form.html +++ b/djadmin2/templates/admin2/bootstrap/model_update_form.html @@ -1,5 +1,7 @@ {% extends "admin2/bootstrap/base.html" %} +{% load admin2_tags %} + {% block title %}{{ action }} {{ model }}{% endblock %} {% block page_title %}{{ action }} {{ model }}{% endblock %} @@ -13,8 +15,34 @@ {{ form.as_p }} {% for formset in inlines %} - {{ formset }} - {% endfor %} +

{{ formset.model|model_verbose_name_plural|capfirst }}

+ {{ formset.management_form }} + + + + {% for field in formset|formset_visible_fieldlist %} + + {% endfor %} + + + + {% for inline_form in formset %} + + {% for field in inline_form.visible_fields %} + + {% endfor %} + + {% endfor %} + +
{{ field }}
+ {% if forloop.first %} + {% for hidden_field in inline_form.hidden_fields %} + {{ hidden_field }} + {% endfor %} + {% endif %} + {{ field }} +
+ {% endfor %} diff --git a/djadmin2/templatetags/admin2_tags.py b/djadmin2/templatetags/admin2_tags.py index e950670..ab75f13 100644 --- a/djadmin2/templatetags/admin2_tags.py +++ b/djadmin2/templatetags/admin2_tags.py @@ -9,3 +9,27 @@ def admin2_urlname(view, action): Converts the view and the specified action into a valid namespaced URLConf name. """ return 'admin2:%s_%s_%s' % (view.app_label, view.model_name, action) + + +@register.filter +def model_verbose_name(obj): + """ + Returns the verbose name of a model instance or class. + """ + return obj._meta.verbose_name + + +@register.filter +def model_verbose_name_plural(obj): + """ + Returns the pluralized verbose name of a model instance or class. + """ + return obj._meta.verbose_name_plural + + +@register.filter +def formset_visible_fieldlist(formset): + """ + Returns the labels of a formset's visible fields as an array. + """ + return [f.label for f in formset.forms[0].visible_fields()] \ No newline at end of file diff --git a/djadmin2/views.py b/djadmin2/views.py index 2ed0e17..f12b572 100644 --- a/djadmin2/views.py +++ b/djadmin2/views.py @@ -135,7 +135,7 @@ class ModelEditFormView(AdminModel2Mixin, Admin2ModelFormMixin, extra_views.Upda return context -class ModelAddFormView(AdminModel2Mixin, Admin2ModelFormMixin, generic.extra_views.CreateWithInlinesView): +class ModelAddFormView(AdminModel2Mixin, Admin2ModelFormMixin, extra_views.CreateWithInlinesView): form_class = None default_template_name = "model_update_form.html" permission_type = 'add' diff --git a/example/blog/admin2.py b/example/blog/admin2.py index 2f70317..eb4ab9f 100644 --- a/example/blog/admin2.py +++ b/example/blog/admin2.py @@ -5,10 +5,8 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm from django.contrib.auth.models import Group, User from rest_framework.relations import PrimaryKeyRelatedField -import extra_views - import djadmin2 -from djadmin2.models import ModelAdmin2 +from djadmin2.models import ModelAdmin2, Admin2Inline from djadmin2.apiviews import Admin2APISerializer @@ -31,7 +29,7 @@ class UserSerializer(Admin2APISerializer): exclude = ('passwords',) -class CommentInline(extra_views.InlineFormSet): +class CommentInline(Admin2Inline): model = Comment