Files
web2.0-backend/members/views/applications.py
T
2017-09-25 20:45:13 +03:00

143 lines
4.9 KiB
Python

from django.shortcuts import render
from django.contrib.auth.decorators import permission_required
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.core.mail import send_mail
from django.conf import settings
from django.utils.translation import ugettext as _
from django.forms.models import model_to_dict
import logging
import html
from members.views.utils import *
from members.tables import RequestTable
from members.forms import ApplicationForm
@ensure_csrf_cookie
@require_http_methods(["GET"])
@permission_required('members.change_member', login_url='/login')
def application_list(request, *args, **kwargs):
"""List member applications not yet processed."""
applications = Request.objects.all()
application_count = len(applications)
table = RequestTable(applications,
request=request,
exclude=['id'],
attrs={'class': 'table table-bordered table-hover'})
table.paginate(page=request.GET.get('page', 1), per_page=25)
table_html = convert_table_to_html(table, request)
context = {
'table': table_html,
'application_count': application_count,
'notification': request.GET.get('notification', None)
}
return render(request, 'application_list.html', context)
@ensure_csrf_cookie
@require_http_methods(["GET"])
@permission_required('members.change_member', login_url='/login')
def application_edit(request, *args, **kwargs):
"""Edit member request information."""
i = kwargs.pop('index', None)
if i is None:
return render(
request, 'error.html', {'error': _('No application id specified')})
else:
application = Request.objects.get(id=i)
form = ApplicationForm(instance=application)
return render(
request,
'application_edit.html',
{'application_id': i, 'form': form})
@ensure_csrf_cookie
@require_http_methods(["POST"])
@permission_required('members.change_member', login_url='/login')
def application_accept(request, *args, **kwargs):
"""Accept application."""
form = ApplicationForm(request.POST)
if form.is_valid():
id = request.POST['id']
application = Request.objects.get(id=id)
member = application.to_member()
member.save()
application.delete()
logging.info(
"Accepted application in member "
"register with the following info: {}"
.format(form))
notification = "{} {}.".format(_("Successfully accepted application"),
str(application))
return HttpResponseRedirect(
'/members/list?notification={}'.format(html.escape(notification)))
else:
return render(request,
'error.html',
{'error': _('Could not accept application object')})
@ensure_csrf_cookie
@require_http_methods(["POST"])
@permission_required('members.change_member', login_url='/login')
def application_delete(request, *args, **kwargs):
"""Delete member application."""
try:
id = request.POST['id']
except KeyError:
return render(
request, 'error.html', {'error': _('No application id specified')})
try:
application = Request.objects.get(id=id)
notification = "{} {}.".format(_("Successfully deleted application"),
str(application))
application.delete()
logging.info(
"Delete application in member register with the following id: {}"
.format(id))
return HttpResponseRedirect(
'/members/applications?notification={}'
.format(html.escape(notification)))
except:
return render(request,
'error.html',
{'error': _('Could not delete application object')})
@ensure_csrf_cookie
@require_http_methods(["GET"])
@permission_required('members.change_member', login_url='/login')
def application_delete_confirm(request, *args, **kwargs):
"""Confirm application deletion."""
i = kwargs.pop('index', None)
if i is None:
return render(request,
'error.html',
{'error': _('No application id specified')})
else:
application = Request.objects.get(id=i)
form = ApplicationForm(instance=application)
return render(request,
'application_delete_confirm.html',
{'application_id': i, 'form': form})
@ensure_csrf_cookie
def application_form(request, *args, **kwargs):
"""Render member application form."""
return render(request, 'application_index.html', {})
@ensure_csrf_cookie
def application_form_success(request, *args, **kwargs):
"""Render application Successfully sent page."""
return render(request, 'application_success.html', {})