migratelists.py 3.24 KB
Newer Older
1
import requests
2 3 4 5
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand

6
from activemembers.models import Board, Committee
7
from mailinglists.models import MailingList, VerbatimAddress
8
from members.models import Member
9 10 11 12 13 14 15 16


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")
Thom Wiggers's avatar
Thom Wiggers committed
17 18 19
        url = ("https://oud.thalia.nu/api/export_mail.php"
               "?apikey={}&lists".format(
                settings.MIGRATION_KEY))
20 21 22 23 24 25
        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():
Thom Wiggers's avatar
Thom Wiggers committed
26
                url = ("https://oud.thalia.nu/api/export_mail.php"
27 28 29 30 31 32 33 34 35 36 37 38 39
                       "?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)

40 41 42 43 44 45 46 47 48 49 50 51
                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
52 53 54 55 56 57 58 59
                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))
60
                for g, users in groups:
61 62 63 64 65 66 67 68
                    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))
69 70 71 72 73 74
                            print("    Adding individual users: {}".format(
                                ', '.join(users))
                            )
                            for user in users:
                                mlist.members.add(
                                    Member.objects.get(user__username=user))
75 76 77 78 79
                for v in verbatims:
                    obj = VerbatimAddress(address=v, mailinglist=mlist)
                    obj.save()

                mlist.save()