from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
+from . import mailing
+
contact_forms = {}
admin_list_width = 0
__metaclass__ = ContactFormMeta
form_tag = None
+ old_form_tags = []
form_title = _('Contact form')
submit_label = _('Submit')
admin_list = None
+ result_page = False
+ mailing_field = None
required_css_class = 'required'
- contact = forms.CharField(max_length=128)
+ contact = NotImplemented
def save(self, request, formsets=None):
from .models import Attachment, Contact
sub_body[name] = value
if sub_body:
body.setdefault(f.form_tag, []).append(sub_body)
-
+
contact = Contact.objects.create(
body=body,
ip=request.META['REMOTE_ADDR'],
'contact/%s/mail_subject.txt' % self.form_tag,
'contact/mail_subject.txt',
], dictionary, context).strip()
- mail_body = render_to_string([
- 'contact/%s/mail_body.txt' % self.form_tag,
- 'contact/mail_body.txt',
- ], dictionary, context)
+ if self.result_page:
+ mail_body = render_to_string(
+ 'contact/%s/results_email.txt' % contact.form_tag,
+ {
+ 'contact': contact,
+ 'results': self.results(contact),
+ }, context)
+ else:
+ mail_body = render_to_string([
+ 'contact/%s/mail_body.txt' % self.form_tag,
+ 'contact/mail_body.txt',
+ ], dictionary, context)
send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [contact.contact], fail_silently=True)
+ if self.mailing_field and self.cleaned_data[self.mailing_field]:
+ email = self.cleaned_data['contact']
+ mailing.subscribe(email)
return contact
--- /dev/null
+# -*- coding: utf-8 -*-
+
+from hashlib import md5
+
+import requests
+from django.conf import settings
+from mailchimp3 import MailChimp
+from mailchimp3.mailchimpclient import MailChimpError
+
+INTERESTS = {settings.MAILCHIMP_GROUP_ID: True}
+
+
+def get_client():
+ headers = requests.utils.default_headers()
+ headers['User-Agent'] = '%s (%s)' % settings.MANAGERS[0]
+
+
+def subscriber_hash(email):
+ return md5(email).hexdigest()
+
+
+def remove_from_groups(email, client):
+ group_ids = []
+ categories = client.lists.interest_categories.all(list_id=settings.MAILCHIMP_LIST_ID)['categories']
+ for category in categories:
+ groups = client.lists.interest_categories.interests.all(
+ list_id=settings.MAILCHIMP_LIST_ID, category_id=category['id'])['interests']
+ group_ids += [group['id'] for group in groups]
+ interests = {group_id: False for group_id in group_ids}
+ client.lists.members.update(
+ settings.MAILCHIMP_LIST_ID, subscriber_hash(email),
+ data={'interests': interests})
+
+
+def subscribe(email):
+ client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY, timeout=10.0)
+ try:
+ member = client.lists.members.get(settings.MAILCHIMP_LIST_ID, subscriber_hash(email))
+ except MailChimpError:
+ pass
+ else:
+ if member['status'] != 'subscribed':
+ remove_from_groups(email, client)
+ client.lists.members.create_or_update(
+ settings.MAILCHIMP_LIST_ID, subscriber_hash(email),
+ data={
+ 'email_address': email,
+ 'status_if_new': 'subscribed',
+ 'status': 'subscribed',
+ 'interests': INTERESTS,
+ }
+ )
--- /dev/null
+# -*- 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
ends_on = (2017, 11, 17, 0, 5)
disabled_template = 'wtem/disabled_contact_form.html'
form_tag = "olimpiada"
+ old_form_tags = ["olimpiada-2016"]
form_title = u"Olimpiada Cyfrowa - Elektroniczny System Zgłoszeń"
submit_label = u"Wyślij zgłoszenie"
admin_list = ['nazwisko', 'school']
'student': forms.formsets.formset_factory(WTEMStudentForm, formset=NonEmptyBaseFormSet),
'commission': forms.formsets.formset_factory(CommissionForm),
}
+ mailing_field = 'zgoda_newsletter'
contact = forms.EmailField(label=u'Adres e-mail Przewodniczącego/Przewodniczącej', max_length=128)
przewodniczacy = forms.CharField(label=u'Imię i nazwisko Przewodniczącego/Przewodniczącej', max_length=128)
unidecode
-pytz
\ No newline at end of file
+pytz
+
+markdown2
+
+mailchimp3
\ No newline at end of file