<s> from django import template <EOL> from django . core . exceptions import PermissionDenied , ObjectDoesNotExist <EOL> from django . db import models , transaction <EOL> from django . forms . models import modelform_factory <EOL> from django . http import Http404 , HttpResponse <EOL> from django . utils . encoding import force_unicode , smart_unicode <EOL> from django . utils . html import escape , conditional_escape <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ <EOL> from xadmin . plugins . ajax import JsonErrorDict <EOL> from xadmin . sites import site <EOL> from xadmin . util import lookup_field , display_for_field , label_for_field , unquote , boolean_icon <EOL> from xadmin . views import BaseAdminPlugin , ModelFormAdminView , ListAdminView <EOL> from xadmin . views . base import csrf_protect_m , filter_hook <EOL> from xadmin . views . edit import ModelFormAdminUtil <EOL> from xadmin . views . list import EMPTY_CHANGELIST_VALUE <EOL> from xadmin . layout import FormHelper <EOL> class EditablePlugin ( BaseAdminPlugin ) : <EOL> list_editable = [ ] <EOL> def __init__ ( self , admin_view ) : <EOL> super ( EditablePlugin , self ) . __init__ ( admin_view ) <EOL> self . editable_need_fields = { } <EOL> def init_request ( self , * args , ** kwargs ) : <EOL> active = bool ( self . request . method == '<STR_LIT:GET>' and self . admin_view . has_change_permission ( ) and self . list_editable ) <EOL> if active : <EOL> self . model_form = self . get_model_view ( ModelFormAdminUtil , self . model ) . form_obj <EOL> return active <EOL> def result_item ( self , item , obj , field_name , row ) : <EOL> if self . list_editable and item . field and item . field . editable and ( field_name in self . list_editable ) : <EOL> pk = getattr ( obj , obj . _meta . pk . attname ) <EOL> field_label = label_for_field ( field_name , obj , <EOL> model_admin = self . admin_view , <EOL> return_attr = False <EOL> ) <EOL> item . wraps . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> item . btns . append ( ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) % <EOL> ( _ ( u"<STR_LIT>" ) % field_label , field_name , self . admin_view . model_admin_url ( '<STR_LIT>' , pk ) + '<STR_LIT>' + field_name ) ) <EOL> if field_name not in self . editable_need_fields : <EOL> self . editable_need_fields [ field_name ] = item . field <EOL> return item <EOL> def get_media ( self , media ) : <EOL> if self . editable_need_fields : <EOL> media = media + self . model_form . media + self . vendor ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> return media <EOL> class EditPatchView ( ModelFormAdminView , ListAdminView ) : <EOL> def init_request ( self , object_id , * args , ** kwargs ) : <EOL> self . org_obj = self . get_object ( unquote ( object_id ) ) <EOL> self . pk_attname = self . opts . pk . attname <EOL> if not self . has_change_permission ( self . org_obj ) : <EOL> raise PermissionDenied <EOL> if self . org_obj is None : <EOL> raise Http404 ( _ ( '<STR_LIT>' ) % <EOL> { '<STR_LIT:name>' : force_unicode ( self . opts . verbose_name ) , '<STR_LIT:key>' : escape ( object_id ) } ) <EOL> def get_new_field_html ( self , f ) : <EOL> result = self . result_item ( self . org_obj , f , { '<STR_LIT>' : <EOL> False , '<STR_LIT:object>' : self . org_obj } ) <EOL> return mark_safe ( result . text ) if result . allow_tags else conditional_escape ( result . text ) <EOL> def _get_new_field_html ( self , field_name ) : <EOL> try : <EOL> f , attr , value = lookup_field ( field_name , self . org_obj , self ) <EOL> except ( AttributeError , ObjectDoesNotExist ) : <EOL> return EMPTY_CHANGELIST_VALUE <EOL> else : <EOL> allow_tags = False <EOL> if f is None : <EOL> allow_tags = getattr ( attr , '<STR_LIT>' , False ) <EOL> boolean = getattr ( attr , '<STR_LIT>' , False ) <EOL> if boolean : <EOL> allow_tags = True <EOL> text = boolean_icon ( value ) <EOL> else : <EOL> text = smart_unicode ( value ) <EOL> else : <EOL> if isinstance ( f . rel , models . ManyToOneRel ) : <EOL> field_val = getattr ( self . org_obj , f . name ) <EOL> if field_val is None : <EOL> text = EMPTY_CHANGELIST_VALUE <EOL> else : <EOL> text = field_val <EOL> else : <EOL> text = display_for_field ( value , f ) <EOL> return mark_safe ( text ) if allow_tags else conditional_escape ( text ) <EOL> @ filter_hook <EOL> def get ( self , request , object_id ) : <EOL> model_fields = [ f . name for f in self . opts . fields ] <EOL> fields = [ f for f in request . GET [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) if f in model_fields ] <EOL> defaults = { <EOL> "<STR_LIT>" : self . form , <EOL> "<STR_LIT>" : fields , <EOL> "<STR_LIT>" : self . formfield_for_dbfield , <EOL> } <EOL> form_class = modelform_factory ( self . model , ** defaults ) <EOL> form = form_class ( instance = self . org_obj ) <EOL> helper = FormHelper ( ) <EOL> helper . form_tag = False <EOL> form . helper = helper <EOL> s = '<STR_LIT>' + '<STR_LIT>' <EOL> t = template . Template ( s ) <EOL> c = template . Context ( { '<STR_LIT>' : form , '<STR_LIT>' : self . model_admin_url ( '<STR_LIT>' , self . org_obj . pk ) } ) <EOL> return HttpResponse ( t . render ( c ) ) <EOL> @ filter_hook <EOL> @ csrf_protect_m <EOL> @ transaction . atomic <EOL> def post ( self , request , object_id ) : <EOL> model_fields = [ f . name for f in self . opts . fields ] <EOL> fields = [ f for f in request . POST . keys ( ) if f in model_fields ] <EOL> defaults = { <EOL> "<STR_LIT>" : self . form , <EOL> "<STR_LIT>" : fields , <EOL> "<STR_LIT>" : self . formfield_for_dbfield , <EOL> } <EOL> form_class = modelform_factory ( self . model , ** defaults ) <EOL> form = form_class ( <EOL> instance = self . org_obj , data = request . POST , files = request . FILES ) <EOL> result = { } <EOL> if form . is_valid ( ) : <EOL> form . save ( commit = True ) <EOL> result [ '<STR_LIT:result>' ] = '<STR_LIT:success>' <EOL> result [ '<STR_LIT>' ] = form . cleaned_data <EOL> result [ '<STR_LIT>' ] = dict ( <EOL> [ ( f , self . get_new_field_html ( f ) ) for f in fields ] ) <EOL> else : <EOL> result [ '<STR_LIT:result>' ] = '<STR_LIT:error>' <EOL> result [ '<STR_LIT>' ] = JsonErrorDict ( form . errors , form ) . as_json ( ) <EOL> return self . render_response ( result ) <EOL> site . register_plugin ( EditablePlugin , ListAdminView ) <EOL> site . register_modelview ( r'<STR_LIT>' , EditPatchView , name = '<STR_LIT>' ) </s>
