diff --git a/users/models.py b/users/models.py index 823d1f7f..bf725ea2 100755 --- a/users/models.py +++ b/users/models.py @@ -49,6 +49,7 @@ import re import uuid import datetime import sys +import csv from django.db import models from django.db.models import Q @@ -59,6 +60,7 @@ from django.dispatch import receiver from django.utils.functional import cached_property from django.template import Context, loader from django.core.mail import send_mail +from django.http import HttpResponse from django.core.urlresolvers import reverse from django.db import transaction from django.utils import timezone @@ -349,6 +351,41 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, self.unarchive() self.save() + def create_data_file(self): + """Génère le fichier contenant toutes les infos que l'on a sur l'user: + - Infos perso + - Machines + - Cotisations + - Paramètres mail + - Historique sur les objects. + renvoit le chemin du fichier pour lancer le téléchargement. + """ + interfaces = self.users_interfaces([self]) + + response = HttpResponse(content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{}_data.csv"'.format(self.get_full_name()) + + writer = csv.writer(response) + writer.writerow(["Ce fichier contient toutes les données que Re2o possède pour l\'utilisateur {}".format(self.get_full_name())]) + writer.writerow(["Nom de la variable","Valeur","Commentaire"]) + writer.writerow(["Informations personnelles"]) + writer.writerow(["Prénom",str(self.name)]) + writer.writerow(["Nom",str(self.surname)]) + writer.writerow(["Téléphone",str(self.telephone)]) + writer.writerow(["Commentaire",str(self.comment)]) + writer.writerow(["École",str(self.school)]) + writer.writerow(["Chambre",str(self.room)]) + writer.writerow(["Date d'inscription",str(self.registered)]) + writer.writerow(["Dernière connexion",str(self.last_login)]) + writer.writerow(["Solde",str(self.solde)]) + + return response + + + + + + @property def is_staff(self): """ Fonction de base django, renvoie si l'user est admin""" diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index 52a214a6..b42bd09e 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -150,6 +150,9 @@ with this program; if not, write to the Free Software Foundation, Inc., {% acl_end %} {% history_button users text=True %} + + CSV +
diff --git a/users/urls.py b/users/urls.py index 1e6ffa8c..ca7914f0 100644 --- a/users/urls.py +++ b/users/urls.py @@ -34,6 +34,7 @@ urlpatterns = [ url(r'^new_user/$', views.new_user, name='new-user'), url(r'^new_club/$', views.new_club, name='new-club'), url(r'^edit_info/(?P[0-9]+)$', views.edit_info, name='edit-info'), + url(r'^download_data/(?P[0-9]+)$', views.download_data, name='download-data'), url(r'^edit_club_admin_members/(?P[0-9]+)$', views.edit_club_admin_members, name='edit-club-admin-members'), diff --git a/users/views.py b/users/views.py index ad8543b5..dcc2d9e1 100644 --- a/users/views.py +++ b/users/views.py @@ -225,6 +225,12 @@ def edit_info(request, user, userid): request ) +@login_required +@can_edit(User) +def download_data(request, user, userid): + """ Call the create_data_file method from user and push the file to download """ + return user.create_data_file() + @login_required @can_edit(User, 'state')