<s> from django . conf . urls import url <EOL> from django . contrib import admin <EOL> from django . contrib . admin . options import IS_POPUP_VAR <EOL> from django . contrib . admin . utils import unquote <EOL> from django . contrib . auth import update_session_auth_hash <EOL> from django . contrib . auth . forms import AdminPasswordChangeForm <EOL> from django . contrib . messages import success <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . http import ( <EOL> Http404 , HttpResponseRedirect ) <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . decorators import method_decorator <EOL> from django . utils . encoding import force_text <EOL> from django . utils . html import escape <EOL> from django . views . decorators . debug import sensitive_post_parameters <EOL> from . forms import ( <EOL> UserChangeForm , UserCreationForm ) <EOL> from . models import Profile , User <EOL> class ProfileAdminInline ( admin . StackedInline ) : <EOL> can_delete = False <EOL> model = Profile <EOL> exclude = ( '<STR_LIT>' , ) <EOL> def view_on_site ( self , obj ) : <EOL> return obj . get_absolute_url ( ) <EOL> @ admin . register ( User ) <EOL> class UserAdmin ( admin . ModelAdmin ) : <EOL> actions = [ '<STR_LIT>' ] <EOL> list_display = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:email>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> list_display_links = ( '<STR_LIT>' , '<STR_LIT:email>' ) <EOL> list_filter = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> list_select_related = ( '<STR_LIT>' , ) <EOL> ordering = ( '<STR_LIT:email>' , ) <EOL> search_fields = ( '<STR_LIT:email>' , ) <EOL> fieldsets = ( <EOL> ( None , { <EOL> '<STR_LIT>' : ( '<STR_LIT:email>' , '<STR_LIT:password>' ) } ) , <EOL> ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) } ) , <EOL> ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) } ) , <EOL> ) <EOL> filter_horizontal = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , ) <EOL> add_fieldsets = ( <EOL> ( None , { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT:email>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> } ) , <EOL> ) <EOL> add_form = UserCreationForm <EOL> form = UserChangeForm <EOL> change_password_form = AdminPasswordChangeForm <EOL> change_user_password_template = ( <EOL> '<STR_LIT>' ) <EOL> def get_date_joined ( self , user ) : <EOL> return user . profile . joined <EOL> get_date_joined . short_description = '<STR_LIT>' <EOL> get_date_joined . admin_order_field = ( <EOL> '<STR_LIT>' ) <EOL> def get_name ( self , user ) : <EOL> return user . profile . name <EOL> get_name . short_description = '<STR_LIT:Name>' <EOL> get_name . admin_order_field = '<STR_LIT>' <EOL> def get_fieldsets ( self , request , obj = None ) : <EOL> if not obj : <EOL> return self . add_fieldsets <EOL> return super ( ) . get_fieldsets ( request , obj ) <EOL> def get_form ( <EOL> self , request , obj = None , ** kwargs ) : <EOL> if obj is None : <EOL> kwargs [ '<STR_LIT>' ] = self . add_form <EOL> return super ( ) . get_form ( <EOL> request , obj , ** kwargs ) <EOL> def get_inline_instances ( <EOL> self , request , obj = None ) : <EOL> if obj is None : <EOL> return tuple ( ) <EOL> inline_instance = ProfileAdminInline ( <EOL> self . model , self . admin_site ) <EOL> return ( inline_instance , ) <EOL> def get_urls ( self ) : <EOL> password_change = [ <EOL> url ( r'<STR_LIT>' , <EOL> self . admin_site . admin_view ( <EOL> self . user_change_password ) , <EOL> name = '<STR_LIT>' ) , <EOL> ] <EOL> urls = super ( ) . get_urls ( ) <EOL> urls = password_change + urls <EOL> return urls <EOL> @ method_decorator ( sensitive_post_parameters ( ) ) <EOL> def user_change_password ( <EOL> self , request , user_id , form_url = '<STR_LIT>' ) : <EOL> if not self . has_change_permission ( <EOL> request ) : <EOL> raise PermissionDenied <EOL> user = self . get_object ( <EOL> request , unquote ( user_id ) ) <EOL> if user is None : <EOL> raise Http404 ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> name = force_text ( <EOL> self . model <EOL> . _meta . verbose_name ) , <EOL> key = escape ( user_id ) ) ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = self . change_password_form ( <EOL> user , request . POST ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> change_message = ( <EOL> self . construct_change_message ( <EOL> request , form , None ) ) <EOL> self . log_change ( <EOL> request , user , change_message ) <EOL> success ( <EOL> request , '<STR_LIT>' ) <EOL> update_session_auth_hash ( <EOL> request , form . user ) <EOL> return HttpResponseRedirect ( '<STR_LIT:..>' ) <EOL> else : <EOL> form = self . change_password_form ( user ) <EOL> context = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' . format ( <EOL> escape ( user . get_username ( ) ) ) , <EOL> '<STR_LIT>' : form_url , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : ( <EOL> IS_POPUP_VAR in request . POST <EOL> or IS_POPUP_VAR in request . GET ) , <EOL> '<STR_LIT>' : self . model . _meta , <EOL> '<STR_LIT>' : user , <EOL> } <EOL> context . update ( <EOL> admin . site . each_context ( request ) ) <EOL> request . current_app = self . admin_site . name <EOL> return TemplateResponse ( <EOL> request , <EOL> self . change_user_password_template , <EOL> context ) <EOL> def make_staff ( self , request , queryset ) : <EOL> rows_updated = queryset . update ( <EOL> is_staff = True ) <EOL> if rows_updated == <NUM_LIT:1> : <EOL> message = '<STR_LIT>' <EOL> else : <EOL> message = '<STR_LIT>' . format ( <EOL> rows_updated ) <EOL> message += '<STR_LIT>' <EOL> self . message_user ( request , message ) <EOL> make_staff . short_description = ( <EOL> '<STR_LIT>' ) </s>
