mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-23 11:53:12 +00:00
API: Add support for mailings
This commit is contained in:
parent
ee3f4fb598
commit
a86369e7c8
3 changed files with 109 additions and 0 deletions
|
@ -23,6 +23,7 @@ Serializers for the API app
|
|||
"""
|
||||
|
||||
from rest_framework import serializers
|
||||
from users.models import Club, Adherent
|
||||
from machines.models import Service_link
|
||||
|
||||
|
||||
|
@ -34,6 +35,20 @@ class ServiceLinkSerializer(serializers.ModelSerializer):
|
|||
fields = ('name',)
|
||||
|
||||
|
||||
class MailingSerializer(serializers.ModelSerializer):
|
||||
name = serializers.CharField(source='pseudo')
|
||||
|
||||
class Meta:
|
||||
model = Club
|
||||
fields = ('name',)
|
||||
|
||||
|
||||
class MailingMemberSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Adherent
|
||||
fields = ('email', 'name', 'surname', 'pseudo',)
|
||||
|
||||
|
||||
class ServicesSerializer(serializers.ModelSerializer):
|
||||
"""Evaluation d'un Service, et serialisation"""
|
||||
server = serializers.SerializerMethodField('get_server_name')
|
||||
|
|
|
@ -34,4 +34,10 @@ urlpatterns = [
|
|||
url(r'^services/$', views.services),
|
||||
url(r'^services/(?P<server_name>\w+)/(?P<service_name>\w+)/regen/$', views.services_server_service_regen),
|
||||
url(r'^services/(?P<server_name>\w+)/$', views.services_server),
|
||||
|
||||
# Mailings
|
||||
url(r'^mailing/standard/$', views.mailing_standard),
|
||||
url(r'^mailing/standard/(?P<ml_name>\w+)/members/$', views.mailing_standard_ml_members),
|
||||
url(r'^mailing/club/$', views.mailing_club),
|
||||
url(r'^mailing/club/(?P<ml_name>\w+)/members/$', views.mailing_club_ml_members),
|
||||
]
|
||||
|
|
88
api/views.py
88
api/views.py
|
@ -27,6 +27,9 @@ HTML pages such as the login and index pages for a better integration.
|
|||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from re2o.utils import all_has_access
|
||||
|
||||
from users.models import Club
|
||||
from machines.models import Service_link, Service, Interface, Domain
|
||||
|
||||
from .serializers import *
|
||||
|
@ -109,3 +112,88 @@ def services_server(request, server_name):
|
|||
services = query.all()
|
||||
seria = ServiceLinkSerializer(services, many=True)
|
||||
return JSONSuccess(seria.data)
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
@login_required
|
||||
@permission_required('machines.serveur')
|
||||
@accept_method(['GET'])
|
||||
def mailing_standard(request):
|
||||
"""All the available standard mailings.
|
||||
|
||||
Returns:
|
||||
GET:
|
||||
A JSONSucess response with a field `data` containing:
|
||||
* a list of dictionnaries (one for each mailing) containing:
|
||||
* a field `name`: the name of a mailing
|
||||
"""
|
||||
return JSONSuccess([
|
||||
{'name': 'adherents'}
|
||||
])
|
||||
|
||||
@csrf_exempt
|
||||
@login_required
|
||||
@permission_required('machines.serveur')
|
||||
@accept_method(['GET'])
|
||||
def mailing_standard_ml_members(request):
|
||||
"""All the members of a specific standard mailing
|
||||
|
||||
Returns:
|
||||
GET:
|
||||
A JSONSucess response with a field `data` containing:
|
||||
* a list if dictionnaries (one for each member) containing:
|
||||
* a field `email`: the email of the member
|
||||
* a field `name`: the name of the member
|
||||
* a field `surname`: the surname of the member
|
||||
* a field `pseudo`: the pseudo of the member
|
||||
"""
|
||||
# All with active connextion
|
||||
if ml_name == 'adherents':
|
||||
members = all_has_access().values('email').distinct()
|
||||
# Unknown mailing
|
||||
else:
|
||||
return JSONError("This mailing does not exist")
|
||||
seria = MailingMemberSerializer(members, many=True)
|
||||
return JSONSuccess(seria.data)
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
@login_required
|
||||
@permission_required('machines.serveur')
|
||||
@accept_method(['GET'])
|
||||
def mailing_club(request):
|
||||
"""All the available club mailings.
|
||||
|
||||
Returns:
|
||||
GET:
|
||||
A JSONSucess response with a field `data` containing:
|
||||
* a list of dictionnaries (one for each mailing) containing:
|
||||
* a field `name` indicating the name of a mailing
|
||||
"""
|
||||
clubs = Club.objects.filter(mailing=True).values('pseudo')
|
||||
seria = MailingSerializer(clubs, many=True)
|
||||
return JSONSuccess(seria.data)
|
||||
|
||||
@csrf_exempt
|
||||
@login_required
|
||||
@permission_required('machines.serveur')
|
||||
@accept_method(['GET'])
|
||||
def mailing_club_ml_members(request):
|
||||
"""All the members of a specific club mailing
|
||||
|
||||
Returns:
|
||||
GET:
|
||||
A JSONSucess response with a field `data` containing:
|
||||
* a list if dictionnaries (one for each member) containing:
|
||||
* a field `email`: the email of the member
|
||||
* a field `name`: the name of the member
|
||||
* a field `surname`: the surname of the member
|
||||
* a field `pseudo`: the pseudo of the member
|
||||
"""
|
||||
try:
|
||||
club = Club.objects.get(mailing=True, pseudo=ml_name)
|
||||
except Club.DoesNotExist:
|
||||
return JSONError("This mailing does not exist")
|
||||
members = club.administrators.all().values('email').distinct()
|
||||
seria = MailingMemberSerializer(members, many=True)
|
||||
return JSONSuccess(seria.data)
|
||||
|
|
Loading…
Reference in a new issue