<s> from django . contrib import messages <EOL> from django . contrib . auth import logout , login , authenticate <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth . models import User <EOL> from django . http import HttpResponseBadRequest , Http404 <EOL> from django . shortcuts import render , redirect , get_object_or_404 <EOL> from reddit . forms import UserForm , ProfileForm <EOL> from reddit . utils . helpers import post_only <EOL> from users . models import RedditUser <EOL> def user_profile ( request , username ) : <EOL> user = get_object_or_404 ( User , username = username ) <EOL> profile = RedditUser . objects . get ( user = user ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : profile } ) <EOL> @ login_required <EOL> def edit_profile ( request ) : <EOL> user = RedditUser . objects . get ( user = request . user ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> profile_form = ProfileForm ( instance = user ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> profile_form = ProfileForm ( request . POST , instance = user ) <EOL> if profile_form . is_valid ( ) : <EOL> profile = profile_form . save ( commit = False ) <EOL> profile . update_profile_data ( ) <EOL> profile . save ( ) <EOL> messages . success ( request , "<STR_LIT>" ) <EOL> else : <EOL> raise Http404 <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : profile_form } ) <EOL> def user_login ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . user . is_authenticated ( ) : <EOL> messages . warning ( request , "<STR_LIT>" ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> username = request . POST . get ( '<STR_LIT:username>' ) <EOL> password = request . POST . get ( '<STR_LIT:password>' ) <EOL> if not username or not password : <EOL> return HttpResponseBadRequest ( ) <EOL> user = authenticate ( username = username , <EOL> password = password ) <EOL> if user : <EOL> if user . is_active : <EOL> login ( request , user ) <EOL> redirect_url = request . POST . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> return redirect ( redirect_url ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : "<STR_LIT>" } ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : "<STR_LIT>" } ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> @ post_only <EOL> def user_logout ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . user . is_authenticated ( ) : <EOL> redirect_page = request . POST . get ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> logout ( request ) <EOL> messages . success ( request , '<STR_LIT>' ) <EOL> return redirect ( redirect_page ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> def register ( request ) : <EOL> """<STR_LIT>""" <EOL> user_form = UserForm ( ) <EOL> if request . user . is_authenticated ( ) : <EOL> messages . warning ( request , <EOL> '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : user_form } ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> user_form = UserForm ( request . POST ) <EOL> if user_form . is_valid ( ) : <EOL> user = user_form . save ( ) <EOL> user . set_password ( user . password ) <EOL> user . save ( ) <EOL> reddit_user = RedditUser ( ) <EOL> reddit_user . user = user <EOL> reddit_user . save ( ) <EOL> user = authenticate ( username = request . POST [ '<STR_LIT:username>' ] , <EOL> password = request . POST [ '<STR_LIT:password>' ] ) <EOL> login ( request , user ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : user_form } ) </s>
