| @@ -0,0 +1,33 @@ | |||||
| from django.core import mail | |||||
| from django.core.mail import EmailMessage | |||||
| from django.conf import settings | |||||
| from django.core.mail.backends.smtp import EmailBackend | |||||
| NOT_SENT, SENT, PARTLY_SENT = 0, 1, 2 | |||||
| def send_from_arbeitsministerium(subject, content, recipients, reply_to=None): | |||||
| return send(subject, content, settings.EMAIL_ARBEITSMINISTERIUM, recipients, | |||||
| reply_to=reply_to, | |||||
| auth_user=settings.EMAIL_ARBEITSMINISTERIUM_USER, | |||||
| auth_password=settings.EMAIL_ARBEITSMINISTERIUM_PASSWORD) | |||||
| def send(subject, content, sender, recipients, reply_to=None, | |||||
| auth_user=None, auth_password=None): | |||||
| failed, succeeded = False, False | |||||
| if type(recipients) != list: | |||||
| recipients = [recipients] | |||||
| for recipient in set(recipients): | |||||
| try: | |||||
| mail.send_mail(subject, content, sender, recipients, | |||||
| auth_user=auth_user, | |||||
| auth_password=auth_password) | |||||
| except Exception as e: | |||||
| print("Error when sending mail:", e) | |||||
| failed = True | |||||
| else: | |||||
| succeeded = True | |||||
| return NOT_SENT if failed and not succeeded else SENT if not failed\ | |||||
| and succeeded else PARTLY_SENT | |||||
| @@ -2,6 +2,7 @@ from django.shortcuts import render | |||||
| from django import forms | from django import forms | ||||
| from captcha.fields import CaptchaField | from captcha.fields import CaptchaField | ||||
| from .models import Betrieb, Partei, PresidentCandidate, Question | from .models import Betrieb, Partei, PresidentCandidate, Question | ||||
| from .mailutils import send_from_arbeitsministerium | |||||
| class BetriebForm(forms.Form): | class BetriebForm(forms.Form): | ||||
| @@ -51,6 +52,10 @@ def betrieb_new(request): | |||||
| ip_address=get_client_ip(request), | ip_address=get_client_ip(request), | ||||
| confirmed=False) | confirmed=False) | ||||
| betrieb.save() | betrieb.save() | ||||
| send_from_arbeitsministerium( | |||||
| 'Anmeldung des Betriebs: {}'.format(betrieb.name), | |||||
| MAIL_CONTENT_BETRIEB.format(name=betrieb.manager), [betrieb.email] | |||||
| ) | |||||
| return render_confirmation(request) | return render_confirmation(request) | ||||
| else: | else: | ||||
| form = BetriebForm() | form = BetriebForm() | ||||
| @@ -118,3 +123,10 @@ def get_client_ip(request): | |||||
| else: | else: | ||||
| ip = request.META.get('REMOTE_ADDR') | ip = request.META.get('REMOTE_ADDR') | ||||
| return ip | return ip | ||||
| MAIL_CONTENT_BETRIEB = "Lieber {name},\nvielen Dank für die Anmeldung Deines" \ | |||||
| " Betriebes. Die Informationsveranstaltungen beginnen erst im Februar, deshalb\n" \ | |||||
| "wird sich das Arbeitsministerium dann erst mit Dir in Verbindung setzen.\n\n" \ | |||||
| "Viele Grüße,\n" \ | |||||
| "Organisation Goethopia" | |||||
| @@ -13,6 +13,8 @@ https://docs.djangoproject.com/en/1.8/ref/settings/ | |||||
| # Build paths inside the project like this: os.path.join(BASE_DIR, ...) | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||||
| import os | import os | ||||
| deployed = '1' == os.environ.get('DJANGO_DEPLOY', '0') | |||||
| BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||||
| @@ -132,3 +134,15 @@ STATICFILES_DIRS = [ | |||||
| STATIC_ROOT = os.environ.get('DJANGO_STATIC_ROOT', | STATIC_ROOT = os.environ.get('DJANGO_STATIC_ROOT', | ||||
| os.path.join((os.path.join(BASE_DIR, os.pardir)), "static")) | os.path.join((os.path.join(BASE_DIR, os.pardir)), "static")) | ||||
| # Email setup | |||||
| EMAIL_HOST = os.environ.get('EMAIL_HOST', 'localhost') | |||||
| EMAIL_PORT = 587 if deployed else 25 | |||||
| EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER', '') | |||||
| EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD', '') | |||||
| EMAIL_USE_TLS = True if deployed else False | |||||
| EMAIL_ARBEITSMINISTERIUM = os.environ.get('EMAIL_ARBEITSMINISTERIUM', '') | |||||
| EMAIL_ARBEITSMINISTERIUM_USER = os.environ.get('EMAIL_ARBEITSMINISTERIUM', '') | |||||
| EMAIL_ARBEITSMINISTERIUM_PASSWORD = os.environ.get('EMAIL_ARBEITSMINISTERIUM_PASSWORD', '') | |||||