[member] add user list and filter

This commit is contained in:
Pierre-antoine Comby 2019-09-23 12:50:14 +02:00
parent 55d984435d
commit 07d1cd225b
6 changed files with 105 additions and 2 deletions

31
apps/member/filters.py Normal file
View File

@ -0,0 +1,31 @@
# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2018-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django_filters import FilterSet, CharFilter,NumberFilter
from django.contrib.auth.models import User
from django.db.models import CharField
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit
from .models import Club
class UserFilter(FilterSet):
class Meta:
model = User
fields = ['last_name','first_name','username','profile__section']
filter_overrides={
CharField:{
'filter_class':CharFilter,
'extra': lambda f:{
'lookup_expr':'icontains'
}
}
}
class UserFilterFormHelper(FormHelper):
form_method = 'GET'
layout = Layout(
'last_name','first_name','username','profile__section',
Submit('Submit','Apply Filter'),
)

View File

@ -2,12 +2,26 @@
import django_tables2 as tables import django_tables2 as tables
from .models import Club from .models import Club
from django.conf import settings
from django.contrib.auth.models import User
class ClubTable(tables.Table): class ClubTable(tables.Table):
class Meta: class Meta:
attrs = {'class':'table table-bordered table-condensed table-striped table-hover'} attrs = {'class':'table table-bordered table-condensed table-striped table-hover'}
model = Club model = Club
template_name = 'django_tables2/bootstrap.html' template_name = 'django_tables2/bootstrap.html'
fields= ('id','name','email') fields = ('id','name','email')
row_attrs = {'class':'table-row', row_attrs = {'class':'table-row',
'data-href': lambda record: record.pk } 'data-href': lambda record: record.pk }
class UserTable(tables.Table):
section = tables.Column(accessor='profile.section')
solde = tables.Column(accessor='note.balance')
class Meta:
attrs = {'class':'table table-bordered table-condensed table-striped table-hover'}
template_name = 'django_tables2/bootstrap.html'
fields = ('last_name','first_name','username','email')
model = User

View File

@ -15,5 +15,6 @@ urlpatterns = [
path('club/<int:pk>/',views.ClubDetailView.as_view(),name="club_detail"), path('club/<int:pk>/',views.ClubDetailView.as_view(),name="club_detail"),
path('club/<int:pk>/add_member/',views.ClubAddMemberView.as_view(),name="club_add_member"), path('club/<int:pk>/add_member/',views.ClubAddMemberView.as_view(),name="club_add_member"),
path('club/create/',views.ClubCreateView.as_view(),name="club_create"), path('club/create/',views.ClubCreateView.as_view(),name="club_create"),
path('user/',views.UserListView.as_view(),name="user_list"),
path('user/<int:pk>',views.UserDetailView.as_view(),name="user_detail") path('user/<int:pk>',views.UserDetailView.as_view(),name="user_detail")
] ]

View File

@ -8,6 +8,7 @@ from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, ListView, DetailView from django.views.generic import CreateView, ListView, DetailView
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.db.models import Q from django.db.models import Q
@ -16,9 +17,13 @@ from django_tables2.views import SingleTableView
from .models import Profile, Club, Membership from .models import Profile, Club, Membership
from .forms import ProfileForm, ClubForm,MembershipForm, MemberFormSet,FormSetHelper from .forms import ProfileForm, ClubForm,MembershipForm, MemberFormSet,FormSetHelper
from .tables import ClubTable from .tables import ClubTable,UserTable
from .filters import UserFilter, UserFilterFormHelper
from note.models.transactions import Transaction from note.models.transactions import Transaction
from note.tables import HistoryTable from note.tables import HistoryTable
class UserCreateView(CreateView): class UserCreateView(CreateView):
""" """
Une vue pour inscrire un utilisateur et lui créer un profile Une vue pour inscrire un utilisateur et lui créer un profile
@ -60,6 +65,28 @@ class UserDetailView(LoginRequiredMixin,DetailView):
context['club_list'] = ClubTable(club_list) context['club_list'] = ClubTable(club_list)
return context return context
class UserListView(LoginRequiredMixin,SingleTableView):
model = User
table_class = UserTable
template_name = 'member/user_list.html'
filter_class = UserFilter
formhelper_class = UserFilterFormHelper
def get_queryset(self,**kwargs):
qs = super().get_queryset()
self.filter = self.filter_class(self.request.GET,queryset=qs)
self.filter.form.helper = self.formhelper_class()
return self.filter.qs
def get_context_data(self,**kwargs):
context = super().get_context_data(**kwargs)
context["filter"] = self.filter
return context
###################################
############## CLUB ###############
###################################
class ClubCreateView(LoginRequiredMixin,CreateView): class ClubCreateView(LoginRequiredMixin,CreateView):
""" """

View File

@ -5,6 +5,7 @@ Django==2.2.3
django-allauth==0.39.1 django-allauth==0.39.1
django-crispy-forms==1.7.2 django-crispy-forms==1.7.2
django-extensions==2.1.9 django-extensions==2.1.9
django-filter==2.2.0
django-polymorphic==2.0.3 django-polymorphic==2.0.3
django-reversion==3.0.3 django-reversion==3.0.3
django-tables2==2.1.0 django-tables2==2.1.0

View File

@ -0,0 +1,29 @@
{% extends "base.html" %}
{% load render_table from django_tables2 %}
{% load crispy_forms_tags%}
{% block content %}
<a class="btn btn-primary" href="{% url 'member:signup' %}">New User</a>
<div class="row">
{% crispy filter.form filter.form.helper %}
</div>
<div class="row">
<div id="replaceable-content" class="col-6">
{% render_table table %}
</div>
</div>
{% endblock %}
{% block extrajavascript %}
<script type="text/javascript">
$(document).ready(function($) {
$(".table-row").click(function() {
window.document.location = $(this).data("href");
});
});
</script>
{% endblock %}