<s> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth . forms import PasswordChangeForm <EOL> from django . contrib . auth import authenticate , login <EOL> from django . shortcuts import render , get_object_or_404 <EOL> from accounts . forms import UserEditForm , UserConfirmDeletionForm , UserRegisterForm <EOL> from django . contrib import messages <EOL> from crispy_forms . helper import FormHelper <EOL> from crispy_forms . layout import Submit <EOL> from django . contrib . auth import logout as logout_user <EOL> from django . http import HttpResponseRedirect <EOL> from django . core . urlresolvers import reverse <EOL> from offers . models import Comment , Offer <EOL> from django . core . paginator import Paginator , EmptyPage , PageNotAnInteger <EOL> from . models import User <EOL> @ login_required <EOL> def self_profile ( request ) : <EOL> return render ( request , '<STR_LIT>' , { "<STR_LIT:user>" : request . user } ) <EOL> def profile ( request , username ) : <EOL> user = get_object_or_404 ( User , username = username ) <EOL> comments = user . comment_set . filter ( status = Comment . PUBLISHED , offer__status = Offer . PUBLISHED ) . order_by ( '<STR_LIT>' ) <EOL> paginator = Paginator ( comments , <NUM_LIT:5> ) <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> try : <EOL> comments = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> comments = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> comments = paginator . page ( paginator . num_pages ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> "<STR_LIT:user>" : user , <EOL> "<STR_LIT>" : comments , <EOL> } ) <EOL> @ login_required <EOL> def edit_account ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = UserEditForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> request . user . first_name = form . cleaned_data [ "<STR_LIT>" ] <EOL> request . user . last_name = form . cleaned_data [ "<STR_LIT>" ] <EOL> request . user . email = form . cleaned_data [ "<STR_LIT:email>" ] <EOL> request . user . save ( ) <EOL> messages . success ( request , '<STR_LIT>' ) <EOL> else : <EOL> messages . error ( request , '<STR_LIT>' ) <EOL> else : <EOL> form = UserEditForm ( { <EOL> "<STR_LIT>" : request . user . first_name , <EOL> "<STR_LIT>" : request . user . last_name , <EOL> "<STR_LIT:email>" : request . user . email , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , { "<STR_LIT>" : form } ) <EOL> @ login_required <EOL> def change_password ( request ) : <EOL> helper = FormHelper ( ) <EOL> helper . add_input ( Submit ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = PasswordChangeForm ( <EOL> request . user , <EOL> request . POST <EOL> ) <EOL> if form . is_valid ( ) : <EOL> messages . success ( request , "<STR_LIT>" ) <EOL> form . save ( ) <EOL> else : <EOL> form = PasswordChangeForm ( request . user ) <EOL> form . helper = helper <EOL> return render ( request , '<STR_LIT>' , { "<STR_LIT>" : form } ) <EOL> @ login_required <EOL> def deactivate_account ( request ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = UserConfirmDeletionForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> user = authenticate ( username = form . cleaned_data [ "<STR_LIT:username>" ] , password = form . cleaned_data [ "<STR_LIT:password>" ] ) <EOL> if user is None or user != request . user : <EOL> messages . error ( request , "<STR_LIT>" ) <EOL> else : <EOL> request . user . is_active = False <EOL> request . user . save ( ) <EOL> logout_user ( request ) <EOL> messages . success ( <EOL> request , <EOL> "<STR_LIT>" <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> form = UserConfirmDeletionForm ( ) <EOL> return render ( request , '<STR_LIT>' , { "<STR_LIT>" : form } ) <EOL> @ login_required <EOL> def comment_list ( request ) : <EOL> comments_list = Comment . visible . filter ( commenter = request . user ) . order_by ( '<STR_LIT>' ) <EOL> paginator = Paginator ( comments_list , <NUM_LIT:10> ) <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> try : <EOL> comments = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> comments = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> comments = paginator . page ( paginator . num_pages ) <EOL> return render ( request , '<STR_LIT>' , { "<STR_LIT>" : comments } ) <EOL> @ login_required <EOL> def followed_list ( request ) : <EOL> offer_list = Offer . visible_offers . filter ( followers = request . user ) <EOL> paginator = Paginator ( offer_list , <NUM_LIT:5> ) <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> try : <EOL> offers = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> offers = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> offers = paginator . page ( paginator . num_pages ) <EOL> return render ( request , '<STR_LIT>' , { "<STR_LIT>" : offers } ) <EOL> def register ( request ) : <EOL> if request . user . is_authenticated ( ) : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> user = User ( is_staff = False , is_superuser = False ) <EOL> form = UserRegisterForm ( request . POST , instance = user ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> user = authenticate ( username = form . cleaned_data [ "<STR_LIT:username>" ] , password = form . cleaned_data [ "<STR_LIT>" ] ) <EOL> login ( request , user ) <EOL> messages . success ( request , "<STR_LIT>" ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> else : <EOL> messages . error ( request , "<STR_LIT>" ) <EOL> else : <EOL> form = UserRegisterForm ( ) <EOL> return render ( request , '<STR_LIT>' , { "<STR_LIT>" : form } ) </s>
