Skip to content

Commit

Permalink
#37 пагинация для шаблонов в первом приближении
Browse files Browse the repository at this point in the history
  • Loading branch information
YraganTron committed Jun 1, 2017
1 parent 846c98c commit 18c3324
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 5 deletions.
8 changes: 8 additions & 0 deletions core/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@
('vvv', 'very detail'),
('vvvv', 'all'),
)

ORDER_BY_CHOICES = (
('4', '4'),
('15', '15'),
('30', '30'),
('50', '50'),
('', 'all')
)
6 changes: 6 additions & 0 deletions core/forms/task_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from core.forms.fields import CronFormField
from core import models
from core import consts


class Search(forms.Form):
Expand All @@ -16,6 +17,11 @@ class Search(forms.Form):
widget=forms.SelectMultiple(attrs={'class': 'need-select2'}))


class PaginateBy(forms.Form):
paginate_by = forms.ChoiceField(choices=consts.ORDER_BY_CHOICES, required=False,
widget=forms.Select(attrs={'class': 'need-select2, custom-paginate'}))


class Edit(forms.ModelForm):
hosts = forms.ModelMultipleChoiceField(queryset=models.Host.objects.order_by(Lower('name')), required=False,
widget=forms.SelectMultiple(attrs={'class': 'need-select2'}))
Expand Down
11 changes: 11 additions & 0 deletions core/static/core/js/paginate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Created by YraganTron on 01.06.17.
*/

$(document).ready(function () {
$('.custom-paginate').on('change', function (evt) {
console.log(evt);
console.log(evt.target.form.id);
$('#' + evt.target.form.id).submit();
})
});
1 change: 1 addition & 0 deletions core/templates/core/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<script src="/node_modules/jquery/dist/jquery.min.js"></script>
<script src="/node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/node_modules/select2/dist/js/select2.min.js"></script>
<script src="{{ STATIC_URL }}core/js/paginate.js"></script>

{% block form_media %}
{{ form.media }}
Expand Down
12 changes: 9 additions & 3 deletions core/templates/core/base_search.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="row">
{% for field in form %}
<div class="col-sm-4">
{% bootstrap_field field layout='horizontal' %}
{% bootstrap_field field layout='horizontal' bound_css_class='' %}
</div>
{% endfor %}
</div>
Expand Down Expand Up @@ -40,7 +40,11 @@
Objects not found
</div>
{% else %}
<div><h4>Count: {{ object_list|length }}</h4></div>
<div>
<h4>Count: {{ page_obj.paginator.count }}</h4>
{% block form_paginate_by_up %}
{% endblock %}
</div>
<table class="table {% block table_class %}table-hover{% endblock table_class %}">
<thead>
{% block thead %}
Expand All @@ -55,14 +59,16 @@
{% block tbody %}
{% for object in object_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ forloop.counter0|add:page_obj.start_index }} </td>
{% block table_row %}
{% endblock table_row %}
</tr>
{% endfor %}
{% endblock tbody %}
</tbody>
</table>
{% block paginate %}
{% endblock %}
{% endif %}
</div>
{% endblock container %}
55 changes: 54 additions & 1 deletion core/templates/core/task_template/search.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% extends 'core/base_search.html' %}
{% load ansible_tag %}
{% load bootstrap3 %}

{% block left_buttons %}
<a href="{% url 'task_template_create' %}" class="btn btn-success">
Expand All @@ -7,6 +9,15 @@
</a>
{% endblock left_buttons %}

{% block form_paginate_by_up %}
<form id="paginate-form-up" method="get">
<label>
{% bootstrap_field form2.paginate_by show_label=False bound_css_class='' %}
</label>
<input type="submit" value="submit" hidden>
</form>
{% endblock %}

{% block table_heads %}
<th>Name</th>
<th>Last status</th>
Expand Down Expand Up @@ -41,4 +52,46 @@
{% endif %}
</td>
{% endwith %}
{% endblock table_row %}
{% endblock table_row %}

{% block paginate %}
<form id="paginate-form-down" method="get">
<label>
{% bootstrap_field form2.paginate_by show_label=False bound_css_class='' %}
</label>
<input type="submit" value="submit" hidden>
</form>
{% if is_paginated %}
<div class="text-center">
<ul class="pagination text-center">
{% if page_obj.has_previous %}
<li><a href="?{% url_replace request 'page' page_obj.previous_page_number %}">&laquo;</a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span>{{ i }}<span class="sr-only">(current)</span></span></li>
{% else %}
<li class="page-item">
{% if i == 1 %}
<a class="active" href="?{% url_replace request 'page' i %}">{{ i }}</a>
{% else %}
{% if i|add:"-2" <= page_obj.number and page_obj.number <= i|add:"2" %}
<a href="?{% url_replace request 'page' i %}">{{ i }}</a>
{% elif i|divisibleby:"10" or i == page_obj.paginator.num_pages %}
<a href="?{% url_replace request 'page' i %}">{{ i }}</a>
{% endif %}
{% endif %}
</li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li><a href="?{% url_replace request 'page' page_obj.next_page_number %}">&raquo;</a></li>
{% else %}
<li class="disabled"><span>&raquo;</span></li>
{% endif %}
</ul>
</div>
{% endif %}
{% endblock %}
Empty file added core/templatetags/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions core/templatetags/ansible_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django import template

register = template.Library()


@register.simple_tag
def url_replace(request, field, value):

dict_ = request.GET.copy()
dict_[field] = value

return dict_.urlencode()
18 changes: 17 additions & 1 deletion core/views/task_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@
class Search(mixins.PermissionRequiredMixin, mixins.FormMixin, views.ListView):
template_name = 'core/task_template/search.html'
form_class = core.forms.task_template.Search
paginate_by = 20
second_form_class = core.forms.task_template.PaginateBy
paginate_by = 15
title = 'Task templates'
model = models.TaskTemplate
permission_required = 'core.view_task_template'

def get_paginate_by(self, queryset):
if self.request.GET.get('paginate_by'):
paginate_by = self.request.GET.get('paginate_by')
elif self.request.GET.get('paginate_by') == '':
paginate_by = 10 ** 10
else:
paginate_by = self.paginate_by
return paginate_by

def get_breadcrumbs(self):
return (
('Home', reverse('index')),
Expand All @@ -43,6 +53,12 @@ def get_queryset(self):
if host_groups:
queryset = queryset.filter(host_groups__in=host_groups)
return queryset

def get_context_data(self, **kwargs):
context = super(Search, self).get_context_data(**kwargs)
context['form2'] = self.second_form_class(self.request.GET)
return context

search = Search.as_view()


Expand Down

0 comments on commit 18c3324

Please sign in to comment.