Move and rename a lot of stuff into kaehmy app
This commit is contained in:
+175
@@ -0,0 +1,175 @@
|
||||
from django.db.models import Count
|
||||
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
|
||||
from django.core.mail import send_mail
|
||||
|
||||
import logging
|
||||
import requests
|
||||
from dealer.git import git
|
||||
|
||||
from members.views.utils import *
|
||||
from kaehmy.models import Application, CustomRole, PresetRole
|
||||
from kaehmy.forms import ApplicationForm
|
||||
|
||||
|
||||
@ensure_csrf_cookie
|
||||
@require_http_methods(["GET"])
|
||||
def list_view(request, *args, **kwargs):
|
||||
"""Kaehmy application list"""
|
||||
|
||||
role_filter = request.GET.get('role', None)
|
||||
if role_filter is not None and str(role_filter) != '-1':
|
||||
applications = Application.objects.filter(custom_roles__id=role_filter) | Application.objects.filter(preset_roles__id=role_filter)
|
||||
else:
|
||||
applications = Application.objects.all()
|
||||
|
||||
applications = applications.order_by('-timestamp')
|
||||
filter_options_preset = PresetRole.objects.annotate(form_count=Count('forms')).filter(form_count__gt=0)
|
||||
filter_options_preset_list = [(r.id, r.name, r.form_count) for r in filter_options_preset]
|
||||
filter_options_custom = CustomRole.objects.annotate(form_count=Count('forms')).filter(form_count__gt=0)
|
||||
filter_options_custom_list = [(r.id, r.name, r.form_count) for r in filter_options_custom]
|
||||
|
||||
filter_options = filter_options_preset_list + filter_options_custom_list
|
||||
filter_options.sort(key=lambda f: f[1])
|
||||
|
||||
context = {
|
||||
'applications': applications,
|
||||
'application_count': len(applications),
|
||||
'filter_options': filter_options
|
||||
}
|
||||
return render(request, 'list.html', context)
|
||||
|
||||
|
||||
@ensure_csrf_cookie
|
||||
@require_http_methods(["POST"])
|
||||
def 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 = {
|
||||
'error': form.errors
|
||||
}
|
||||
return render(request, 'error.html', context)
|
||||
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
def statistics_view(request, *args, **kwargs):
|
||||
"""Kaehmys roles listed by applications"""
|
||||
|
||||
applications = Application.objects.all()
|
||||
role_list = []
|
||||
|
||||
preset_roles = PresetRole.objects.all()
|
||||
custom_roles = CustomRole.objects.all()
|
||||
|
||||
for preset in preset_roles:
|
||||
people = [form.name for form in preset.forms.all()]
|
||||
role_list.append((preset.name, len(people), ', '.join(people)))
|
||||
for custom in custom_roles:
|
||||
people = [form.name for form in custom.forms.all()]
|
||||
role_list.append((custom.name, len(people), ', '.join(people)))
|
||||
|
||||
context = {
|
||||
'applications': applications,
|
||||
'application_count': len(applications),
|
||||
'role_list': role_list
|
||||
}
|
||||
return render(request, 'statistics.html', context)
|
||||
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
def view(request, *args, **kwargs):
|
||||
"""Render Kaehmy form page."""
|
||||
form = ApplicationForm()
|
||||
return render(request, 'kaehmy.html', {'form': form})
|
||||
|
||||
|
||||
@ensure_csrf_cookie
|
||||
@require_http_methods(["POST"])
|
||||
def submit(request, *args, **kwargs):
|
||||
"""Submit Kaehmy form."""
|
||||
form = ApplicationForm(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 = CustomRole(
|
||||
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 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 = {
|
||||
'error': form.errors
|
||||
}
|
||||
return render(request, 'error.html', context)
|
||||
return HttpResponseRedirect('/kaehmy')
|
||||
|
||||
|
||||
@require_http_methods(['GET'])
|
||||
def export_view(request, *args, **kwargs):
|
||||
def make_table(queryset):
|
||||
table = KaehmyExportTable(queryset,
|
||||
request=request,
|
||||
exclude=['id'],
|
||||
attrs={'class': 'table table-bordered table-hover'})
|
||||
|
||||
table.paginate(page=request.GET.get('page', 1), per_page=9999)
|
||||
table_html = convert_table_to_html(table, request)
|
||||
return table_html
|
||||
|
||||
kaehmys = Application.objects.all()
|
||||
non_board = filter(lambda q: not q.has_any_board_role(), kaehmys)
|
||||
board = filter(lambda q: q.has_any_board_role(), kaehmys)
|
||||
|
||||
context = {
|
||||
'non_board_table': make_table(non_board),
|
||||
'board_table': make_table(board),
|
||||
}
|
||||
return render(request, 'export.html', context)
|
||||
Reference in New Issue
Block a user