<s> import json <EOL> import logging <EOL> from django . views . generic import ListView , TemplateView <EOL> from django . views . generic . list import MultipleObjectMixin <EOL> from django . http import HttpResponse <EOL> from django . conf import settings <EOL> from . . datatables import Datatable , DatatableOptions <EOL> log = logging . getLogger ( __name__ ) <EOL> class DatatableJSONResponseMixin ( object ) : <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if request . is_ajax ( ) or request . GET . get ( '<STR_LIT>' ) == '<STR_LIT:true>' : <EOL> return self . get_ajax ( request , * args , ** kwargs ) <EOL> return super ( DatatableJSONResponseMixin , self ) . get ( request , * args , ** kwargs ) <EOL> def get_json_response_object ( self , datatable ) : <EOL> """<STR_LIT>""" <EOL> datatable . populate_records ( ) <EOL> response_data = { <EOL> '<STR_LIT>' : self . request . GET . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : datatable . total_initial_record_count , <EOL> '<STR_LIT>' : datatable . unpaged_record_count , <EOL> '<STR_LIT>' : [ dict ( record , ** { <EOL> '<STR_LIT>' : record . pop ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : record . pop ( '<STR_LIT>' ) , <EOL> } ) for record in datatable . get_records ( ) ] , <EOL> } <EOL> return response_data <EOL> def serialize_to_json ( self , response_data ) : <EOL> """<STR_LIT>""" <EOL> indent = None <EOL> if settings . DEBUG : <EOL> indent = <NUM_LIT:4> <EOL> return json . dumps ( response_data , indent = indent ) <EOL> class DatatableMixin ( DatatableJSONResponseMixin , MultipleObjectMixin ) : <EOL> """<STR_LIT>""" <EOL> datatable_class = None <EOL> context_datatable_name = '<STR_LIT>' <EOL> def get_ajax ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> datatable = self . get_datatable ( ) <EOL> datatable . configure ( ) <EOL> response_data = self . get_json_response_object ( datatable ) <EOL> response = HttpResponse ( self . serialize_to_json ( response_data ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> return response <EOL> def get_datatable ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> datatable_class = self . get_datatable_class ( ) <EOL> if datatable_class is None : <EOL> class AutoMeta : <EOL> model = self . model or self . get_queryset ( ) . model <EOL> opts = AutoMeta ( ) <EOL> datatable_class = Datatable <EOL> else : <EOL> opts = datatable_class . options_class ( datatable_class . _meta ) <EOL> kwargs = self . get_datatable_kwargs ( ** kwargs ) <EOL> for meta_opt in opts . __dict__ : <EOL> if meta_opt in kwargs : <EOL> setattr ( opts , meta_opt , kwargs . pop ( meta_opt ) ) <EOL> datatable_class = type ( '<STR_LIT>' % ( datatable_class . __name__ , ) , ( datatable_class , ) , { <EOL> '<STR_LIT:Meta>' : opts , <EOL> } ) <EOL> return datatable_class ( ** kwargs ) <EOL> def get_datatable_class ( self ) : <EOL> return self . datatable_class <EOL> def get_datatable_kwargs ( self , ** kwargs ) : <EOL> queryset = self . get_queryset ( ) <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : queryset , <EOL> '<STR_LIT>' : self , <EOL> '<STR_LIT>' : self . model or queryset . model , <EOL> } ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT:url>' ] = self . request . path <EOL> kwargs [ '<STR_LIT>' ] = self . request . GET <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = { } <EOL> settings = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> for k in settings : <EOL> if hasattr ( self , k ) : <EOL> kwargs [ k ] = getattr ( self , k ) <EOL> return kwargs <EOL> def preload_record_data ( self , obj ) : <EOL> return { } <EOL> def get_datatable_context_name ( self ) : <EOL> return self . context_datatable_name <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( DatatableMixin , self ) . get_context_data ( ** kwargs ) <EOL> context [ self . get_datatable_context_name ( ) ] = self . get_datatable ( ) <EOL> return context <EOL> class DatatableView ( DatatableMixin , ListView ) : <EOL> """<STR_LIT>""" <EOL> class MultipleDatatableMixin ( DatatableJSONResponseMixin ) : <EOL> """<STR_LIT>""" <EOL> datatable_classes = None <EOL> def get_ajax ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> datatable = self . get_active_ajax_datatable ( ) <EOL> datatable . configure ( ) <EOL> response_data = self . get_json_response_object ( datatable ) <EOL> response = HttpResponse ( self . serialize_to_json ( response_data ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> return response <EOL> def get_datatables ( self , only = None ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _datatables = { } <EOL> datatable_classes = self . get_datatable_classes ( ) <EOL> for name , datatable_class in datatable_classes . items ( ) : <EOL> if only and name != only : <EOL> continue <EOL> queryset_getter_name = '<STR_LIT>' % ( name , ) <EOL> queryset_getter = getattr ( self , queryset_getter_name , None ) <EOL> if queryset_getter is None : <EOL> raise ValueError ( "<STR_LIT>" % ( self . __class__ . __name__ , <EOL> queryset_getter_name ) ) <EOL> queryset = queryset_getter ( ) <EOL> if datatable_class is None : <EOL> class AutoMeta : <EOL> model = queryset . model <EOL> datatable_class = type ( '<STR_LIT>' % ( self . __class__ . __name__ , ) , ( Datatable , ) , { <EOL> '<STR_LIT:Meta>' : AutoMeta , <EOL> } ) <EOL> elif datatable_class . _meta . model is None : <EOL> opts = datatable_class . options_class ( datatable_class . _meta ) <EOL> opts . model = queryset . model <EOL> datatable_class = type ( '<STR_LIT>' % ( datatable_class . __name__ , ) , ( datatable_class , ) , { <EOL> '<STR_LIT:Meta>' : opts , <EOL> } ) <EOL> default_kwargs = { <EOL> '<STR_LIT>' : queryset , <EOL> } <EOL> kwargs_getter_name = '<STR_LIT>' % ( name , ) <EOL> kwargs_getter = getattr ( self , kwargs_getter_name , self . get_default_datatable_kwargs ) <EOL> kwargs = kwargs_getter ( ** default_kwargs ) <EOL> if '<STR_LIT:url>' in kwargs : <EOL> kwargs [ '<STR_LIT:url>' ] = kwargs [ '<STR_LIT:url>' ] + "<STR_LIT>" % ( name , ) <EOL> self . _datatables [ name ] = datatable_class ( ** kwargs ) <EOL> return self . _datatables <EOL> def get_datatable_classes ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . datatable_classes is None : <EOL> return { } <EOL> return dict ( self . datatable_classes ) <EOL> def get_default_datatable_kwargs ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs [ '<STR_LIT>' ] = self <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT:url>' ] = self . request . path <EOL> kwargs [ '<STR_LIT>' ] = self . request . GET <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] = { } <EOL> return kwargs <EOL> def get_active_ajax_datatable ( self ) : <EOL> """<STR_LIT>""" <EOL> datatables_dict = self . get_datatables ( only = self . request . GET [ '<STR_LIT>' ] ) <EOL> return list ( datatables_dict . values ( ) ) [ <NUM_LIT:0> ] <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( MultipleDatatableMixin , self ) . get_context_data ( ** kwargs ) <EOL> for name , datatable in self . get_datatables ( ) . items ( ) : <EOL> context_name = '<STR_LIT>' % ( name , ) <EOL> context [ context_name ] = datatable <EOL> return context <EOL> class MultipleDatatableView ( MultipleDatatableMixin , TemplateView ) : <EOL> pass </s>
