Verified Commit a2af72cd authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Remove old mailing list sync API and add alias support

parent a22d7bcb
......@@ -6,9 +6,13 @@ from mailinglists.models import MailingList
class MailingListSerializer(serializers.ModelSerializer):
class Meta:
model = MailingList
fields = ('name', 'prefix', 'archived', 'moderated', 'addresses')
fields = ('names', 'prefix', 'archived', 'moderated', 'addresses')
names = serializers.SerializerMethodField('_names')
addresses = serializers.SerializerMethodField('_addresses')
def _names(self, instance):
return [instance.name] + [x.alias for x in instance.aliasses.all()]
def _addresses(self, instance):
return instance.all_addresses()
......@@ -25,42 +25,33 @@ def get_automatic_lists():
lists = []
lists += _create_automatic_list(
'leden', '[THALIA]', Member.all_with_membership('member', 'user'),
True, True, True)
['leden', 'members'], '[THALIA]',
Member.all_with_membership('member', 'user'), True, True, True)
lists += _create_automatic_list(
'begunstigers', '[THALIA]', Member.all_with_membership(
'supporter','user'), multilingual=True)
lists += _create_automatic_list(
'ereleden', '[THALIA]', Member.all_with_membership(
'honorary','user'), multilingual=True)
lists += _create_automatic_list(
'members', '[THALIA]', Member.all_with_membership(
'member', 'user'), multilingual=True)
lists += _create_automatic_list(
'supporters', '[THALIA]', Member.all_with_membership(
['begunstigers', 'supporters'], '[THALIA]', Member.all_with_membership(
'supporter', 'user'), multilingual=True)
lists += _create_automatic_list(
'honorary', '[THALIA]', Member.all_with_membership(
['ereleden', 'honorary'], '[THALIA]', Member.all_with_membership(
'honorary', 'user'), multilingual=True)
lists += _create_automatic_list(
'mentors', '[THALIA] [MENTORS]', mentors, moderated=False)
['mentors'], '[THALIA] [MENTORS]', mentors, moderated=False)
lists += _create_automatic_list(
'activemembers', '[THALIA] [COMMITTEES]',
['activemembers'], '[THALIA] [COMMITTEES]',
active_members)
lists += _create_automatic_list(
'commissievoorzitters', '[THALIA] [CHAIRS]',
['commissievoorzitters'], '[THALIA] [CHAIRS]',
committee_chairs, moderated=False)
lists += _create_automatic_list(
'optin', '[THALIA] [OPTIN]', Member.active_members.filter(
['optin'], '[THALIA] [OPTIN]', Member.active_members.filter(
receive_optin=True).prefetch_related('user'))
return lists
def _create_automatic_list(name, prefix, members,
def _create_automatic_list(names, prefix, members,
archived=True, moderated=True, multilingual=False):
data = {
'name': name,
'names': names,
'prefix': prefix,
'archived': archived,
'moderated': moderated,
......@@ -74,7 +65,8 @@ def _create_automatic_list(name, prefix, members,
localized_data['addresses'] = [
member.user.email for member in members
if member.language == language[0]]
localized_data['name'] += '-{}'.format(language[0])
localized_data['names'] = [
'{}-{}'.format(n, language[0]) for n in names]
yield localized_data
else:
data['addresses'] = [member.user.email for member in members]
......
from django.conf.urls import include, url
from . import views
app_name = "mailinglists"
urlpatterns = [
url('^', views.index, name='index'),
]
import hashlib
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.shortcuts import get_object_or_404, render
from activemembers.models import CommitteeMembership
from members.models import Member
from .models import MailingList
# Consider replacing this completely;
# - either by a cronjob Python script that queries the database directly
# - or by a __save__ handler that updates mailman when MailingList changes
# - or at least by a more nicely separated API with less GET variables
# see issue #29
def index(request):
if 'apikey' not in request.GET:
raise PermissionDenied
apihash = hashlib.sha1(request.GET['apikey'].encode('utf-8')).hexdigest()
if apihash != 'cb004452d9c80e295bebfc778871b3b082d70ad8':
raise PermissionDenied
if 'lists' in request.GET:
context = {'lists': MailingList.objects.all()
.prefetch_related('aliasses')}
return render(request, 'mailinglists/lists.txt', context,
content_type='text/plain')
elif 'list' in request.GET:
pk = request.GET['list'].split('_') # could be id_aliasname
mailinglist = get_object_or_404(MailingList, pk=int(pk[0]))
if len(pk) > 1 and mailinglist.aliasses.filter(alias=pk[1]):
mailinglist.name = pk[1]
return render(request, 'mailinglists/list_data.txt',
{'list': mailinglist}, content_type='text/plain')
elif 'membership_type' in request.GET:
membership_type = {"Current Members": "member",
"Benefactor": "supporter",
"Honorary Member": "honorary"
}[request.GET['membership_type']]
members = Member.all_with_membership(membership_type, 'user')
elif 'custom' in request.GET:
if request.GET['custom'] == 'commissievoorzitters':
memberships = (CommitteeMembership.active_memberships
.filter(committee__board=None)
.filter(chair=True)
.prefetch_related('member__user'))
members = [x.member for x in memberships]
# The intern is always included; we mock an object with a .email
members += [{'user': {'email': 'intern@thalia.nu'}}]
elif request.GET['custom'] == 'optin':
members = (Member.objects.filter(receive_optin=True)
.prefetch_related('user'))
try:
return render(request, 'mailinglists/custom_list.txt',
{'members': members}, content_type='text/plain')
except NameError:
raise Http404("Insufficient arguments")
......@@ -65,7 +65,6 @@ thalia_sitemap.update(events_sitemap)
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='index.html'), name='index'),
url(r'^admin/', admin.site.urls),
url(r'^mailinglists/', include('mailinglists.urls')),
url(r'^members/', include('members.urls')),
url(r'^account/$', members.views.account, name='account'),
url(r'^events/', include('events.urls')),
......
Supports Markdown
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