Add email stuff

This commit is contained in:
Jan Tuomi
2017-10-11 22:40:19 +03:00
parent e7e21fb1ee
commit 04931eec42
4 changed files with 107 additions and 5 deletions
+38 -1
View File
@@ -13,6 +13,22 @@ from webapp.models import OhlhafvChallenge, KaehmyForm
from webapp.forms import OhlhafvForm, KaehmyForm_Form, KaehmyCommentForm
from webapp.tables import OhlhafvTable
from django.core.mail import send_mail
from django.conf import settings
def send_email(to, subject, body):
success = send_mail(
subject,
body,
settings.DEFAULT_EMAIL_FROM,
[to],
fail_silently=False,
)
if success == 0:
raise Exception('Failed to send email!')
@require_http_methods(["GET"])
def main_index(request, *args, **kwargs):
@@ -127,6 +143,17 @@ def kaehmy_submit(request, *args, **kwargs):
name=custom_name, is_board=custom_is_board)
custom_role.save()
application.custom_roles.add(custom_role)
form.save()
email = form.cleaned_data.get('email', '')
name = form.cleaned_data.get('name', 'Anonymous')
subject = 'Arwokas kirjattu kirje mahdolliselle tulewalle kiltahenkilölle'
body = ('Moikka {}!\r\n\r\nHienoa, että kilta kiinnostaa! Kaehmysi on vastaanotettu.\r\n'
'Mahdollisista kommenteista tulee ilmoitus sähköpostitse.\r\n\r\n'
'Käy katsomassa kaehmytilanne osoitteessa http://sika.sahkoinsinoorikilta.fi/kaehmy').format(name)
send_email(email, subject, body)
logging.debug('Sent kaehmy email to recipient <{}>'.format(email))
else:
context = {
'errors': form.errors
@@ -198,7 +225,17 @@ def kaehmy_comment(request, *args, **kwargs):
form = KaehmyCommentForm(request.POST)
if form.is_valid():
form.save()
comment = form.save()
email = comment.parent.email
name = comment.name
subject = 'Kaehmyysi tai kommenttiisi on vastattu!'
body = ('{} on vastannut kaehmyhakemukseesi tai kommenttiisi kaehmypalvelussa.\r\n\r\n'
'Käy lukemassa viesti osoitteessa http://sika.sahkoinsinoorikilta.fi/kaehmy').format(name.capitalize())
send_email(email, subject, body)
logging.debug('Sent kaehmy comment email to recipient <{}>'.format(email))
return redirect('/kaehmy')
else:
print(form)