2018-01-25 14:49:47 -05:00
import random
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
2018-01-25 14:49:47 -05:00
from django . contrib . auth . models import User
2017-08-17 13:20:57 -04:00
2018-01-25 14:49:47 -05:00
from OACPL . utils import render_to_string
from . models import ResetToken
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
2018-01-31 18:55:36 -05:00
from variables . models import Variable
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-31 18:55:36 -05:00
banner = Variable . objects . get ( key = ' banner ' )
popup_header = Variable . objects . get ( key = ' popup_header ' )
popup_body = Variable . objects . get ( key = ' popup_body ' )
objectives = Variable . objects . get ( key = ' objectives ' )
return render ( request , ' index.html ' , { ' attorneys ' : attorneys , ' contact ' : settings . EMAIL_CONTACT , ' youtube ' : settings . YOUTUBE_CONFERENCE , ' banner ' : banner , ' objectives ' : objectives , ' popup_header ' : popup_header , ' popup_body ' : popup_body } )
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 ) :
2018-01-31 18:55:36 -05:00
terms = Variable . objects . get ( key = ' terms ' )
2017-09-15 10:50:24 -04:00
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 ( )
2018-01-31 18:55:36 -05:00
return render ( request , ' login.html ' , { ' navbar ' : False , ' footer ' : False , ' register ' : register_form , ' terms ' : terms } )
2017-09-15 10:50:24 -04:00
2018-01-25 14:49:47 -05:00
def reset ( request ) :
if request . method == ' POST ' :
if request . POST . get ( ' password1 ' ) == request . POST . get ( ' password2 ' ) :
reset_req = ResetToken . objects . filter ( token = request . POST . get ( ' token ' ) ) . first ( )
reset_req . user . set_password ( request . POST . get ( ' password1 ' ) )
reset_req . user . save ( )
auth . login ( request , reset_req . user )
reset_req . delete ( )
return redirect ( ' / ' )
return render ( request , ' reset.html ' , { ' navbar ' : False , ' footer ' : False , ' token ' : request . GET . get ( ' token ' ) } )
def reset_token ( request ) :
user = User . objects . filter ( email = request . POST . get ( ' email ' ) ) . first ( )
if user :
token = ' ' . join ( [ chr ( random . randrange ( 97 , 122 ) ) for i in range ( 8 ) ] )
reset = ResetToken . objects . create ( token = token , user = user )
reset . save ( )
2018-01-25 14:51:35 -05:00
mail . send_mail ( ' OACPL Password Recovery ' , ' To reset your password navigate to https://oacpl.org/reset and enter code: ' + token , settings . EMAIL_HOST_USER , [ user . email ] , html_message = render_to_string ( ' email.html ' , { ' content ' : ' To reset your password click <a href= " /reset?token= {} " >here</a>.<br><br>If the link does not work, please navigate to {} /reset and enter the following code: {} ' . format ( token , settings . BASE_URL , token ) , ' name ' : user . first_name + ' ' + user . last_name , ' base_url ' : settings . BASE_URL } ) )
2018-01-25 14:49:47 -05:00
return JsonResponse ( { ' success ' : True } )
2017-08-18 23:37:16 -04:00
def logout ( request ) :
auth . logout ( request )
return redirect ( ' / ' )