users listing using django_tables

This commit is contained in:
Serghei Mihai 2015-04-28 11:38:05 +02:00
parent c98c7b826e
commit 836b9f7656
8 changed files with 116 additions and 1 deletions

View File

@ -76,6 +76,7 @@ setup(
'python-ldap',
'gadjo',
'requests',
'django-tables2',
],
zip_safe=False,
cmdclass={

View File

@ -0,0 +1,16 @@
from django.utils.translation import ugettext as _
import django_tables2 as tables
from .models import LocalAccount
class AccountTable(tables.Table):
username = tables.TemplateColumn(
'<a rel="popup" href="#" %}">{{ record.username }}</a>',
verbose_name=_('Username'))
class Meta:
model = LocalAccount
attrs = {'class': 'main', 'id': 'user-table'}
fields = ('username', 'active', 'first_name', 'last_name')
empty_text = _('None')

View File

@ -5,5 +5,5 @@
{% block more-user-links %}
{{ block.super }}
<a href="{% url "manage-users" %}">{% trans 'User management' %}</a>
<a href="{% url "manage-users" organization.slug %}">{% trans 'User management' %}</a>
{% endblock %}

View File

@ -0,0 +1,15 @@
{% extends "organization/base.html" %}
{% load i18n staticfiles django_tables2 %}
{% block page-title %}
{% trans 'Users management' %}
{% endblock %}
{% block appbar %}
<h2>{% trans "Local users" %}</h2>
{% endblock %}
{% block content %}
{% render_table table "organization/users_table.html" %}
{% endblock %}

View File

@ -0,0 +1,58 @@
{% extends "django_tables2/table.html" %}
{% load django_tables2 %}
{% block table.thead %}
<thead>
<tr>
{% for column in table.columns %}
{% if column.orderable %}
<th {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a></th>
{% else %}
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
{% endif %}
{% endfor %}
{% block table.head.last.column %}
{% endblock %}
</tr>
</thead>
{% endblock table.thead %}
{% block table.tbody.row %}
<tr data-ref="{{ row.record.id }}" class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}"> {# avoid cycle for Django 1.2-1.6 compatibility #}
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>{% if column.localize == None %}{{ cell }}{% else %}{% if column.localize %}{{ cell|localize }}{% else %}{{ cell|unlocalize }}{% endif %}{% endif %}</td>
{% endfor %}
{% block table.tbody.last.column %}
{% endblock %}
</tr>
{% endblock table.tbody.row %}
{% block pagination %}
<p class="paginator">
{% if table.page.number > 1 %}
{% if table.page.previous_page_number != 1 %}
<a href="{% querystring table.prefixed_page_field=1 %}">1</a>
...
{% endif %}
{% endif %}
{% if table.page.has_previous %}
<a href="{% querystring table.prefixed_page_field=table.page.previous_page_number %}">{{ table.page.previous_page_number }}</a>
{% endif %}
<span class="this-page">{{ table.page.number }}</span>
{% if table.page.has_next %}
<a href="{% querystring table.prefixed_page_field=table.page.next_page_number %}">{{ table.page.next_page_number }}</a>
{% endif %}
{% if table.page.number != table.page.paginator.num_pages %}
{% if table.page.paginator.num_pages > 1 %}
{% if table.page.next_page_number != table.page.paginator.num_pages %}
...
<a href="{% querystring table.prefixed_page_field=table.page.paginator.num_pages %}">{{ table.page.paginator.num_pages }}</a>
{% endif %}
{% endif %}
{% endif %}
</p>
{% endblock %}

View File

@ -4,4 +4,5 @@ from .views import *
urlpatterns = patterns('',
url(r'^$', manage, name='manage'),
url(r'^users/?$', users, name='manage-users'),
)

View File

@ -1,5 +1,13 @@
from django.utils.translation import ugettext as _
from django.core.urlresolvers import reverse_lazy
from django.views.generic.base import TemplateView
from django.views.generic.list import ListView
from django_tables2 import RequestConfig
from .models import LocalAccount, Organization
from .tables import AccountTable
class OrganizationMixin(object):
@ -20,3 +28,16 @@ class ManageView(TemplateView):
template_name = 'organization/manage.html'
manage = ManageView.as_view()
class UsersPageView(OrganizationMixin, ListView):
template_name = 'organization/users.html'
model = LocalAccount
def get_context_data(self, *args, **kwargs):
context = super(UsersPageView, self).get_context_data(*args, **kwargs)
table = AccountTable(context['object_list'])
RequestConfig(self.request).configure(table)
context['table'] = table
return context
users = UsersPageView.as_view()

View File

@ -33,6 +33,7 @@ INSTALLED_APPS = (
'gadjo',
'uauth',
'uauth.organization',
'django_tables2',
)
METADATA_URIS = (
@ -53,6 +54,8 @@ MIDDLEWARE_CLASSES = (
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + ('django.core.context_processors.request',)
ROOT_URLCONF = 'uauth.urls'
WSGI_APPLICATION = 'uauth.wsgi.application'