<s> import copy <EOL> from django import forms <EOL> from django . db import models <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . forms . models import modelform_factory <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . encoding import force_unicode <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ , ugettext_lazy <EOL> from xadmin . layout import FormHelper , Layout , Fieldset , Container , Col <EOL> from xadmin . plugins . actions import BaseActionView , ACTION_CHECKBOX_NAME <EOL> from xadmin . util import model_ngettext , vendor <EOL> from xadmin . views . base import filter_hook <EOL> from xadmin . views . edit import ModelFormAdminView <EOL> BATCH_CHECKBOX_NAME = '<STR_LIT>' <EOL> class ChangeFieldWidgetWrapper ( forms . Widget ) : <EOL> def __init__ ( self , widget ) : <EOL> self . is_hidden = widget . is_hidden <EOL> self . needs_multipart_form = widget . needs_multipart_form <EOL> self . attrs = widget . attrs <EOL> self . widget = widget <EOL> def __deepcopy__ ( self , memo ) : <EOL> obj = copy . copy ( self ) <EOL> obj . widget = copy . deepcopy ( self . widget , memo ) <EOL> obj . attrs = self . widget . attrs <EOL> memo [ id ( self ) ] = obj <EOL> return obj <EOL> @ property <EOL> def media ( self ) : <EOL> media = self . widget . media + vendor ( '<STR_LIT>' ) <EOL> return media <EOL> def render ( self , name , value , attrs = None ) : <EOL> output = [ ] <EOL> is_required = self . widget . is_required <EOL> output . append ( u'<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( BATCH_CHECKBOX_NAME , name , ( is_required and '<STR_LIT>' or '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) ) <EOL> output . extend ( [ ( '<STR_LIT>' % <EOL> ( ( not is_required and '<STR_LIT>' or '<STR_LIT>' ) , name ) ) , <EOL> self . widget . render ( name , value , attrs ) , '<STR_LIT>' ] ) <EOL> return mark_safe ( u'<STR_LIT>' . join ( output ) ) <EOL> def build_attrs ( self , extra_attrs = None , ** kwargs ) : <EOL> "<STR_LIT>" <EOL> self . attrs = self . widget . build_attrs ( extra_attrs = None , ** kwargs ) <EOL> return self . attrs <EOL> def value_from_datadict ( self , data , files , name ) : <EOL> return self . widget . value_from_datadict ( data , files , name ) <EOL> def id_for_label ( self , id_ ) : <EOL> return self . widget . id_for_label ( id_ ) <EOL> class BatchChangeAction ( BaseActionView ) : <EOL> action_name = "<STR_LIT>" <EOL> description = ugettext_lazy ( <EOL> u'<STR_LIT>' ) <EOL> batch_change_form_template = None <EOL> model_perm = '<STR_LIT>' <EOL> batch_fields = [ ] <EOL> def change_models ( self , queryset , cleaned_data ) : <EOL> n = queryset . count ( ) <EOL> data = { } <EOL> for f in self . opts . fields : <EOL> if not f . editable or isinstance ( f , models . AutoField ) or not f . name in cleaned_data : <EOL> continue <EOL> data [ f ] = cleaned_data [ f . name ] <EOL> if n : <EOL> for obj in queryset : <EOL> for f , v in data . items ( ) : <EOL> f . save_form_data ( obj , v ) <EOL> obj . save ( ) <EOL> self . message_user ( _ ( "<STR_LIT>" ) % { <EOL> "<STR_LIT:count>" : n , "<STR_LIT>" : model_ngettext ( self . opts , n ) <EOL> } , '<STR_LIT:success>' ) <EOL> def get_change_form ( self , is_post , fields ) : <EOL> edit_view = self . get_model_view ( ModelFormAdminView , self . model ) <EOL> def formfield_for_dbfield ( db_field , ** kwargs ) : <EOL> formfield = edit_view . formfield_for_dbfield ( db_field , required = is_post , ** kwargs ) <EOL> formfield . widget = ChangeFieldWidgetWrapper ( formfield . widget ) <EOL> return formfield <EOL> defaults = { <EOL> "<STR_LIT>" : edit_view . form , <EOL> "<STR_LIT>" : fields , <EOL> "<STR_LIT>" : formfield_for_dbfield , <EOL> } <EOL> return modelform_factory ( self . model , ** defaults ) <EOL> def do_action ( self , queryset ) : <EOL> if not self . has_change_permission ( ) : <EOL> raise PermissionDenied <EOL> change_fields = [ f for f in self . request . POST . getlist ( BATCH_CHECKBOX_NAME ) if f in self . batch_fields ] <EOL> if change_fields and self . request . POST . get ( '<STR_LIT>' ) : <EOL> self . form_obj = self . get_change_form ( True , change_fields ) ( <EOL> data = self . request . POST , files = self . request . FILES ) <EOL> if self . form_obj . is_valid ( ) : <EOL> self . change_models ( queryset , self . form_obj . cleaned_data ) <EOL> return None <EOL> else : <EOL> self . form_obj = self . get_change_form ( False , self . batch_fields ) ( ) <EOL> helper = FormHelper ( ) <EOL> helper . form_tag = False <EOL> helper . add_layout ( Layout ( Container ( Col ( '<STR_LIT>' , <EOL> Fieldset ( "<STR_LIT>" , * self . form_obj . fields . keys ( ) , css_class = "<STR_LIT>" ) , horizontal = True , span = <NUM_LIT:12> ) <EOL> ) ) ) <EOL> self . form_obj . helper = helper <EOL> count = len ( queryset ) <EOL> if count == <NUM_LIT:1> : <EOL> objects_name = force_unicode ( self . opts . verbose_name ) <EOL> else : <EOL> objects_name = force_unicode ( self . opts . verbose_name_plural ) <EOL> context = self . get_context ( ) <EOL> context . update ( { <EOL> "<STR_LIT:title>" : _ ( "<STR_LIT>" ) % objects_name , <EOL> '<STR_LIT>' : objects_name , <EOL> '<STR_LIT>' : self . form_obj , <EOL> '<STR_LIT>' : queryset , <EOL> '<STR_LIT:count>' : count , <EOL> "<STR_LIT>" : self . opts , <EOL> "<STR_LIT>" : self . app_label , <EOL> '<STR_LIT>' : ACTION_CHECKBOX_NAME , <EOL> } ) <EOL> return TemplateResponse ( self . request , self . batch_change_form_template or <EOL> self . get_template_list ( '<STR_LIT>' ) , context , current_app = self . admin_site . name ) <EOL> @ filter_hook <EOL> def get_media ( self ) : <EOL> media = super ( BatchChangeAction , self ) . get_media ( ) <EOL> media = media + self . form_obj . media + self . vendor ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> return media </s>
