<s> from django . contrib import messages <EOL> from django . contrib . auth import login as login_user , logout as logout_user <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth . forms import AuthenticationForm , SetPasswordForm <EOL> from django . core . mail import send_mail <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import transaction <EOL> from django . db . models import Q <EOL> from django . shortcuts import get_object_or_404 , redirect , render <EOL> from django . template . loader import render_to_string <EOL> from twobuntu . accounts . forms import ProfileForm , RegistrationForm , ResetForm <EOL> from twobuntu . accounts . models import ConfirmationKey , Profile <EOL> from twobuntu . articles . models import Article <EOL> from twobuntu . decorators import canonical <EOL> @ canonical ( Profile ) <EOL> def profile ( request , profile ) : <EOL> """<STR_LIT>""" <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : profile , <EOL> '<STR_LIT>' : profile , <EOL> '<STR_LIT>' : Article . apply_filter ( request , Q ( author = profile . user ) ) , <EOL> } ) <EOL> def login ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = AuthenticationForm ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> login_user ( request , form . get_user ( ) ) <EOL> return redirect ( request . GET [ '<STR_LIT>' ] if '<STR_LIT>' in request . GET else '<STR_LIT>' ) <EOL> else : <EOL> form = AuthenticationForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> } ) <EOL> @ login_required <EOL> def logout ( request ) : <EOL> """<STR_LIT>""" <EOL> logout_user ( request ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> @ transaction . atomic <EOL> def register ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = RegistrationForm ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> user = form . save ( commit = False ) <EOL> user . email = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> user . is_active = False <EOL> user . save ( ) <EOL> key = ConfirmationKey ( user = user ) <EOL> key . save ( ) <EOL> template = render_to_string ( '<STR_LIT>' , { <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT:url>' : request . build_absolute_uri ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:key>' : key . key } ) ) , <EOL> } ) <EOL> send_mail ( '<STR_LIT>' , template , '<STR_LIT>' , [ user . email ] ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = RegistrationForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> } ) <EOL> @ transaction . atomic <EOL> def register_confirm ( request , key ) : <EOL> """<STR_LIT>""" <EOL> key = get_object_or_404 ( ConfirmationKey , key = key ) <EOL> key . user . is_active = True <EOL> key . user . save ( ) <EOL> key . delete ( ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> def reset ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = ResetForm ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> key = ConfirmationKey ( user = form . user ) <EOL> key . save ( ) <EOL> template = render_to_string ( '<STR_LIT>' , { <EOL> '<STR_LIT:user>' : key . user , <EOL> '<STR_LIT:url>' : request . build_absolute_uri ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:key>' : key . key } ) ) , <EOL> } ) <EOL> send_mail ( '<STR_LIT>' , template , '<STR_LIT>' , [ key . user . email ] ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = ResetForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> } ) <EOL> @ transaction . atomic <EOL> def reset_confirm ( request , key ) : <EOL> """<STR_LIT>""" <EOL> key = get_object_or_404 ( ConfirmationKey , key = key ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = SetPasswordForm ( user = key . user , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> key . delete ( ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = SetPasswordForm ( user = key . user ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> } ) <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def edit ( request , id ) : <EOL> """<STR_LIT>""" <EOL> profile = get_object_or_404 ( Profile , user = id ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = ProfileForm ( instance = profile , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> profile . user . first_name = form . cleaned_data [ '<STR_LIT>' ] <EOL> profile . user . last_name = form . cleaned_data [ '<STR_LIT>' ] <EOL> profile . user . email = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> profile . user . save ( ) <EOL> form . save ( ) <EOL> return redirect ( profile ) <EOL> else : <EOL> form = ProfileForm ( <EOL> instance = profile , <EOL> initial = { <EOL> '<STR_LIT>' : profile . user . first_name , <EOL> '<STR_LIT>' : profile . user . last_name , <EOL> '<STR_LIT:email>' : profile . user . email , <EOL> } ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> } ) </s>
