<s> import json <EOL> import re <EOL> import operator <EOL> import logging <EOL> from . . forms import XEditableUpdateForm <EOL> from . base import DatatableView <EOL> from django import get_version <EOL> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from django . views . decorators . csrf import ensure_csrf_cookie <EOL> from django . db . models import ForeignKey <EOL> log = logging . getLogger ( __name__ ) <EOL> CAN_UPDATE_FIELDS = get_version ( ) . split ( '<STR_LIT:.>' ) >= [ '<STR_LIT:1>' , '<STR_LIT:5>' ] <EOL> class XEditableMixin ( object ) : <EOL> xeditable_form_class = XEditableUpdateForm <EOL> xeditable_fieldname_param = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if request . GET . get ( self . xeditable_fieldname_param ) : <EOL> return self . get_ajax_xeditable_choices ( request , * args , ** kwargs ) <EOL> method = super ( XEditableMixin , self ) . get <EOL> method = ensure_csrf_cookie ( method ) <EOL> return method ( request , * args , ** kwargs ) <EOL> def get_ajax_xeditable_choices ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> field_name = request . GET . get ( self . xeditable_fieldname_param ) <EOL> if not field_name : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> queryset = self . get_queryset ( ) <EOL> if not self . model : <EOL> self . model = queryset . model <EOL> from datatableview . views import legacy <EOL> if isinstance ( self , legacy . LegacyDatatableMixin ) : <EOL> columns = self . _get_datatable_options ( ) [ '<STR_LIT>' ] <EOL> for name in columns : <EOL> if isinstance ( name , ( list , tuple ) ) : <EOL> name = name [ <NUM_LIT:1> ] <EOL> if name == field_name : <EOL> break <EOL> else : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> else : <EOL> if field_name not in self . get_datatable ( ) . config [ '<STR_LIT>' ] : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> field = self . model . _meta . get_field_by_name ( field_name ) [ <NUM_LIT:0> ] <EOL> choices = self . get_field_choices ( field , field_name ) <EOL> return HttpResponse ( json . dumps ( choices ) ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . object_list = None <EOL> form = self . get_xeditable_form ( self . get_xeditable_form_class ( ) ) <EOL> if form . is_valid ( ) : <EOL> obj = self . get_update_object ( form ) <EOL> if obj is None : <EOL> data = json . dumps ( { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : "<STR_LIT>" <EOL> } ) <EOL> return HttpResponse ( data , content_type = "<STR_LIT:application/json>" , status = <NUM_LIT> ) <EOL> return self . update_object ( form , obj ) <EOL> else : <EOL> data = json . dumps ( { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : form . errors , <EOL> } ) <EOL> return HttpResponse ( data , content_type = "<STR_LIT:application/json>" , status = <NUM_LIT> ) <EOL> def get_xeditable_form_class ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . xeditable_form_class <EOL> def get_xeditable_form_kwargs ( self ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { <EOL> '<STR_LIT>' : self . get_queryset ( ) . model , <EOL> } <EOL> if self . request . method in ( '<STR_LIT:POST>' , '<STR_LIT>' ) : <EOL> kwargs . update ( { <EOL> '<STR_LIT:data>' : self . request . POST , <EOL> } ) <EOL> return kwargs <EOL> def get_xeditable_form ( self , form_class ) : <EOL> """<STR_LIT>""" <EOL> return form_class ( ** self . get_xeditable_form_kwargs ( ) ) <EOL> def get_update_object ( self , form ) : <EOL> """<STR_LIT>""" <EOL> pk = form . cleaned_data [ '<STR_LIT>' ] <EOL> queryset = self . get_queryset ( ) <EOL> try : <EOL> obj = queryset . get ( pk = pk ) <EOL> except queryset . model . DoesNotExist : <EOL> obj = None <EOL> return obj <EOL> def update_object ( self , form , obj ) : <EOL> """<STR_LIT>""" <EOL> field_name = form . cleaned_data [ '<STR_LIT:name>' ] <EOL> value = form . cleaned_data [ '<STR_LIT:value>' ] <EOL> setattr ( obj , field_name , value ) <EOL> save_kwargs = { } <EOL> if CAN_UPDATE_FIELDS : <EOL> save_kwargs [ '<STR_LIT>' ] = [ field_name ] <EOL> obj . save ( ** save_kwargs ) <EOL> data = json . dumps ( { <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> } ) <EOL> return HttpResponse ( data , content_type = "<STR_LIT:application/json>" ) <EOL> def get_field_choices ( self , field , field_name ) : <EOL> """<STR_LIT>""" <EOL> if self . request . GET . get ( '<STR_LIT>' ) : <EOL> names = [ '<STR_LIT:id>' , '<STR_LIT:text>' ] <EOL> else : <EOL> names = [ '<STR_LIT:value>' , '<STR_LIT:text>' ] <EOL> choices_getter = getattr ( self , '<STR_LIT>' , None ) <EOL> if choices_getter is None : <EOL> if isinstance ( field , ForeignKey ) : <EOL> choices_getter = self . _get_foreignkey_choices <EOL> else : <EOL> choices_getter = self . _get_default_choices <EOL> return [ dict ( zip ( names , choice ) ) for choice in choices_getter ( field , field_name ) ] <EOL> def _get_foreignkey_choices ( self , field , field_name ) : <EOL> formfield_kwargs = { } <EOL> if not field . blank : <EOL> formfield_kwargs [ '<STR_LIT>' ] = None <EOL> formfield = field . formfield ( ** formfield_kwargs ) <EOL> return formfield . choices <EOL> def _get_default_choices ( self , field , field_name ) : <EOL> return field . choices <EOL> class XEditableDatatableView ( XEditableMixin , DatatableView ) : <EOL> pass </s>
