<s> import bforms <EOL> from django . http import HttpResponseRedirect , HttpResponseForbidden <EOL> from helpers import * <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from django . conf import settings as settin <EOL> from django . contrib import auth <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth import views <EOL> import exceptions <EOL> from django . core . urlresolvers import reverse <EOL> import helpers <EOL> from django . core . mail import send_mail <EOL> from django . template . loader import render_to_string <EOL> from django . views . generic . base import View <EOL> from django . views . generic . edit import FormMixin <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . generic import TemplateView <EOL> from django . views . generic . edit import FormMixin <EOL> class FormCreateUser ( FormMixin , View ) : <EOL> form_class = bforms . UserCreationForm <EOL> template_name = '<STR_LIT>' <EOL> payload = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> success_url = '<STR_LIT:/>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> form = bforms . UserCreationForm ( ) <EOL> loginform = bforms . LoginForm ( ) <EOL> self . payload [ '<STR_LIT>' ] = form <EOL> self . payload [ '<STR_LIT>' ] = loginform <EOL> return render ( self . request , self . payload , '<STR_LIT>' ) <EOL> def form_valid ( self , form ) : <EOL> form . save ( ) <EOL> from django . contrib . auth import authenticate <EOL> user = authenticate ( username = form . cleaned_data [ '<STR_LIT:username>' ] , password = form . cleaned_data [ '<STR_LIT>' ] ) <EOL> login ( request , user ) <EOL> return super ( FormCreateUser , self ) . form_valid ( form ) <EOL> create_user = FormCreateUser . as_view ( ) <EOL> class UserManageView ( TemplateView , FormMixin ) : <EOL> "<STR_LIT>" <EOL> template_name = '<STR_LIT>' <EOL> password_form_class = bforms . PasswordChangeForm <EOL> def_topic_form_class = bforms . SetDefaultForm <EOL> @ method_decorator ( login_required ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( UserManageView , self ) . dispatch ( * args , ** kwargs ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> subs = SubscribedUser . objects . filter ( user = self . request . user ) . select_related ( ) <EOL> passwordchangeform = self . password_form_class ( request . user ) <EOL> invites = Invite . objects . filter ( user = request . user ) <EOL> def_topic_form = self . def_topic_form_class ( request . user ) <EOL> payload = dict ( subs = subs , form = passwordchangeform , invites = invites , def_topic_form = def_topic_form ) <EOL> return render ( self . request , payload , '<STR_LIT>' ) <EOL> def post ( self , request ) : <EOL> if request . POST . has_key ( '<STR_LIT>' ) : <EOL> topic_name = request . POST [ '<STR_LIT>' ] <EOL> topic = Topic . objects . get ( name = topic_name ) <EOL> sub = SubscribedUser . objects . get ( user = request . user , topic = topic ) <EOL> sub . delete ( ) <EOL> if request . POST . has_key ( '<STR_LIT>' ) : <EOL> passwordchangeform = self . password_form_class ( request . user , request . POST ) <EOL> if passwordchangeform . is_valid ( ) : <EOL> passwordchangeform . save ( ) <EOL> return self . form_valid ( passwordchangeform ) <EOL> else : <EOL> return self . form_invalid ( passwordchangeform ) <EOL> if request . POST . has_key ( '<STR_LIT>' ) : <EOL> def_topic_form = self . def_topic_form_class ( request . user ) <EOL> if def_topic_form . is_valid ( ) : <EOL> def_topic_form . save ( ) <EOL> return self . form_valid ( def_topic_form ) <EOL> else : <EOL> return self . form_invalid ( def_topic_form ) <EOL> def get_success_url ( self ) : <EOL> return reverse ( <EOL> '<STR_LIT>' , <EOL> ) <EOL> def form_valid ( self , form ) : <EOL> return super ( UserManageView , self ) . form_valid ( form ) <EOL> user_manage = UserManageView . as_view ( ) <EOL> def activate_user ( request , username ) : <EOL> user = User . objects . get ( username = username ) <EOL> try : <EOL> key = EmailActivationKey . objects . get ( user = user ) <EOL> except EmailActivationKey . DoesNotExist : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> request_key = request . GET . get ( '<STR_LIT:key>' , '<STR_LIT>' ) <EOL> if request_key == key . key : <EOL> profile = user . get_profile ( ) <EOL> profile . email_validated = True <EOL> profile . save ( ) <EOL> key . delete ( ) <EOL> payload = { } <EOL> return render ( request , payload , '<STR_LIT>' ) <EOL> else : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> class ResetPassword ( View ) : <EOL> payload = { '<STR_LIT>' : '<STR_LIT>' } <EOL> def get ( self , request ) : <EOL> form = bforms . PasswordResetForm ( ) <EOL> self . payload [ '<STR_LIT>' ] = form <EOL> return render ( request , self . payload , '<STR_LIT>' ) <EOL> def post ( self , request ) : <EOL> form = bforms . PasswordResetForm ( request . POST ) <EOL> self . payload [ '<STR_LIT>' ] = form <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> return render ( request , self . payload , '<STR_LIT>' ) <EOL> reset_password = ResetPassword . as_view <EOL> def reset_password_sent ( request ) : <EOL> payload = { } <EOL> return render ( request , payload , '<STR_LIT>' ) <EOL> class ResetPasswordDone ( View ) : <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request , username ) : <EOL> user = User . objects . get ( username = username ) <EOL> try : <EOL> key = PasswordResetKey . objects . get ( user = user ) <EOL> except PasswordResetKey . DoesNotExist : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> request_key = request . GET . get ( '<STR_LIT:key>' , '<STR_LIT>' ) <EOL> if request_key == key . key : <EOL> password = helpers . generate_random_key ( ) <EOL> user . set_password ( password ) <EOL> mail_text = render_to_string ( '<STR_LIT>' , dict ( user = user , password = password ) ) <EOL> send_mail ( '<STR_LIT>' , mail_text , '<STR_LIT>' , [ user . email ] ) <EOL> key . delete ( ) <EOL> payload = { } <EOL> return render ( request , payload , '<STR_LIT>' ) <EOL> else : <EOL> return HttpResponseForbidden ( '<STR_LIT>' ) <EOL> reset_password_done = ResetPasswordDone . as_view ( ) </s>
