diff --git a/jm2l/__init__.py b/jm2l/__init__.py index 8931d02..a55bdb8 100644 --- a/jm2l/__init__.py +++ b/jm2l/__init__.py @@ -127,6 +127,7 @@ def main(global_config, **settings): config.add_route('pict_salle', '/salle_picture/{salle_id:(\d+)}') config.add_route('list_users', '/{year:\d+}/ListParticipant') + config.add_route('list_users_csv', '/{year:\d+}/ListParticipant.csv') config.add_route('list_orga', '/{year:\d+}/ListOrga') # HTML Routes - Public diff --git a/jm2l/templates/Participant/list_users.mako b/jm2l/templates/Participant/list_users.mako index 4e1457a..be25b0c 100644 --- a/jm2l/templates/Participant/list_users.mako +++ b/jm2l/templates/Participant/list_users.mako @@ -8,6 +8,7 @@ now = datetime.datetime.now() %>
+ + \ No newline at end of file diff --git a/jm2l/views.py b/jm2l/views.py index e33ab51..aaf4c63 100644 --- a/jm2l/views.py +++ b/jm2l/views.py @@ -4,6 +4,7 @@ from pyramid.httpexceptions import HTTPBadRequest, HTTPUnauthorized from pyramid.renderers import render_to_response from pyramid.view import notfound_view_config, forbidden_view_config from pyramid.view import view_config +from pyramid.response import Response from mako.template import Template from pyramid_mailer.message import Message from .upload import IMAGEPATH @@ -22,6 +23,8 @@ from pytz import timezone from icalendar import Event as Evt from pyramid_mailer.message import Message # Then, standard libs +import csv +import cStringIO as StringIO import webhelpers.paginate as paginate import unicodedata import datetime @@ -950,6 +953,42 @@ def vote_logo(request): return HTTPFound(location=come) raise HTTPForbidden(u'Vous devez vous identifier pour obtenir une réponse.') +@view_config(route_name='list_users_csv', renderer="string") +def list_users_csv(request): + for_year = int(request.matchdict.get('year', CurrentYear)) + if request.user is None: + # Don't answer to users that aren't logged + raise HTTPForbidden(u'Vous devez vous identifier pour obtenir une réponse.') + if not request.user.Staff: + raise HTTPForbidden(u'Vous n\'avez pas l\'autorité suffisante pour effectuer cette action.') + + stmt = (DBSession.query(Sejour).filter(Sejour.for_year==for_year)).subquery() + # stmt_event = (DBSession.query(User_Event).filter() + adalias = aliased(Sejour, stmt) + Data = DBSession.query(User, adalias ) \ + .outerjoin(adalias)\ + .order_by(User.slug)\ + .all() + FileHandle = StringIO.StringIO() + fileWriter = csv.writer(FileHandle, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC) + fileWriter.writerow(["Identifiant_JM2L", "Nom", "Prenom", "Status_%s" % for_year ]) + for user, sejour in Data: + tab_identifier = [user.slug, user.nom.encode('utf-8'), user.prenom.encode('utf-8'), ''] + if user.is_IntervenantOnYear(CurrentYear): + tab_identifier[3] = "Intervenant" + elif user.Staff: + tab_identifier[3] = "Staff" + elif sejour and sejour.orga_part: + tab_identifier[3] = "Bénévole" + elif sejour: + tab_identifier[3] = "Visiteur" + else: + continue + fileWriter.writerow(tab_identifier) + + return Response(app_iter=FileHandle.getvalue(), content_type="text/csv") + + @view_config(route_name='list_users', renderer="jm2l:templates/Participant/list_users.mako") def list_users(request): for_year = int(request.matchdict.get('year', CurrentYear))