Skip to content

Commit

Permalink
forms.UnchangableModelMixin is used in DisplayModelMetaclass to set f…
Browse files Browse the repository at this point in the history
…orm.has_changed() result to False. bs_breadcrumbs() Jinja2 macro offers optional title kwarg. bs_inline_formsets() Jinja2 macro now offers caller() sections for begin / end of each formset / form in formset. models.get_verbose_name() now supports foreign key fields specified as strings like 'action__type'. common.css support to highlight current hash as id of element. Better naming for conditional display css classes. Support for bs_breadcrumbs() title kwarg in views.ListSortingView.
  • Loading branch information
Dmitri-Sintsov committed Nov 16, 2015
1 parent e6e9e33 commit 60d0f65
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
8 changes: 8 additions & 0 deletions django_jinja_knockout/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ def display_model_formfield_callback(db_field, **kwargs):
return db_field.formfield(**defaults)


class UnchangableModelMixin():

def has_changed(self):
# Display forms never change.
return False


# Metaclass used to create read-only forms (display models). #
class DisplayModelMetaclass(ModelFormMetaclass):
def __new__(mcs, name, bases, attrs):
if attrs is None:
attrs = {}
bases = bases + (UnchangableModelMixin,)
attrs.update({'formfield_callback': display_model_formfield_callback})
return ModelFormMetaclass.__new__(mcs, name, bases, attrs)

Expand Down
5 changes: 4 additions & 1 deletion django_jinja_knockout/jinja2/bs_breadcrumbs.htm
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{% macro bs_breadcrumbs(navs) -%}
{% macro bs_breadcrumbs(title=None, navs) -%}
<ol class="breadcrumb">
{% if title is not none %}
<li class="bold">{{ title }}:</li>
{% endif %}
{% for nav in navs %}
<li class="{{ nav.class }}">
{% if nav.class != 'active' %}<a href="{{ nav.url }}">{% endif %}
Expand Down
5 changes: 4 additions & 1 deletion django_jinja_knockout/jinja2/bs_inline_formsets.htm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</div>
{% endif %}
{% for formset in formsets %}
{{ caller(formset_begin=formset) }}
<div class="formset default-padding">
<div class="management-form">
{{ formset.management_form }}
Expand All @@ -38,6 +39,7 @@
<table class="table">
{% endif %}
{% for form in formset %}
{{ caller(form_begin=form) }}
{% set formset_index = loop.index0 %}
{% if formset.can_delete %}
{% if formset_index < formset.min_num %}
Expand All @@ -47,6 +49,7 @@
{% endif %}
{% endif %}
{% include 'bs_formset_form.htm' %}
{{ caller(form_end=form) }}
{% endfor %}
{% if action == '' %}
</table>
Expand All @@ -58,7 +61,7 @@
<button class="btn btn-info btn-sm" data-bind="click: addForm, visible: hasMoreForms">{{ _('Add "%(verbose_name)s"', verbose_name=formset.model._meta.verbose_name) }}</button>
{% endif %}
</div>
{{ caller(formset.prefix) }}
{{ caller(formset_end=formset) }}
{% endfor %}
{% if html.submit_text != '' %}
<button type="submit" class="btn btn-primary">{{ html.submit_text }}</button>
Expand Down
11 changes: 7 additions & 4 deletions django_jinja_knockout/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

def get_verbose_name(obj, fieldname):
if type(obj) is str:
Model = apps.get_model(*obj.split('.'))
return Model._meta.get_field_by_name(fieldname)[0].verbose_name
else:
return obj._meta.get_field_by_name(fieldname)[0].verbose_name
obj = apps.get_model(*obj.split('.'))
fieldpath = fieldname.split('__')
if len(fieldpath) > 1:
fieldname = fieldpath.pop()
for _fieldname in fieldpath:
obj = obj._meta.get_field(_fieldname).rel.to
return obj._meta.get_field_by_name(fieldname)[0].verbose_name


class ContentTypeLinker(object):
Expand Down
9 changes: 7 additions & 2 deletions django_jinja_knockout/static/css/front/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
font-weight: 800;
}

.href-target:target {
border: 2px solid Aquamarine;
background-color: Aquamarine;
}

.vscroll,
.bootstrap-dialog-message {
max-height: 500px;
Expand Down Expand Up @@ -122,12 +127,12 @@ textarea.ko-template {
visibility: hidden;
}

.display-block .conditional-display {
.display-block-condition .conditional-display {
display: block;
margin: 0.25em 0 0.25em 0;
}

.display-inline .conditional-display {
.display-inline-condition .conditional-display {
display: inline-block;
margin: 0 0.25em 0 0.25em;
}
Expand Down
2 changes: 2 additions & 0 deletions django_jinja_knockout/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,12 @@ def get_context_data(self, **kwargs):
context_data = super().get_context_data(**kwargs)
context_data.update({
'cbv': self,
'filter_title': {},
'filter_navs': {},
'filter_display': {}
})
for fieldname in self.__class__.allowed_filter_fields:
context_data['filter_title'][fieldname] = get_verbose_name(self.__class__.model, fieldname)
navs, display = self.get_filter_navs(fieldname)
context_data['filter_navs'][fieldname] = navs
context_data['filter_display'][fieldname] = display
Expand Down

0 comments on commit 60d0f65

Please sign in to comment.