<s> """<STR_LIT>""" <EOL> import email . utils <EOL> import uuid <EOL> from django . conf import settings <EOL> from django . conf . urls import patterns , url <EOL> from django . contrib . auth import authenticate , login , get_user_model <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . mail import EmailMessage <EOL> from django . http import Http404 <EOL> from django . shortcuts import render , redirect <EOL> from django . template import Context , loader <EOL> from django . utils . translation import ugettext as _ <EOL> from . . utils import create_organization <EOL> from . . utils import model_field_attr <EOL> from . forms import UserRegistrationForm , OrganizationRegistrationForm <EOL> from . tokens import RegistrationTokenGenerator <EOL> class BaseBackend ( object ) : <EOL> """<STR_LIT>""" <EOL> org_model = None <EOL> def __init__ ( self , org_model = None , * args , ** kwargs ) : <EOL> self . user_model = get_user_model ( ) <EOL> self . org_model = org_model <EOL> def get_urls ( self ) : <EOL> raise NotImplementedError <EOL> def get_success_url ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_form ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise AttributeError ( _ ( "<STR_LIT>" ) ) <EOL> return self . form_class ( ** kwargs ) <EOL> def get_token ( self , user , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return RegistrationTokenGenerator ( ) . make_token ( user ) <EOL> def get_username ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( uuid . uuid4 ( ) ) [ : model_field_attr ( self . user_model , '<STR_LIT:username>' , '<STR_LIT:max_length>' ) ] <EOL> def activate_organizations ( self , user ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> relation_name = self . org_model ( ) . user_relation_name <EOL> except TypeError : <EOL> relation_name = "<STR_LIT>" <EOL> organization_set = getattr ( user , relation_name ) <EOL> for org in organization_set . filter ( is_active = False ) : <EOL> org . is_active = True <EOL> org . save ( ) <EOL> def activate_view ( self , request , user_id , token ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user = self . user_model . objects . get ( id = user_id , is_active = False ) <EOL> except self . user_model . DoesNotExist : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) ) <EOL> if not RegistrationTokenGenerator ( ) . check_token ( user , token ) : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) ) <EOL> form = self . get_form ( data = request . POST or None , instance = user ) <EOL> if form . is_valid ( ) : <EOL> form . instance . is_active = True <EOL> user = form . save ( ) <EOL> user . set_password ( form . cleaned_data [ '<STR_LIT:password>' ] ) <EOL> user . save ( ) <EOL> self . activate_organizations ( user ) <EOL> user = authenticate ( username = form . cleaned_data [ '<STR_LIT:username>' ] , <EOL> password = form . cleaned_data [ '<STR_LIT:password>' ] ) <EOL> login ( request , user ) <EOL> return redirect ( self . get_success_url ( ) ) <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : form } ) <EOL> def send_reminder ( self , user , sender = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if user . is_active : <EOL> return False <EOL> token = RegistrationTokenGenerator ( ) . make_token ( user ) <EOL> kwargs . update ( { '<STR_LIT>' : token } ) <EOL> self . _send_email ( user , self . reminder_subject , self . reminder_body , <EOL> sender , ** kwargs ) <EOL> def _send_email ( self , user , subject_template , body_template , <EOL> sender = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if sender : <EOL> from_email = "<STR_LIT>" % ( sender . first_name , sender . last_name , <EOL> email . utils . parseaddr ( settings . DEFAULT_FROM_EMAIL ) [ <NUM_LIT:1> ] ) <EOL> reply_to = "<STR_LIT>" % ( sender . first_name , sender . last_name , <EOL> sender . email ) <EOL> else : <EOL> from_email = settings . DEFAULT_FROM_EMAIL <EOL> reply_to = from_email <EOL> headers = { '<STR_LIT>' : reply_to } <EOL> kwargs . update ( { '<STR_LIT>' : sender , '<STR_LIT:user>' : user } ) <EOL> ctx = Context ( kwargs , autoescape = False ) <EOL> subject_template = loader . get_template ( subject_template ) <EOL> body_template = loader . get_template ( body_template ) <EOL> subject = subject_template . render ( ctx ) . strip ( ) <EOL> body = body_template . render ( ctx ) <EOL> return EmailMessage ( subject , body , from_email , [ user . email ] , <EOL> headers = headers ) . send ( ) <EOL> class RegistrationBackend ( BaseBackend ) : <EOL> """<STR_LIT>""" <EOL> activation_subject = '<STR_LIT>' <EOL> activation_body = '<STR_LIT>' <EOL> reminder_subject = '<STR_LIT>' <EOL> reminder_body = '<STR_LIT>' <EOL> form_class = UserRegistrationForm <EOL> def get_success_url ( self ) : <EOL> return reverse ( '<STR_LIT>' ) <EOL> def get_urls ( self ) : <EOL> return patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , view = self . success_view , <EOL> name = "<STR_LIT>" ) , <EOL> url ( r'<STR_LIT>' , <EOL> view = self . activate_view , name = "<STR_LIT>" ) , <EOL> url ( r'<STR_LIT>' , view = self . create_view , name = "<STR_LIT>" ) , <EOL> ) <EOL> def register_by_email ( self , email , sender = None , request = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user = self . user_model . objects . get ( email = email ) <EOL> except self . user_model . DoesNotExist : <EOL> user = self . user_model . objects . create ( username = self . get_username ( ) , <EOL> email = email , password = self . user_model . objects . make_random_password ( ) ) <EOL> user . is_active = False <EOL> user . save ( ) <EOL> self . send_activation ( user , sender , ** kwargs ) <EOL> return user <EOL> def send_activation ( self , user , sender = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if user . is_active : <EOL> return False <EOL> token = self . get_token ( user ) <EOL> kwargs . update ( { '<STR_LIT>' : token } ) <EOL> self . _send_email ( user , self . activation_subject , self . activation_body , <EOL> sender , ** kwargs ) <EOL> def create_view ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if request . user . is_authenticated ( ) : <EOL> return redirect ( "<STR_LIT>" ) <EOL> form = OrganizationRegistrationForm ( request . POST or None ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> user = self . user_model . objects . get ( email = form . cleaned_data [ '<STR_LIT:email>' ] ) <EOL> except self . user_model . DoesNotExist : <EOL> user = self . user_model . objects . create ( username = self . get_username ( ) , <EOL> email = form . cleaned_data [ '<STR_LIT:email>' ] , <EOL> password = self . user_model . objects . make_random_password ( ) ) <EOL> user . is_active = False <EOL> user . save ( ) <EOL> else : <EOL> return redirect ( "<STR_LIT>" ) <EOL> organization = create_organization ( user , form . cleaned_data [ '<STR_LIT:name>' ] , <EOL> form . cleaned_data [ '<STR_LIT>' ] , is_active = False ) <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT:user>' : user , '<STR_LIT>' : organization } ) <EOL> return render ( request , '<STR_LIT>' , <EOL> { '<STR_LIT>' : form } ) <EOL> def success_view ( self , request ) : <EOL> return render ( request , '<STR_LIT>' , { } ) <EOL> class InvitationBackend ( BaseBackend ) : <EOL> """<STR_LIT>""" <EOL> invitation_subject = '<STR_LIT>' <EOL> invitation_body = '<STR_LIT>' <EOL> reminder_subject = '<STR_LIT>' <EOL> reminder_body = '<STR_LIT>' <EOL> form_class = UserRegistrationForm <EOL> def get_success_url ( self ) : <EOL> return reverse ( '<STR_LIT>' ) <EOL> def get_urls ( self ) : <EOL> return patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , <EOL> view = self . activate_view , name = "<STR_LIT>" ) , <EOL> ) <EOL> def invite_by_email ( self , email , sender = None , request = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user = self . user_model . objects . get ( email = email ) <EOL> except self . user_model . DoesNotExist : <EOL> user = self . user_model . objects . create ( username = self . get_username ( ) , <EOL> email = email , password = self . user_model . objects . make_random_password ( ) ) <EOL> user . is_active = False <EOL> user . save ( ) <EOL> self . send_invitation ( user , sender , ** kwargs ) <EOL> return user <EOL> def send_invitation ( self , user , sender = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if user . is_active : <EOL> return False <EOL> token = self . get_token ( user ) <EOL> kwargs . update ( { '<STR_LIT>' : token } ) <EOL> self . _send_email ( user , self . invitation_subject , self . invitation_body , <EOL> sender , ** kwargs ) </s>
