Add new JSON API for mailing lists

parent f93d2ab3
import hashlib
from rest_framework import permissions
class MailingListPermission(permissions.BasePermission):
"""
Permission check for mailing list secret key
"""
def has_permission(self, request, view):
if request.user.is_superuser:
return True
if 'secret' in request.GET:
apihash = hashlib.sha1(request.GET['secret']
.encode('utf-8')).hexdigest()
return apihash == '356a192b7913b04c54574d18c28d46e6395428ab'
return False
from rest_framework import serializers
from mailinglists.models import MailingList
class MailingListSerializer(serializers.ModelSerializer):
class Meta:
model = MailingList
fields = ('name', 'prefix', 'archived', 'moderated', 'addresses')
addresses = serializers.SerializerMethodField('_addresses')
def _addresses(self, instance):
return instance.all_addresses()
from rest_framework import routers
from mailinglists.api import viewsets
router = routers.SimpleRouter()
router.register(r'mailinglists', viewsets.MailingListViewset)
urlpatterns = router.urls
from rest_framework import viewsets
from mailinglists import services
from mailinglists.api.permissions import MailingListPermission
from mailinglists.api.serializers import MailingListSerializer
from mailinglists.models import MailingList
class MailingListViewset(viewsets.ReadOnlyModelViewSet):
permission_classes = [MailingListPermission]
queryset = MailingList.objects.all()
serializer_class = MailingListSerializer
def list(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
automatic_lists = services.get_automatic_lists()
if automatic_lists is not None:
response.data = list(response.data) + automatic_lists
return response
from activemembers.models import CommitteeMembership
from members.models import Member
from thaliawebsite.settings import settings
def get_automatic_lists():
memberships = (CommitteeMembership.active_memberships
.filter(committee__board=None)
.filter(chair=True)
.prefetch_related('member__user'))
committee_chairs = [x.member for x in memberships]
# name, prefix, members, archived, moderated, multilingual
lists = [
('leden', '[THALIA]', Member.all_with_membership(
'member', 'user'), True, True, True),
('begunstigers', '[THALIA]', Member.all_with_membership(
'supporter', 'user'), True, True, True),
('ereleden', '[THALIA]', Member.all_with_membership(
'honorary', 'user'), True, True, True),
('members', '[THALIA]', Member.all_with_membership(
'member', 'user'), True, True, True),
('supporters', '[THALIA]', Member.all_with_membership(
'supporter', 'user'), True, True, True),
('honorary', '[THALIA]', Member.all_with_membership(
'honorary', 'user'), True, True, True),
('commissievoorzitters', '[THALIA] [VOORZITTERS]',
committee_chairs, True, False, False),
('optin', '[THALIA] [OPTIN]', Member.active_members.filter(
receive_optin=True).prefetch_related('user'), True, True, False),
]
return_data = []
for list in lists:
data = {
'name': list[0],
'prefix': list[1],
'archived': list[3],
'moderated': list[4],
}
if list[5]:
data['addresses'] = (member.user.email for member in list[2])
return_data.append(data)
for language in settings.LANGUAGES:
localized_data = data.copy()
localized_data['addresses'] = [
member.user.email for member in list[2]
if member.language == language[0]]
localized_data['name'] += '-{}'.format(language[0])
return_data.append(localized_data)
else:
data['addresses'] = (member.user.email for member in list[2])
return_data.append(data)
return return_data
......@@ -106,6 +106,7 @@ urlpatterns = [
url(r'^', include('events.api.urls')),
url(r'^', include('members.api.urls')),
url(r'^', include('partners.api.urls')),
url(r'^', include('mailinglists.api.urls')),
], namespace='v1')),
])),
url(r'^education/', include('education.urls')),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment