diff --git a/api/serializers.py b/api/serializers.py index 647f9d26..1a1f970e 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -577,3 +577,18 @@ class DNSZonesSerializer(serializers.ModelSerializer): fields = ('name', 'soa', 'ns_records', 'originv4', 'originv6', 'mx_records', 'txt_records', 'srv_records', 'a_records', 'aaaa_records', 'cname_records') + + +# Mailing + + +class MailingMemberSerializer(UserSerializer): + class Meta(UserSerializer.Meta): + fields = ('name', 'pseudo', 'email') + +class MailingSerializer(ClubSerializer): + members = MailingMemberSerializer(many=True) + admins = MailingMemberSerializer(source='administrators', many=True) + + class Meta(ClubSerializer.Meta): + fields = ('name', 'members', 'admins') diff --git a/api/urls.py b/api/urls.py index 57ce1e12..1d244de8 100644 --- a/api/urls.py +++ b/api/urls.py @@ -94,5 +94,7 @@ urlpatterns = [ url(r'^', include(router.urls)), url(r'^dhcp/hostmacip', views.HostMacIpView.as_view()), url(r'^dns/zones', views.DNSZonesView.as_view()), + url(r'^mailing/standard', views.StandardMailingView.as_view()), + url(r'^mailing/club', views.ClubMailingView.as_view()), url(r'^token-auth', views.ObtainExpiringAuthToken.as_view()) ] diff --git a/api/views.py b/api/views.py index b290d578..fce0bc23 100644 --- a/api/views.py +++ b/api/views.py @@ -31,7 +31,7 @@ from django.conf import settings from rest_framework.authtoken.views import ObtainAuthToken from rest_framework.authtoken.models import Token from rest_framework.response import Response -from rest_framework import viewsets, generics +from rest_framework import viewsets, generics, views import cotisations.models as cotisations import machines.models as machines @@ -39,9 +39,10 @@ import preferences.models as preferences import topologie.models as topologie import users.models as users -from re2o.utils import all_active_interfaces +from re2o.utils import all_active_interfaces, all_has_access from . import serializers +from .pagination import PageSizedPagination # COTISATIONS APP @@ -345,6 +346,26 @@ class DNSZonesView(generics.ListAPIView): serializer_class = serializers.DNSZonesSerializer +# Mailing views + + +class StandardMailingView(views.APIView): + pagination_class = PageSizedPagination + get_queryset = lambda self: all_has_access() + + def get(self, request, format=None): + adherents_data = serializers.MailingMemberSerializer(self.get_queryset(), many=True).data + data = [{'name': 'adherents', 'members': adherents_data}] + paginator = self.pagination_class() + paginator.paginate_queryset(data, request) + return paginator.get_paginated_response(data) + + +class ClubMailingView(generics.ListAPIView): + queryset = users.Club.objects.all() + serializer_class = serializers.MailingSerializer + + # Subclass the standard rest_framework.auth_token.views.ObtainAuthToken # in order to renew the lease of the token and add expiration time class ObtainExpiringAuthToken(ObtainAuthToken):