<s> from django . conf import settings <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . sites . shortcuts import get_current_site <EOL> from django . forms import modelformset_factory <EOL> from django . shortcuts import render <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . generic import View <EOL> from registration . models import RegistrationProfile <EOL> from registration . views import RegistrationView <EOL> from orchestra . accounts . bitformfield import BitFormField <EOL> from orchestra . accounts . forms import CommunicationPreferenceForm <EOL> from orchestra . accounts . forms import UserForm <EOL> from orchestra . accounts . forms import WorkerForm <EOL> from orchestra . accounts import signals <EOL> from orchestra . models import CommunicationPreference <EOL> from orchestra . models import Worker <EOL> UserModel = get_user_model ( ) <EOL> class OrchestraRegistrationView ( RegistrationView ) : <EOL> SEND_ACTIVATION_EMAIL = getattr ( settings , '<STR_LIT>' , True ) <EOL> success_url = '<STR_LIT>' <EOL> def register ( self , form ) : <EOL> """<STR_LIT>""" <EOL> site = get_current_site ( self . request ) <EOL> if hasattr ( form , '<STR_LIT>' ) : <EOL> new_user_instance = form . save ( ) <EOL> else : <EOL> new_user_instance = ( UserModel ( ) . objects <EOL> . create_user ( ** form . cleaned_data ) ) <EOL> new_user = RegistrationProfile . objects . create_inactive_user ( <EOL> new_user = new_user_instance , <EOL> site = site , <EOL> send_email = self . SEND_ACTIVATION_EMAIL , <EOL> request = self . request , <EOL> ) <EOL> signals . orchestra_user_registered . send ( sender = self . __class__ , <EOL> user = new_user , <EOL> request = self . request ) <EOL> return new_user <EOL> @ method_decorator ( login_required , name = '<STR_LIT>' ) <EOL> class WorkerViewMixin ( View ) : <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . set_context_data ( request , * args , ** kwargs ) <EOL> return super ( ) . dispatch ( request , * args , ** kwargs ) <EOL> def set_context_data ( self , request , * args , ** kwargs ) : <EOL> self . worker = Worker . objects . get ( user = request . user ) <EOL> class AccountSettingsView ( WorkerViewMixin ) : <EOL> template_name = '<STR_LIT>' <EOL> user_form = UserForm <EOL> worker_form = WorkerForm <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> user_form = self . user_form ( <EOL> instance = request . user ) <EOL> worker_form = self . worker_form ( instance = self . worker ) <EOL> return render ( request , self . template_name , { <EOL> '<STR_LIT>' : user_form , <EOL> '<STR_LIT>' : worker_form , <EOL> } ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> user_form = self . user_form ( <EOL> data = request . POST , instance = request . user ) <EOL> worker_form = self . worker_form ( <EOL> data = request . POST , instance = self . worker ) <EOL> success = user_form . is_valid ( ) and worker_form . is_valid ( ) <EOL> if success : <EOL> user_form . save ( ) <EOL> worker_form . save ( ) <EOL> return render ( request , self . template_name , { <EOL> '<STR_LIT>' : user_form , <EOL> '<STR_LIT>' : worker_form , <EOL> '<STR_LIT:success>' : success , <EOL> } ) <EOL> class CommunicationPreferenceSettingsView ( WorkerViewMixin ) : <EOL> template_name = '<STR_LIT>' <EOL> comm_pref_form = CommunicationPreferenceForm <EOL> default_choices = CommunicationPreference . COMMUNICATION_METHODS <EOL> method_choices = { <EOL> CommunicationPreference . CommunicationType . TASK_STATUS_CHANGE . value : <EOL> ( ( CommunicationPreference . CommunicationMethods . EMAIL , '<STR_LIT>' ) , ) , <EOL> } <EOL> def set_context_data ( self , request , * args , ** kwargs ) : <EOL> super ( ) . set_context_data ( request , * args , ** kwargs ) <EOL> self . comm_prefs = CommunicationPreference . objects . filter ( <EOL> worker = self . worker ) <EOL> self . CommunicationPreferenceFormSet = modelformset_factory ( <EOL> CommunicationPreference , <EOL> form = CommunicationPreferenceForm , <EOL> extra = <NUM_LIT:0> <EOL> ) <EOL> self . descriptions = [ comm_pref . get_descriptions ( ) <EOL> for comm_pref in self . comm_prefs ] <EOL> def set_method_choices ( self , formset ) : <EOL> for form in formset : <EOL> comm_type = form . initial . get ( '<STR_LIT>' ) <EOL> choices = self . method_choices . get ( comm_type , <EOL> self . default_choices ) <EOL> form . fields [ '<STR_LIT>' ] = BitFormField ( choices = self . default_choices , <EOL> widget_choices = choices ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> comm_pref_formset = self . CommunicationPreferenceFormSet ( <EOL> queryset = self . comm_prefs ) <EOL> self . set_method_choices ( comm_pref_formset ) <EOL> return render ( request , self . template_name , { <EOL> '<STR_LIT>' : zip ( comm_pref_formset , self . descriptions ) , <EOL> '<STR_LIT>' : comm_pref_formset , <EOL> } ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> comm_pref_formset = self . CommunicationPreferenceFormSet ( <EOL> data = request . POST ) <EOL> self . set_method_choices ( comm_pref_formset ) <EOL> success = comm_pref_formset . is_valid ( ) <EOL> if success : <EOL> comm_pref_formset . save ( ) <EOL> return render ( request , self . template_name , { <EOL> '<STR_LIT>' : zip ( comm_pref_formset , self . descriptions ) , <EOL> '<STR_LIT>' : comm_pref_formset , <EOL> '<STR_LIT:success>' : success , <EOL> } ) </s>
