Make mailinglist API functionally equivalent

This should probably be replaced again soon by something nicer,
but this is functionally equivalent to what we had.
parent bfc0991d
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-08-05 09:01
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mailinglists', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='listalias',
name='alias',
field=models.CharField(max_length=100),
),
migrations.AlterField(
model_name='mailinglist',
name='name',
field=models.CharField(max_length=100),
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-08-05 09:08
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mailinglists', '0002_auto_20160805_1101'),
]
operations = [
migrations.AlterField(
model_name='listalias',
name='alias',
field=models.CharField(max_length=100, validators=[django.core.validators.RegexValidator(message='Enter a [a-zA-Z0-9] name', regex='[a-zA-Z0-9]+')]),
),
migrations.AlterField(
model_name='mailinglist',
name='name',
field=models.CharField(max_length=100, validators=[django.core.validators.RegexValidator(message='Enter a [a-zA-Z0-9] name', regex='[a-zA-Z0-9]+')]),
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-08-05 09:13
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mailinglists', '0003_auto_20160805_1108'),
]
operations = [
migrations.AlterField(
model_name='listalias',
name='alias',
field=models.CharField(max_length=100, validators=[django.core.validators.RegexValidator(message='Enter a [a-zA-Z0-9] name', regex='^[a-zA-Z0-9]+$')]),
),
migrations.AlterField(
model_name='mailinglist',
name='name',
field=models.CharField(max_length=100, validators=[django.core.validators.RegexValidator(message='Enter a [a-zA-Z0-9] name', regex='^[a-zA-Z0-9]+$')]),
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10rc1 on 2016-08-05 20:45
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mailinglists', '0004_auto_20160805_1113'),
]
operations = [
migrations.AlterField(
model_name='listalias',
name='alias',
field=models.CharField(max_length=100, validators=[django.core.validators.RegexValidator(message='Enter a simpler name', regex='^[a-zA-Z0-9]+$')]),
),
migrations.AlterField(
model_name='mailinglist',
name='name',
field=models.CharField(max_length=100, validators=[django.core.validators.RegexValidator(message='Enter a simpler name', regex='^[a-zA-Z0-9]+$')]),
),
]
from django.db import models
from django.core import validators
from django.utils.translation import ugettext_lazy as _
from members.models import Member
from committees.models import Committee
class MailingList(models.Model):
name = models.EmailField()
name = models.CharField(max_length=100,
validators=[validators.RegexValidator(
regex=r'^[a-zA-Z0-9]+$',
message=_('Enter a simpler name'))
],
)
prefix = models.CharField(max_length=200)
archived = models.BooleanField(default=True)
moderated = models.BooleanField(default=False)
......@@ -14,11 +21,11 @@ class MailingList(models.Model):
def all_addresses(self):
for member in self.members.all():
yield member.email
yield member.user.email
for committee in self.committees.all().prefetch_related("members"):
for member in committee.members.all():
yield member.email
yield member.user.email
for address in self.addresses.all():
yield address
......@@ -31,7 +38,15 @@ class VerbatimAddress(models.Model):
address = models.EmailField()
mailinglist = models.ForeignKey(MailingList, related_name='addresses')
def __str__(self):
return self.address
class ListAlias(models.Model):
alias = models.EmailField()
alias = models.CharField(max_length=100,
validators=[validators.RegexValidator(
regex=r'^[a-zA-Z0-9]+$',
message=_('Enter a simpler name'))
],
)
mailinglist = models.ForeignKey(MailingList, related_name='aliasses')
START
{% for member in members %}{{ member.user.email }}
{% endfor %}END
START
{{ list.name }}
{{ list.prefix }}
{{ list.archived|yesno:"1,0" }}
{{ list.moderated|yesno:"1,0" }}
{% for address in list.all_addresses %}{{ address }}
{% endfor %}END
START
{% for list in lists %}{{ list.pk }}
{% endfor %}END
from django.conf.urls import include, url
from . import views
urlpatterns = [
url('^', views.index, name='index'),
]
from django.core.exceptions import PermissionDenied
from django.shortcuts import render, get_object_or_404
from django.http import Http404
from .models import MailingList
from members.models import Member
from committees.models import CommitteeMembership
import hashlib
# 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
def API(request):
pass
# - 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()}
return render(request, 'mailinglists/lists.txt', context,
content_type='text/plain')
elif 'list' in request.GET:
mailinglist = get_object_or_404(MailingList,
pk=int(request.GET['list']))
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(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")
......@@ -76,6 +76,12 @@ class Member(models.Model):
is_active.boolean = True
is_active.short_description = _('Is this user currently active')
@classmethod
def all_with_membership(cls, membership_type, prefetch=None):
return [x for x in cls.objects.all().prefetch_related(prefetch)
if x.current_membership
and x.current_membership.type == membership_type]
# ---- Address information -----
address_street = models.CharField(
......
......@@ -48,6 +48,7 @@ INSTALLED_APPS = [
'committees',
'photos',
'utils',
'mailinglists',
]
MIDDLEWARE = [
......
......@@ -27,6 +27,7 @@ import members
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='index.html'), name='index'),
url(r'^admin/', admin.site.urls),
url(r'^mailinglists/', include('mailinglists.urls', namespace='mailinglists')),
url(r'^members/', include('members.urls', namespace='members')),
url(r'^nyi$', TemplateView.as_view(template_name='status/nyi.html'), name='#'),
url(r'^association/', include([
......
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