oacpl/main/views.py

69 lines
3.6 KiB
Python
Raw Normal View History

2017-09-16 15:58:49 -04:00
from django.http import JsonResponse
2017-08-18 23:37:16 -04:00
from django.contrib import auth
from django.contrib.auth.models import Group, User, Permission
2017-09-15 15:54:05 -04:00
from django.core import mail
2017-09-18 00:05:16 -04:00
from django.db.models import Q
2017-09-16 15:58:49 -04:00
from django.shortcuts import render, redirect
2017-09-29 19:00:10 -04:00
from django.template.loader import render_to_string
2017-08-17 13:20:57 -04:00
2017-09-30 15:25:06 -04:00
from OACPL.utils import url_fix_render_to_string
2017-08-17 13:20:57 -04:00
from charter_members.models import Attorney
2017-09-15 12:41:20 -04:00
from newsletters.models import Subscriber
2017-09-15 15:54:05 -04:00
from OACPL import settings
2017-08-17 13:20:57 -04:00
def index(request):
attorneys = Attorney.objects.filter(front_page=True)
return render(request, 'index.html', {'attorneys': attorneys, 'contact': settings.EMAIL_CONTACT})
2017-08-18 23:37:16 -04:00
2017-09-16 15:58:49 -04:00
def contact(request):
name = request.POST.get('name')
email = request.POST.get('email')
subject = request.POST.get('subject')
body = request.POST.get('body')
result = False
if name is not None and email is not None and subject is not None and body is not None:
2017-09-29 16:21:21 -04:00
result = mail.send_mail('OACPL CONTACT: %(subject)s' % locals(), body, settings.EMAIL_HOST_USER, [settings.EMAIL_CONTACT],
2017-10-23 23:18:01 -04:00
html_message=url_fix_render_to_string('email.html', {'content': '<strong>Someone has messaged you via the website contact form!<br><br>Subject:</strong> %(subject)s<br><strong>From:</strong> %(name)s <%(email)s><br><br>%(body)s' % locals(), 'signature': ' '}))
2017-09-16 15:58:49 -04:00
return JsonResponse({'success': True if result else False})
2017-09-15 10:50:24 -04:00
def login(request):
if request.method == 'POST':
2017-09-15 12:41:20 -04:00
if request.POST.get('request') == 'login':
user = auth.authenticate(request, username=request.POST.get('username'), password=request.POST.get('password'))
if user:
auth.login(request, user)
return redirect('/')
else:
return render(request, 'login.html', {'navbar': False, 'footer': False, 'failed': True})
elif request.POST.get('request') == 'register':
2017-09-29 17:20:21 -04:00
user = User.objects.create_user(request.POST.get('username'), email=request.POST.get('email'), password=request.POST.get('password'))
2017-09-15 12:41:20 -04:00
user.save()
2018-01-06 13:42:37 -05:00
default_group = Group.objects.filter(name='default')
if default_group:
2018-01-06 13:42:37 -05:00
default_group[0].user_set.add(user)
2017-09-15 15:54:05 -04:00
if settings.EMAIL_HOST:
2017-09-30 12:47:47 -04:00
mail.send_mail('OACPL Registration', 'You have successfully registered to the Ontario Association of Child Protection Lawyers!', settings.EMAIL_HOST_USER, [request.POST.get('email')], html_message=render_to_string('email.html', {'content': 'You have successfully registered to the Ontario Association of Child Protection Lawyers!', 'name': user.username, 'base_url': settings.BASE_URL}))
2017-09-15 12:41:20 -04:00
if request.POST.get('newsletter'):
Subscriber.objects.create(email=request.POST.get('email'))
2017-09-18 00:05:16 -04:00
if request.POST.get('caselaw'):
perm = Permission.objects.get(codename='change_user')
admins = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm) | Q(is_superuser=True)).distinct().values_list('email', flat=True)
2017-09-29 19:00:10 -04:00
email = user.email
2018-01-06 13:42:37 -05:00
mail.send_mail('OACPL Case Law Request', '%(email)s has requested case law access.' % locals(), settings.EMAIL_HOST_USER, admins, html_message=render_to_string('email.html', {'content': '<a href="#">%(email)s</a> has requested case law access.', 'base_url': settings.BASE_URL}))
2017-09-15 12:41:20 -04:00
auth.login(request, user)
return redirect('/')
2017-09-15 10:50:24 -04:00
else:
2017-09-15 12:41:20 -04:00
return render(request, 'login.html', {'navbar': False, 'footer': False})
2017-09-15 10:50:24 -04:00
2017-08-18 23:37:16 -04:00
def logout(request):
auth.logout(request)
return redirect('/')