]> git.0d.be Git - django-panik-newsletter.git/commitdiff
add a command to import subscribers from spip
authorFrédéric Péters <fpeters@0d.be>
Sun, 3 Nov 2013 10:31:11 +0000 (11:31 +0100)
committerFrédéric Péters <fpeters@0d.be>
Sun, 3 Nov 2013 10:31:11 +0000 (11:31 +0100)
newsletter/management/commands/load-from-spip-liste.py [new file with mode: 0644]

diff --git a/newsletter/management/commands/load-from-spip-liste.py b/newsletter/management/commands/load-from-spip-liste.py
new file mode 100644 (file)
index 0000000..462ec65
--- /dev/null
@@ -0,0 +1,78 @@
+# -*- coding: utf-8 -*-
+
+from datetime import datetime
+import time
+import gzip
+import xml.etree.ElementTree as ET
+import os
+import re
+import urllib2
+from PIL import Image
+
+from optparse import make_option
+
+from django.conf import settings
+from django.core.files import File
+from django.core.management.base import BaseCommand, CommandError
+from django.core.urlresolvers import reverse
+from django.utils.html import strip_tags
+from django.utils.text import slugify
+
+from ...models import Subscriber
+
+
+class Author(object):
+    id_auteur = None
+    email = None
+
+
+class Command(BaseCommand):
+    args = 'filename'
+    help = 'Load newsletter subscribers from a Spip dump file'
+
+    def handle(self, filename, verbosity, **options):
+        self.verbose = (verbosity > 1)
+
+        with open(filename) as fd:
+            if self.verbose:
+                print 'Reading SPIP dump file'
+            content = fd.read()
+            # the spip_courriers parts of the spip export are not properly
+            # encoded, we manually remove them here so the XML file can be
+            # parsed correctly.
+            content = content[:content.find('<spip_courriers>')] + \
+                      content[content.rfind('</spip_courriers>')+17:]
+            self.root = ET.fromstring(content)
+
+            self.load_authors()
+
+            if self.verbose:
+                print 'Creating subscribers'
+            for author_xml in self.root.iter('spip_auteurs_elargis'):
+                if author_xml.find('spip_listes_format').text not in ('text', 'html'):
+                    continue
+                author = self.authors.get(author_xml.find('id_auteur').text)
+                if author.email is None:
+                    continue
+
+                try:
+                    Subscriber.objects.get(email=author.email)
+                except Subscriber.DoesNotExist:
+                    pass
+                else:
+                    continue
+
+                subscriber = Subscriber()
+                subscriber.email = author.email
+                subscriber.is_validated = True
+                subscriber.is_registered = True
+                subscriber.password = 'xxx'
+                subscriber.save()
+
+    def load_authors(self):
+        self.authors = {}
+        for author_xml in self.root.iter('spip_auteurs'):
+            author = Author()
+            for attr in ('id_auteur', 'email'):
+                setattr(author, attr, author_xml.find(attr).text)
+            self.authors[author.id_auteur] = author