From 05b9d18e1047f8f49566583b7b7c679d63f4b85e Mon Sep 17 00:00:00 2001 From: Asif Date: Sun, 10 Apr 2016 10:42:18 +0300 Subject: [PATCH] detail view for inline formset --- MANIFEST.in | 1 + crudbuilder/mixins.py | 7 ++++ crudbuilder/templates/inline/detail.html | 29 ++++++++++++++++ crudbuilder/templates/instance/detail.html | 2 +- crudbuilder/templatetags/tags.py | 7 ++++ crudbuilder/views.py | 6 ++-- docs/source/templates.rst | 40 ++++++++++++++++------ example/example/models.py | 2 +- 8 files changed, 79 insertions(+), 15 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 9422d85..3c466d8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,5 +1,6 @@ include LICENSE.txt include README.md recursive-include crudbuilder/templates * +recursive-include crudbuilder/static * recursive-include crudbuilder/templatetags * recursive-include docs * \ No newline at end of file diff --git a/crudbuilder/mixins.py b/crudbuilder/mixins.py index 87ecd69..197dabe 100644 --- a/crudbuilder/mixins.py +++ b/crudbuilder/mixins.py @@ -93,6 +93,13 @@ def get_actual_signal(self): return signals['instance'][view] +class BaseDetailViewMixin(CrudBuilderMixin): + def get_context_data(self, **kwargs): + context = super(BaseDetailViewMixin, self).get_context_data(**kwargs) + context['inlineformset'] = self.inlineformset + return context + + class CreateUpdateViewMixin(CrudBuilderMixin): """Common form_valid() method for both Create and Update views""" diff --git a/crudbuilder/templates/inline/detail.html b/crudbuilder/templates/inline/detail.html index a1b295a..03da12f 100644 --- a/crudbuilder/templates/inline/detail.html +++ b/crudbuilder/templates/inline/detail.html @@ -27,5 +27,34 @@

{{object}}

{% endfor %} + +
+ +

{{inlineformset.fk.related_query_name|title}}

+ + + + + {% for field in inlineformset.form.base_fields.keys %} + {% if inlineformset.fk.name != field %} + + {% endif %} + {% endfor %} + + + + {% for object in object|inline_objects:inlineformset.fk %} + + {% for field in inlineformset.form.base_fields.keys %} + {% if inlineformset.fk.name != field %} + + {% endif %} + {% endfor %} + + {% endfor %} + +
{{field}}
{{object|get_value:field}}
+ + {% endblock %} diff --git a/crudbuilder/templates/instance/detail.html b/crudbuilder/templates/instance/detail.html index 0a09159..a92ebd2 100644 --- a/crudbuilder/templates/instance/detail.html +++ b/crudbuilder/templates/instance/detail.html @@ -27,5 +27,5 @@

{{object}}

{% endfor %} -s + {% endblock %} diff --git a/crudbuilder/templatetags/tags.py b/crudbuilder/templatetags/tags.py index a25b528..7ff5bbc 100644 --- a/crudbuilder/templatetags/tags.py +++ b/crudbuilder/templatetags/tags.py @@ -52,6 +52,13 @@ def input_with_class(value, arg): return value +@register.filter(is_safe=True) +def inline_objects(object, inline_fk): + inline_model = inline_fk.model + related_filter = inline_fk.get_forward_related_filter(object) + return inline_model.objects.filter(**related_filter) + + @register.inclusion_tag('widgets/tables/pagination.html') def bootstrap_pagination(page, **kwargs): pagination_kwargs = kwargs.copy() diff --git a/crudbuilder/views.py b/crudbuilder/views.py index 37a3233..4755847 100644 --- a/crudbuilder/views.py +++ b/crudbuilder/views.py @@ -12,7 +12,8 @@ CrudBuilderMixin, BaseListViewMixin, CreateUpdateViewMixin, - InlineFormsetViewMixin + InlineFormsetViewMixin, + BaseDetailViewMixin ) from crudbuilder.abstract import BaseBuilder from crudbuilder.tables import TableBuilder @@ -134,10 +135,11 @@ def generate_detail_view(self): template_name=self.get_template('detail'), login_required=self.check_login_required, permissions=self.view_permission('detail'), + inlineformset=self.inlineformset, permission_required=self.check_permission_required, ) - detail_class = type(name, (CrudBuilderMixin, DetailView), detail_args) + detail_class = type(name, (BaseDetailViewMixin, DetailView), detail_args) self.classes[name] = detail_class return detail_class diff --git a/docs/source/templates.rst b/docs/source/templates.rst index 83f8bd2..2ee48d5 100644 --- a/docs/source/templates.rst +++ b/docs/source/templates.rst @@ -1,7 +1,7 @@ Templates ========= -By default django-crudbuilder uses its own plain html CRUD templates. You can view these templates in `template folder of crudbuilder`_ on Github. +By default django-crudbuilder uses Bootstrap3 style for its CRUD templates. You can view these templates in `template folder of crudbuilder`_ on Github. Use your own HTML templates for crudbuilder @@ -10,16 +10,34 @@ Use your own HTML templates for crudbuilder You can use your own templates for the crudbuilder in following two ways: 5 common templates for all models CRUD -######################################## - -You can create your own 5 common HTML templates for CRUD in templates/crudbuilder, then crudbuilder will use your defined templates.:: - - templates/crudbuilder - object_list.html - object_create.html - object_update.html - object_delete.html - object_detail.html +-------------------------------------- + +You can create your own 5 common HTML templates for CRUD in templates/crudbuilder, then crudbuilder will use your defined templates. + + +Model +##### + +For single object crud.:: + + templates/crudbuilder/instance + list.html + create.html + update.html + delete.html + detail.html + +Inline Formset +############## + +For inline formset.:: + + templates/crudbuilder/inline + list.html + create.html + update.html + delete.html + detail.html Custom templates for specific model: diff --git a/example/example/models.py b/example/example/models.py index 2f137c7..3848797 100644 --- a/example/example/models.py +++ b/example/example/models.py @@ -40,5 +40,5 @@ class PersonEmployment(Audit): Person, blank=True, null=True, - related_name='employments') + ) medical_allowance = models.BooleanField(default=False)