From 8c2f6349018709d10101476f2f51660ff2536a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Wed, 7 Mar 2018 16:04:43 +0000 Subject: [PATCH] REST : Add API for mailing for club and other Access /users/rest/ml/std/ for list of standard mailings Access /users/rest/ml/std/member// for list of members of specific standard mailing Access /users/rest/ml/club/ for list of club mailings Access /users/rest/ml/club/member// for list of members of specific club mailing Access /users/rest/ml/club/admin// for list of admins of specific club mailing --- users/serializers.py | 13 +++++++-- users/urls.py | 26 ++++++++++++++++- users/views.py | 67 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 96 insertions(+), 10 deletions(-) diff --git a/users/serializers.py b/users/serializers.py index 8f4c050d..95388d41 100644 --- a/users/serializers.py +++ b/users/serializers.py @@ -23,9 +23,16 @@ #Maƫl Kervella from rest_framework import serializers -from users.models import User +from users.models import Club, Adherent + +class MailingSerializer(serializers.ModelSerializer): + name = serializers.CharField(source='pseudo') -class MailSerializer(serializers.ModelSerializer): class Meta: - model = User + model = Club + fields = ('name',) + +class MailingMemberSerializer(serializers.ModelSerializer): + class Meta: + model = Adherent fields = ('email',) diff --git a/users/urls.py b/users/urls.py index 425f9618..f15208d5 100644 --- a/users/urls.py +++ b/users/urls.py @@ -102,5 +102,29 @@ urlpatterns = [ ), url(r'^$', views.index, name='index'), url(r'^index_clubs/$', views.index_clubs, name='index-clubs'), - url(r'^rest/mailing/$', views.mailing, name='mailing'), + url( + r'^rest/ml/std/$', + views.ml_std_list, + name='ml-std-list' + ), + url( + r'^rest/ml/std/member/(?P\w+)/$', + views.ml_std_members, + name='ml-std-members' + ), + url( + r'^rest/ml/club/$', + views.ml_club_list, + name='ml-club-list' + ), + url( + r'^rest/ml/club/admin/(?P\w+)/$', + views.ml_club_admins, + name='ml-club-admins' + ), + url( + r'^rest/ml/club/member/(?P\w+)/$', + views.ml_club_members, + name='ml-club-members' + ), ] diff --git a/users/views.py b/users/views.py index 8d9ec603..5d0e31a6 100644 --- a/users/views.py +++ b/users/views.py @@ -52,7 +52,7 @@ from rest_framework.renderers import JSONRenderer from reversion.models import Version from reversion import revisions as reversion -from users.serializers import MailSerializer +from users.serializers import MailingSerializer, MailingMemberSerializer from users.models import ( User, Ban, @@ -843,9 +843,64 @@ class JSONResponse(HttpResponse): @csrf_exempt @login_required @permission_required('machines.serveur') -def mailing(request): - """ Fonction de serialisation des addresses mail de tous les users - Pour generation de ml all users""" - mails = all_has_access().values('email').distinct() - seria = MailSerializer(mails, many=True) +def ml_std_list(request): + """ API view sending all the available standard mailings""" + return JSONResponse([ + {'name': 'adherents'} + ]) + + +@csrf_exempt +@login_required +@permission_required('machines.serveur') +def ml_std_members(request, ml_name): + """ API view sending all the members for a standard mailing""" + # All with active connextion + if ml_name == 'adherents': + members = all_has_access().values('email').distinct() + # Unknown mailing + else: + messages.error(request, "Cette mailing n'existe pas") + return redirect(reverse('index')) + seria = MailingMemberSerializer(members, many=True) + return JSONResponse(seria.data) + + +@csrf_exempt +@login_required +@permission_required('machines.serveur') +def ml_club_list(request): + """ API view sending all the available club mailings""" + clubs = Club.objects.filter(mailing=True).values('pseudo') + seria = MailingSerializer(clubs, many=True) + return JSONResponse(seria.data) + + +@csrf_exempt +@login_required +@permission_required('machines.serveur') +def ml_club_admins(request, ml_name): + """ API view sending all the administrators for a specific club mailing""" + try: + club = Club.objects.get(mailing=True, pseudo=ml_name) + except Club.DoesNotExist: + messages.error(request, "Cette mailing n'existe pas") + return redirect(reverse('index')) + members = club.administrators.all().values('email').distinct() + seria = MailingMemberSerializer(members, many=True) + return JSONResponse(seria.data) + + +@csrf_exempt +@login_required +@permission_required('machines.serveur') +def ml_club_members(request, ml_name): + """ API view sending all the members for a specific club mailing""" + try: + club = Club.objects.get(mailing=True, pseudo=ml_name) + except Club.DoesNotExist: + messages.error(request, "Cette mailing n'existe pas") + return redirect(reverse('index')) + members = club.administrators.all().values('email').distinct() | club.members.all().values('email').distinct() + seria = MailingMemberSerializer(members, many=True) return JSONResponse(seria.data)