Commit 59c08d4f authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Merge branch '95-data-migrations-for-all-mailing-lists' into 'master'

Resolve "Data migrations for all mailing lists"

Closes #95

See merge request !114
parents c1c3ba31 63b7b5ff
......@@ -17,6 +17,11 @@ This is taken care of during member migration.
## Mailing lists
Make sure that the API key is available as `MIGRATION_KEY` in settings.py.
To migrate the mailinglists, make sure that members, committees and boards
have been migrated. Then simply execute `python manage.py migratelists`.
## Members
To migrate members, committees, boards, memberships of committees and boards,
......
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand
from activemembers.models import Committee, Board
from members.models import Member
from mailinglists.models import MailingList, VerbatimAddress
import requests
class Command(BaseCommand):
help = "Migrates mailinglists. This should be done after migrating members"
def handle(self, *args, **options):
if not settings.MIGRATION_KEY:
raise ImproperlyConfigured("MIGRATION_KEY not specified")
url = "https://thalia.nu/api/export_mail.php?apikey={}&lists".format(
settings.MIGRATION_KEY
)
lines = requests.get(url).text.split('\n')
for lID in lines:
if ':' in lID:
raise Exception("Turns out we actually used aliasses.")
if lID.isnumeric():
url = ("https://thalia.nu/api/export_mail.php"
"?apikey={}&list={}".format(
settings.MIGRATION_KEY,
lID,
)
)
lines = requests.get(url).text.split('\n')
lines = lines[1:-2]
name, prefix, arch, mod, *lines = lines
mlist, cr = MailingList.objects.get_or_create(name=name)
mlist.prefix = prefix
mlist.archived = bool(arch)
mlist.moderated = bool(mod)
group_it = iter(lines[:lines.index('-')])
groups = []
while True:
try:
groups.append((next(group_it), []))
while True:
x = next(group_it)
if x == '=':
break
groups[-1][1].append(x)
except StopIteration:
break
lines = lines[lines.index('-')+1:]
users = lines[:lines.index('-')]
verbatims = lines[lines.index('-')+1:]
mlist.save()
for user in users:
mlist.members.add(Member.objects.get(user__username=user))
for g, users in groups:
try:
mlist.committees.add(Committee.objects.get(name_nl=g))
except Committee.DoesNotExist:
try:
mlist.committees.add(Board.objects.get(name_nl=g))
except Committee.DoesNotExist:
print("[{}] Did not find group '{}'".format(name,
g))
print(" Adding individual users: {}".format(
', '.join(users))
)
for user in users:
mlist.members.add(
Member.objects.get(user__username=user))
for v in verbatims:
obj = VerbatimAddress(address=v, mailinglist=mlist)
obj.save()
mlist.save()
......@@ -31,6 +31,7 @@ class Command(BaseCommand):
url = "https://thalia.nu/api/members_export.php?apikey={}".format(
settings.MIGRATION_KEY
)
print("Querying concrete5.. this may take a while")
data = json.loads(requests.get(url).text)
groups = {}
......@@ -44,6 +45,7 @@ class Command(BaseCommand):
for board in data['boards']:
obj, cr = Board.objects.get_or_create(name_nl=board['name'])
obj.name_en = board['name']
print(obj.name_en)
groups[board['gID']] = obj
img = soup.find("img", {"alt": board['name'][8:]})
if img:
......@@ -64,6 +66,7 @@ class Command(BaseCommand):
obj, cr = Committee.objects.get_or_create(
name_nl=committee['name'])
obj.name_en = committee['name']
print(obj.name_en)
groups[committee['gID']] = obj
obj.save()
if committee['name'] not in links:
......
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