287 lines
8.9 KiB
Python
287 lines
8.9 KiB
Python
"""Webapp views."""
|
|
|
|
from django.shortcuts import render, redirect
|
|
from django.contrib.auth import login, logout, authenticate
|
|
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
|
|
|
|
import logging
|
|
import requests
|
|
|
|
from webapp.models import PresetKaehmyRole, CustomKaehmyRole
|
|
from webapp.models import OhlhafvChallenge, KaehmyForm, TelegramChannel
|
|
from webapp.forms import OhlhafvForm, KaehmyForm_Form, KaehmyCommentForm
|
|
from webapp.tables import OhlhafvTable
|
|
|
|
from django.core.mail import send_mail
|
|
|
|
|
|
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):
|
|
"""Render main page."""
|
|
return render(request, "main_index.html", {})
|
|
|
|
|
|
@require_http_methods(["GET", "POST"])
|
|
@ensure_csrf_cookie
|
|
@login_required(login_url='/login')
|
|
@permission_required('members.change_member')
|
|
def admin_index(request, *args, **kwargs):
|
|
"""Render admin main page."""
|
|
return render(request, "admin_index.html", {})
|
|
|
|
|
|
@require_http_methods(["GET", "POST"])
|
|
def login_view(request, *args, **kwargs):
|
|
"""Render login view."""
|
|
if request.method == "POST":
|
|
uname = request.POST.get("username", None)
|
|
pw = request.POST.get("passwd", None)
|
|
user = authenticate(username=uname, password=pw)
|
|
if user is not None:
|
|
login(request, user)
|
|
original_site = request.GET.get("next", None) or "/"
|
|
return redirect(original_site)
|
|
return render(request,
|
|
"login.html",
|
|
{"error": "☹ Kirjautuminen kosahti. Yritä uudelleen!"})
|
|
|
|
# user got here by a get request
|
|
user = request.user
|
|
if user.is_authenticated():
|
|
# user shoud not be here authenticated with get but get rid if is
|
|
return redirect("/")
|
|
return render(request, "login.html", {})
|
|
|
|
|
|
@require_http_methods(["GET", "POST"])
|
|
def logout_view(request, *args, **kwargs):
|
|
"""Logout user and return to main page."""
|
|
logout(request)
|
|
return redirect("/")
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def about_view(request, *args, **kwargs):
|
|
"""Render about page."""
|
|
return render(request, "about.html", {})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def guild_view(request, *args, **kwargs):
|
|
"""Render "Guild" page."""
|
|
return render(request, "guild.html", {})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def freshmen_view(request, *args, **kwargs):
|
|
"""Render "Freshmen" page."""
|
|
return render(request, "freshmen.html", {})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def jobs_view(request, *args, **kwargs):
|
|
"""Render "Jobs" page."""
|
|
return render(request, "jobs.html", {})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def event_calendar_view(request, *args, **kwargs):
|
|
"""Render "Event calendar" page."""
|
|
return render(request, "event_calendar.html", {})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def international_view(request, *args, **kwargs):
|
|
"""Render "International" page."""
|
|
return render(request, "international.html", {})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def sosso_view(request, *args, **kwargs):
|
|
"""Render "Sössö" page."""
|
|
return render(request, "sosso.html", {})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def contact_view(request, *args, **kwargs):
|
|
"""Render "Contact" page."""
|
|
return render(request, "contact.html", {})
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def kaehmy_view(request, *args, **kwargs):
|
|
"""Render Kaehmy form page."""
|
|
form = KaehmyForm_Form()
|
|
return render(request, 'kaehmy.html', {'form': form})
|
|
|
|
|
|
@ensure_csrf_cookie
|
|
@require_http_methods(["POST"])
|
|
def kaehmy_submit(request, *args, **kwargs):
|
|
"""Submit Kaehmy form."""
|
|
form = KaehmyForm_Form(request.POST)
|
|
if form.is_valid():
|
|
application = form.save()
|
|
custom_name = form.cleaned_data.get('custom_role_name')
|
|
custom_is_board = form.cleaned_data.get('custom_role_is_board')
|
|
|
|
if len(custom_name) > 0:
|
|
custom_role = CustomKaehmyRole(
|
|
name=custom_name, is_board=custom_is_board)
|
|
custom_role.save()
|
|
application.custom_roles.add(custom_role)
|
|
|
|
url = 'https://sika.sahkoinsinoorikilta.fi/kaehmy'
|
|
|
|
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 {}').format(name, url)
|
|
|
|
send_email(email, subject, body)
|
|
logging.debug('Sent kaehmy email to recipient <{}>'.format(email))
|
|
|
|
CHAT_IDS = [channel.channel_id for channel in TelegramChannel.objects.all()]
|
|
for chat_id in CHAT_IDS:
|
|
tg_string = 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}'.format(
|
|
settings.TELEGRAM_BOT_TOKEN,
|
|
chat_id,
|
|
'Uusi kaehmy/New kaehmy! {} -> {}'.format(name, url)
|
|
)
|
|
response = requests.get(tg_string).json()
|
|
logging.debug('Telegram API response:\n{}'.format(response))
|
|
logging.debug('Sent kaehmy announcement to {} channels.'.format(len(CHAT_IDS)))
|
|
|
|
else:
|
|
context = {
|
|
'errors': form.errors
|
|
}
|
|
return render(request, 'error.html', context)
|
|
return HttpResponseRedirect('/kaehmy')
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def ohlhafv_view(request, *args, **kwargs):
|
|
"""Render Ohlhafv form page."""
|
|
form = OhlhafvForm()
|
|
return render(request, 'ohlhafv.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()
|
|
# return HttpResponseRedirect('/list/')
|
|
else:
|
|
pass
|
|
# return render(request, 'error.html', {'error': form.errors})
|
|
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()
|
|
|
|
table = OhlhafvTable(challenges,
|
|
request=request,
|
|
exclude=['id', 'challenger_email', 'victim_email'],
|
|
attrs={'class': 'table table-bordered table-hover'})
|
|
|
|
table.paginate(page=request.GET.get('page', 1), per_page=25)
|
|
table_html = table.as_html(request)
|
|
|
|
context = {
|
|
'table': table_html,
|
|
'challenge_count': len(challenges),
|
|
}
|
|
return render(request, 'ohlhafv_list.html', context)
|
|
|
|
|
|
@ensure_csrf_cookie
|
|
@require_http_methods(["GET"])
|
|
def kaehmy_list_view(request, *args, **kwargs):
|
|
"""Kaehmy application list"""
|
|
|
|
applications = KaehmyForm.objects.all()
|
|
|
|
context = {
|
|
'applications': applications,
|
|
'application_count': len(applications)
|
|
}
|
|
return render(request, 'kaehmy_list.html', context)
|
|
|
|
|
|
@ensure_csrf_cookie
|
|
@require_http_methods(["POST"])
|
|
def kaehmy_comment(request, *args, **kwargs):
|
|
"""POST endpoint for commenting"""
|
|
|
|
form = KaehmyCommentForm(request.POST)
|
|
if form.is_valid():
|
|
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)
|
|
context = {
|
|
'errors': form.errors
|
|
}
|
|
return render(request, 'error.html', context)
|
|
|
|
|
|
@require_http_methods(["GET"])
|
|
def kaehmy_statistics_view(request, *args, **kwargs):
|
|
"""Kaehmys roles listed by applications"""
|
|
|
|
applications = KaehmyForm.objects.all()
|
|
role_list = []
|
|
|
|
preset_roles = PresetKaehmyRole.objects.all()
|
|
custom_roles = CustomKaehmyRole.objects.all()
|
|
|
|
for preset in preset_roles:
|
|
role_list.append((preset.name, preset.forms.all().count()))
|
|
for custom in custom_roles:
|
|
role_list.append((custom.name, custom.forms.all().count()))
|
|
|
|
context = {
|
|
'applications': applications,
|
|
'application_count': len(applications),
|
|
'role_list': role_list
|
|
}
|
|
return render(request, 'kaehmy_statistics.html', context)
|