Write initial migration for mailinglists

TODO:
 - some groups do not exist; need to be flattened or created
parent 97db6566
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)
groups = lines[:lines.index('-')]
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 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))
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:
......
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