update send email uses

This commit is contained in:
Aarni Halinen
2022-01-13 21:53:55 +02:00
parent 6f316401f7
commit 7382c4e4bf
7 changed files with 263 additions and 252 deletions
+26 -34
View File
@@ -1,33 +1,26 @@
"""Ohlhafv views."""
from django.db.models import Count
from django.shortcuts import render, redirect
from django.contrib.auth import login, logout, authenticate
import logging
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import ensure_csrf_cookie
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import permission_required, login_required
from django.conf import settings
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.template.loader import render_to_string
import logging
import requests
from dealer.git import git
from sikweb.settings import URL
from ohlhafv.models import OhlhafvChallenge
from ohlhafv.forms import OhlhafvForm
from ohlhafv.tables import OhlhafvTable
from webapp.utils import send_email
from kaehmy.tgbot import TelegramBot
from webapp.utils import send_email
from sikweb.settings import URL
@require_http_methods(["GET"])
def ohlhafv_view(request, *args, **kwargs):
"""Render Ohlhafv form page."""
form = OhlhafvForm()
return render(request, 'ohlhafv:new.html', {'form': form})
return render(request, "ohlhafv:new.html", {"form": form})
@ensure_csrf_cookie
@@ -38,34 +31,33 @@ def ohlhafv_submit(request, *args, **kwargs):
if form.is_valid():
form.save()
challenge = form.instance
email = form.cleaned_data.get('victim_email', '')
url = f"https://{URL}/ohlhafv/list"
url = f'https://{URL}/ohlhafv/list'
subject = _('Sinut on haastettu Ohlhäfviin!')
message = render_to_string(
'ohlhafv:email.html', {
'challenge': challenge,
'url': url,
}
email_body = render_to_string(
"ohlhafv:email.html",
{
"challenge": challenge,
"url": url,
},
)
send_email(email, subject, message)
to_email = form.cleaned_data.get("victim_email", "")
subject = _("Sinut on haastettu Øhlhäfviin!")
send_email(to=to_email, subject=subject, body=email_body)
logging.debug(f"Sent ohlhafv email to recipient <{to_email}>")
try:
tg_message = render_to_string(
'ohlhafv:tgmsg.tpl', {
'challenge': challenge,
'url': url})
"ohlhafv:tgmsg.tpl", {"challenge": challenge, "url": url}
)
bot = TelegramBot()
bot.broadcast(tg_message)
except Exception: # tg spam is not critical. Ignore on failure
except Exception: # tg spam is not critical. Ignore on failure
pass
logging.debug(
'Sent ohlhafv email to recipient <{}>'.format(email))
else:
pass
return HttpResponseRedirect('/ohlhafv/list/')
return HttpResponseRedirect("/ohlhafv/list/")
@ensure_csrf_cookie
@@ -73,9 +65,9 @@ def ohlhafv_submit(request, *args, **kwargs):
def ohlhafv_list(request, *args, **kwargs):
"""Present Ohlhafv challenges list."""
challenges = OhlhafvChallenge.objects.all()
challenges = challenges.order_by('-id')
challenges = challenges.order_by("-id")
context = {
'challenges': challenges,
'challenge_count': len(challenges),
"challenges": challenges,
"challenge_count": len(challenges),
}
return render(request, 'ohlhafv:list.html', context)
return render(request, "ohlhafv:list.html", context)