Files
web2.0-backend/ohlhafv/views.py
T
2022-01-13 01:41:26 +02:00

72 lines
2.1 KiB
Python

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 HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.template.loader import render_to_string
from sikweb.settings import URL
from ohlhafv.models import OhlhafvChallenge
from ohlhafv.forms import OhlhafvForm
from webapp.utils import send_email
from webapp.models import processHooks
@require_http_methods(["GET"])
def ohlhafv_view(request, *args, **kwargs):
"""Render Ohlhafv form page."""
form = OhlhafvForm()
return render(request, 'ohlhafv:new.html', {'form': form})
@ensure_csrf_cookie
@require_http_methods(["POST"])
def ohlhafv_submit(request, *args, **kwargs):
"""Submit Ohlhafv form."""
form = OhlhafvForm(request.POST)
if form.is_valid():
form.save()
challenge = form.instance
email = form.cleaned_data.get('victim_email', '')
url = f'https://{URL}/ohlhafv/list'
subject = _('You have been challenged at Ohlhafv!')
email_body = render_to_string(
'ohlhafv:email.html', {
'challenge': challenge,
'url': url,
}
)
send_email(email, subject, email_body)
try:
webhook_message = render_to_string(
'ohlhafv:tgmsg.tpl', {
'challenge': challenge,
'url': url})
processHooks(message=webhook_message, eventType="ohlhafv")
except Exception:
pass
logging.debug(
'Sent ohlhafv email to recipient <{}>'.format(email))
else:
pass
return HttpResponseRedirect('/ohlhafv/list/')
@ensure_csrf_cookie
@require_http_methods(["GET"])
def ohlhafv_list(request, *args, **kwargs):
"""Present Ohlhafv challenges list."""
challenges = OhlhafvChallenge.objects.all()
challenges = challenges.order_by('-id')
context = {
'challenges': challenges,
'challenge_count': len(challenges),
}
return render(request, 'ohlhafv:list.html', context)