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
2017-09-15 15:54:05 -04:00
from django . core import mail
2017-09-16 15:58:49 -04:00
from django . shortcuts import render , redirect
2017-08-17 13:20:57 -04:00
2018-01-25 00:18:13 -05:00
from charter_members . forms import RegisterForm
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 15:54:05 -04:00
from OACPL import settings
2017-08-17 13:20:57 -04:00
def index ( request ) :
2018-01-14 21:10:47 -05:00
attorneys = Attorney . objects . filter ( front_page = True ) . order_by ( ' order ' )
2018-01-23 21:10:16 -05:00
return render ( request , ' index.html ' , { ' attorneys ' : attorneys , ' contact ' : settings . EMAIL_CONTACT , ' youtube ' : settings . YOUTUBE_CONFERENCE } )
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 ' :
2018-01-25 00:18:13 -05:00
if request . POST . get ( ' request ' ) == ' register ' :
register_form = RegisterForm ( request . POST , request . FILES )
if register_form . is_valid ( ) :
user = register_form . save ( )
auth . login ( request , user )
return redirect ( ' / ' )
elif request . POST . get ( ' request ' ) == ' login ' :
2017-09-15 12:41:20 -04:00
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 } )
2017-10-24 00:33:19 -04:00
2018-01-25 00:18:13 -05:00
if ' register_form ' not in vars ( ) :
register_form = RegisterForm ( )
return render ( request , ' login.html ' , { ' navbar ' : False , ' footer ' : False , ' register ' : register_form } )
2017-09-15 10:50:24 -04:00
2017-08-18 23:37:16 -04:00
def logout ( request ) :
auth . logout ( request )
return redirect ( ' / ' )