change banner
[edumed.git] / contact / management / commands / export_newsletter.py
1 # -*- coding: utf-8 -*-
2 # This file is part of EduMed, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.core.management.base import BaseCommand
6
7 from contact.models import Contact
8 from edumed import contact_forms
9
10 FORMS = (
11     contact_forms.CooperateForm,
12     contact_forms.ContestForm,
13     contact_forms.WTEMForm,
14     contact_forms.TEMForm,
15     contact_forms.CybernauciForm,
16     contact_forms.WLEMForm,
17 )
18
19
20 class Command(BaseCommand):
21     help = 'Export contacts for newsletter.'
22
23     def handle(self, **options):
24         addresses = set(self.get_addresses())
25         for address in addresses:
26             print address
27
28     def get_addresses(self):
29         for form in FORMS:
30             tags = [form.form_tag] + form.old_form_tags
31             contacts = Contact.objects.filter(form_tag__in=tags)
32             for contact in contacts:
33                 if contact.body.get(form.mailing_field):
34                     yield contact.contact