hook mailchimp to contact forms
[edumed.git] / contact / management / commands / export_newsletter.py
diff --git a/contact/management/commands/export_newsletter.py b/contact/management/commands/export_newsletter.py
new file mode 100644 (file)
index 0000000..43b35ee
--- /dev/null
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+# This file is part of EduMed, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from django.core.management.base import BaseCommand
+
+from contact.models import Contact
+from edumed import contact_forms
+
+FORMS = (
+    contact_forms.OlimpiadaForm,
+)
+
+
+class Command(BaseCommand):
+    help = 'Export contacts for newsletter.'
+
+    def handle(self, **options):
+        addresses = set(self.get_addresses())
+        for address in addresses:
+            print address
+
+    def get_addresses(self):
+        for form in FORMS:
+            tags = [form.form_tag] + form.old_form_tags
+            contacts = Contact.objects.filter(form_tag__in=tags)
+            for contact in contacts:
+                if contact.body.get(form.mailing_field):
+                    yield contact.contact