mirror of
https://github.com/nanoy42/coope
synced 2024-11-05 17:36:28 +00:00
commit
8497308c12
4 changed files with 85 additions and 3 deletions
|
@ -108,3 +108,24 @@ class SchoolForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = School
|
model = School
|
||||||
fields = "__all__"
|
fields = "__all__"
|
||||||
|
|
||||||
|
class ExportForm(forms.Form):
|
||||||
|
QUERY_TYPE_CHOICES = (
|
||||||
|
('all', 'Tous les comptes'),
|
||||||
|
('all_active', 'Tous les comptes actifs'),
|
||||||
|
('adherent', 'Tous les adhérents'),
|
||||||
|
('adherent_active', 'Tous les adhérents actifs')
|
||||||
|
)
|
||||||
|
|
||||||
|
FIELDS_CHOICES = (
|
||||||
|
('username', 'Nom d\'utilisateur'),
|
||||||
|
('last_name', 'Nom'),
|
||||||
|
('first_name', 'Prénom'),
|
||||||
|
('email', 'Adresse mail'),
|
||||||
|
('profile.school', 'École'),
|
||||||
|
('profile.balance', 'Solde'),
|
||||||
|
('profile.credit', 'Crédit'),
|
||||||
|
('profile.debit', 'Débit')
|
||||||
|
)
|
||||||
|
query_type = forms.ChoiceField(choices=QUERY_TYPE_CHOICES, label="Ensemble de la demande")
|
||||||
|
fields = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=FIELDS_CHOICES, label="Champs")
|
|
@ -7,6 +7,7 @@
|
||||||
<li><a href="#third">Admins</a></li>
|
<li><a href="#third">Admins</a></li>
|
||||||
<li><a href="#fourth">Superusers</a></li>
|
<li><a href="#fourth">Superusers</a></li>
|
||||||
<li><a href="#fifth">Écoles</a></li>
|
<li><a href="#fifth">Écoles</a></li>
|
||||||
|
<li><a href="#sixth">Exportation de données</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
@ -77,4 +78,14 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</ul>
|
</ul>
|
||||||
</section>
|
</section>
|
||||||
|
<section id="sixth" class="main">
|
||||||
|
<header class="major">
|
||||||
|
<h2>Exportation de données</h2>
|
||||||
|
</header>
|
||||||
|
<form action="{% url 'users:exportCSV' %}" method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{export_form}}
|
||||||
|
<button class="button" target="_blank">Exporter au format csv</button>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -43,4 +43,5 @@ urlpatterns = [
|
||||||
path('allReloads/<int:pk>/<int:page>', views.allReloads, name="allReloads"),
|
path('allReloads/<int:pk>/<int:page>', views.allReloads, name="allReloads"),
|
||||||
path('allConsumptions/<int:pk>/<int:page>', views.all_consumptions, name="allConsumptions"),
|
path('allConsumptions/<int:pk>/<int:page>', views.all_consumptions, name="allConsumptions"),
|
||||||
path('allMenus/<int:pk>/<int:page>', views.all_menus, name="allMenus"),
|
path('allMenus/<int:pk>/<int:page>', views.all_menus, name="allMenus"),
|
||||||
|
path('exportCSV', views.export_csv, name="exportCSV"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -7,14 +7,16 @@ from django.db.models import Q
|
||||||
from django.http import HttpResponse, HttpResponseRedirect
|
from django.http import HttpResponse, HttpResponseRedirect
|
||||||
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
|
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
|
||||||
from django.contrib.auth.decorators import login_required, permission_required
|
from django.contrib.auth.decorators import login_required, permission_required
|
||||||
|
from django.forms.models import model_to_dict
|
||||||
|
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from dal import autocomplete
|
from dal import autocomplete
|
||||||
|
import csv
|
||||||
|
|
||||||
from coopeV3.acl import admin_required, superuser_required, self_or_has_perm, active_required
|
from coopeV3.acl import admin_required, superuser_required, self_or_has_perm, active_required
|
||||||
from .models import CotisationHistory, WhiteListHistory, School
|
from .models import CotisationHistory, WhiteListHistory, School
|
||||||
from .forms import CreateUserForm, LoginForm, CreateGroupForm, EditGroupForm, SelectUserForm, GroupsEditForm, EditPasswordForm, addCotisationHistoryForm, addCotisationHistoryForm, addWhiteListHistoryForm, SelectNonAdminUserForm, SelectNonSuperUserForm, SchoolForm
|
from .forms import CreateUserForm, LoginForm, CreateGroupForm, EditGroupForm, SelectUserForm, GroupsEditForm, EditPasswordForm, addCotisationHistoryForm, addCotisationHistoryForm, addWhiteListHistoryForm, SelectNonAdminUserForm, SelectNonSuperUserForm, SchoolForm, ExportForm
|
||||||
from gestion.models import Reload, Consumption, ConsumptionHistory, MenuHistory
|
from gestion.models import Reload, Consumption, ConsumptionHistory, MenuHistory
|
||||||
|
|
||||||
@active_required
|
@active_required
|
||||||
|
@ -72,7 +74,54 @@ def index(request):
|
||||||
|
|
||||||
:template:`users/index.html`
|
:template:`users/index.html`
|
||||||
"""
|
"""
|
||||||
return render(request, "users/index.html")
|
export_form = ExportForm(request.POST or None)
|
||||||
|
return render(request, "users/index.html", {"export_form": export_form})
|
||||||
|
|
||||||
|
def export_csv(request):
|
||||||
|
export_form = ExportForm(request.POST or None)
|
||||||
|
if export_form.is_valid():
|
||||||
|
users = User.objects
|
||||||
|
qt = export_form.cleaned_data['query_type']
|
||||||
|
if qt == 'all':
|
||||||
|
users = users.all()
|
||||||
|
filename="Utilisateurs-coope"
|
||||||
|
elif qt == 'all_active':
|
||||||
|
users = users.filter(is_active=True)
|
||||||
|
filename="Utilisateurs-actifs-coope"
|
||||||
|
elif qt == 'adherent':
|
||||||
|
pks = [x.pk for x in User.objects.all() if x.profile.is_adherent]
|
||||||
|
users = users.filter(pk__in=pks)
|
||||||
|
filename="Adherents-coope"
|
||||||
|
elif qt == 'adherent_active':
|
||||||
|
pks = [x.pk for x in User.objects.filter(is_active=True) if x.profile.is_adherent]
|
||||||
|
users = users.filter(pk__in=pks)
|
||||||
|
filename="Adherents-actifs-coope"
|
||||||
|
response = HttpResponse(content_type='text/csv')
|
||||||
|
response['Content-Disposition'] = 'attachment; filename="'+ filename + '.csv"'
|
||||||
|
writer = csv.writer(response)
|
||||||
|
fields = export_form.cleaned_data['fields']
|
||||||
|
top = ["#"]
|
||||||
|
for field in fields:
|
||||||
|
top.append(dict(ExportForm.FIELDS_CHOICES)[field])
|
||||||
|
writer.writerow(top)
|
||||||
|
for user in users:
|
||||||
|
userD = model_to_dict(user)
|
||||||
|
profileD = model_to_dict(user.profile)
|
||||||
|
row = [user.pk]
|
||||||
|
for field in fields:
|
||||||
|
if "profile" in field:
|
||||||
|
if "balance" in field:
|
||||||
|
row.append(user.profile.balance)
|
||||||
|
elif "school" in field:
|
||||||
|
row.append(str(user.profile.school))
|
||||||
|
else:
|
||||||
|
row.append(profileD[field[8:]])
|
||||||
|
else:
|
||||||
|
row.append(userD[field])
|
||||||
|
writer.writerow(row)
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
return redirect(reverse('users:index'))
|
||||||
|
|
||||||
########## users ##########
|
########## users ##########
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue