mirror of https://gitlab.crans.org/bde/nk20
Dynamic tabs on conso view
This commit is contained in:
parent
12d7ddfa30
commit
3597e79dc1
|
@ -12,7 +12,6 @@ urlpatterns = [
|
||||||
path('buttons/create/', views.TransactionTemplateCreateView.as_view(), name='template_create'),
|
path('buttons/create/', views.TransactionTemplateCreateView.as_view(), name='template_create'),
|
||||||
path('buttons/update/<int:pk>/', views.TransactionTemplateUpdateView.as_view(), name='template_update'),
|
path('buttons/update/<int:pk>/', views.TransactionTemplateUpdateView.as_view(), name='template_update'),
|
||||||
path('buttons/', views.TransactionTemplateListView.as_view(), name='template_list'),
|
path('buttons/', views.TransactionTemplateListView.as_view(), name='template_list'),
|
||||||
path('consos/<str:template_type>/', views.ConsoView.as_view(), name='consos'),
|
|
||||||
path('consos/', views.ConsoView.as_view(), name='consos'),
|
path('consos/', views.ConsoView.as_view(), name='consos'),
|
||||||
|
|
||||||
# API for the note autocompleter
|
# API for the note autocompleter
|
||||||
|
|
|
@ -8,7 +8,7 @@ from django.urls import reverse
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.views.generic import CreateView, ListView, UpdateView
|
from django.views.generic import CreateView, ListView, UpdateView
|
||||||
|
|
||||||
from .models import Transaction, TransactionCategory, TransactionTemplate, Alias
|
from .models import Transaction, TransactionTemplate, Alias
|
||||||
from .forms import TransactionForm, TransactionTemplateForm, ConsoForm
|
from .forms import TransactionForm, TransactionTemplateForm, ConsoForm
|
||||||
|
|
||||||
|
|
||||||
|
@ -135,19 +135,13 @@ class ConsoView(LoginRequiredMixin, CreateView):
|
||||||
Add some context variables in template such as page title
|
Add some context variables in template such as page title
|
||||||
"""
|
"""
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context['template_types'] = TransactionCategory.objects.all()
|
context['transaction_templates'] = TransactionTemplate.objects.all() \
|
||||||
|
.order_by('template_type')
|
||||||
if 'template_type' not in self.kwargs.keys():
|
context['title'] = _("Consommations")
|
||||||
return context
|
|
||||||
|
|
||||||
template_type = TransactionCategory.objects.filter(
|
|
||||||
name=self.kwargs.get('template_type')).get()
|
|
||||||
context['buttons'] = TransactionTemplate.objects.filter(
|
|
||||||
template_type=template_type)
|
|
||||||
context['title'] = template_type
|
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return reverse('note:consos',
|
"""
|
||||||
args=(self.kwargs.get('template_type'), ))
|
When clicking a button, reload the same page
|
||||||
|
"""
|
||||||
|
return reverse('note:consos')
|
||||||
|
|
|
@ -2,40 +2,92 @@
|
||||||
|
|
||||||
{% load i18n static pretty_money %}
|
{% load i18n static pretty_money %}
|
||||||
|
|
||||||
|
{# Remove page title #}
|
||||||
|
{% block contenttitle %}{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<fieldset class="module aligned">
|
{# Regroup buttons under categories #}
|
||||||
{% for type in template_types %}
|
{% regroup transaction_templates by template_type as template_types %}
|
||||||
<a href="{% url 'note:consos' template_type=type %}"><button>{{ type }}</button></a>
|
|
||||||
{% endfor %}
|
<form method="post" onsubmit="window.onbeforeunload=null">
|
||||||
</fieldset>
|
{% csrf_token %}
|
||||||
<form method="post" onsubmit="window.onbeforeunload=null">{% csrf_token %}
|
|
||||||
{% if form.non_field_errors %}
|
<div class="row">
|
||||||
<p class="errornote">
|
<div class="col-sm-5">
|
||||||
{% for error in form.non_field_errors %}
|
{% if form.non_field_errors %}
|
||||||
{{ error }}
|
<p class="errornote">
|
||||||
|
{% for error in form.non_field_errors %}
|
||||||
|
{{ error }}
|
||||||
|
{% endfor %}
|
||||||
|
</p>
|
||||||
|
{% endif %}
|
||||||
|
{% for field in form %}
|
||||||
|
<div class="form-row{% if field.errors %} errors{% endif %}">
|
||||||
|
{{ field.errors }}
|
||||||
|
<div>
|
||||||
|
{{ field.label_tag }}
|
||||||
|
{% if field.is_readonly %}
|
||||||
|
<div class="readonly">{{ field.contents }}</div>
|
||||||
|
{% else %}
|
||||||
|
{{ field }}
|
||||||
|
{% endif %}
|
||||||
|
{% if field.field.help_text %}
|
||||||
|
<div class="help">{{ field.field.help_text|safe }}</div>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</p>
|
</div>
|
||||||
{% endif %}
|
|
||||||
<fieldset class="module aligned">
|
<div class="col-sm-7">
|
||||||
{% for field in form %}
|
<div class="card text-center">
|
||||||
<div class="form-row{% if field.errors %} errors{% endif %}">
|
{# Tabs for button categories #}
|
||||||
{{ field.errors }}
|
<div class="card-header">
|
||||||
<div>
|
<ul class="nav nav-tabs nav-fill card-header-tabs">
|
||||||
{{ field.label_tag }}
|
{% for template_type in template_types %}
|
||||||
{% if field.is_readonly %}
|
<li class="nav-item">
|
||||||
<div class="readonly">{{ field.contents }}</div>
|
<a class="nav-link" data-toggle="tab" href="#{{ template_type.grouper|slugify }}">
|
||||||
{% else %}
|
{{ template_type.grouper }}
|
||||||
{{ field }}
|
</a>
|
||||||
{% endif %}
|
</li>
|
||||||
{% if field.field.help_text %}
|
{% endfor %}
|
||||||
<div class="help">{{ field.field.help_text|safe }}</div>
|
</ul>
|
||||||
{% endif %}
|
</div>
|
||||||
|
|
||||||
|
{# Tabs content #}
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="tab-content">
|
||||||
|
{% for template_type in template_types %}
|
||||||
|
<div class="tab-pane" id="{{ template_type.grouper|slugify }}">
|
||||||
|
{% for button in template_type.list %}
|
||||||
|
<button class="btn btn-outline-dark" name="button" value="{{ button.name }}">
|
||||||
|
{{ button.name }} ({{ button.amount | pretty_money }})
|
||||||
|
</button>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
</div>
|
||||||
{% for button in buttons %}
|
</div>
|
||||||
<button name="button" value="{{ button.name }}">{{ button.name }} ({{ button.amount | pretty_money }})</button>
|
|
||||||
{% endfor %}
|
|
||||||
</fieldset>
|
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
$ = django.jQuery;
|
||||||
|
$(document).ready(function() {
|
||||||
|
// If hash of a category in the URL, then select this category
|
||||||
|
// else select the first one
|
||||||
|
if (location.hash) {
|
||||||
|
$("a[href='" + location.hash + "']").tab("show");
|
||||||
|
} else {
|
||||||
|
$("a[data-toggle='tab']").first().tab("show");
|
||||||
|
}
|
||||||
|
|
||||||
|
// When selecting a category, change URL
|
||||||
|
$(document.body).on("click", "a[data-toggle='tab']", function(event) {
|
||||||
|
location.hash = this.getAttribute("href");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in New Issue