<s> from django . utils . encoding import force_text <EOL> from rest_framework import status <EOL> from rest_framework . response import Response <EOL> from rest_framework_extensions . settings import extensions_api_settings <EOL> from rest_framework_extensions import utils <EOL> class BulkOperationBaseMixin ( object ) : <EOL> def is_object_operation ( self ) : <EOL> return bool ( self . get_object_lookup_value ( ) ) <EOL> def get_object_lookup_value ( self ) : <EOL> return self . kwargs . get ( getattr ( self , '<STR_LIT>' , None ) or self . lookup_field , None ) <EOL> def is_valid_bulk_operation ( self ) : <EOL> if extensions_api_settings . DEFAULT_BULK_OPERATION_HEADER_NAME : <EOL> header_name = utils . prepare_header_name ( extensions_api_settings . DEFAULT_BULK_OPERATION_HEADER_NAME ) <EOL> return bool ( self . request . META . get ( header_name , None ) ) , { <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( <EOL> extensions_api_settings . DEFAULT_BULK_OPERATION_HEADER_NAME <EOL> ) <EOL> } <EOL> else : <EOL> return True , { } <EOL> class ListDestroyModelMixin ( BulkOperationBaseMixin ) : <EOL> def delete ( self , request , * args , ** kwargs ) : <EOL> if self . is_object_operation ( ) : <EOL> return super ( ListDestroyModelMixin , self ) . destroy ( request , * args , ** kwargs ) <EOL> else : <EOL> return self . destroy_bulk ( request , * args , ** kwargs ) <EOL> def destroy_bulk ( self , request , * args , ** kwargs ) : <EOL> is_valid , errors = self . is_valid_bulk_operation ( ) <EOL> if is_valid : <EOL> queryset = self . filter_queryset ( self . get_queryset ( ) ) <EOL> self . pre_delete_bulk ( queryset ) <EOL> queryset . delete ( ) <EOL> self . post_delete_bulk ( queryset ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> else : <EOL> return Response ( errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> def pre_delete_bulk ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def post_delete_bulk ( self , queryset ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ListUpdateModelMixin ( BulkOperationBaseMixin ) : <EOL> def patch ( self , request , * args , ** kwargs ) : <EOL> if self . is_object_operation ( ) : <EOL> return super ( ListUpdateModelMixin , self ) . partial_update ( request , * args , ** kwargs ) <EOL> else : <EOL> return self . partial_update_bulk ( request , * args , ** kwargs ) <EOL> def partial_update_bulk ( self , request , * args , ** kwargs ) : <EOL> is_valid , errors = self . is_valid_bulk_operation ( ) <EOL> if is_valid : <EOL> queryset = self . filter_queryset ( self . get_queryset ( ) ) <EOL> update_bulk_dict = self . get_update_bulk_dict ( serializer = self . get_serializer_class ( ) ( ) , data = request . data ) <EOL> self . pre_save_bulk ( queryset , update_bulk_dict ) <EOL> try : <EOL> queryset . update ( ** update_bulk_dict ) <EOL> except ValueError as e : <EOL> errors = { <EOL> '<STR_LIT>' : force_text ( e ) <EOL> } <EOL> return Response ( errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> self . post_save_bulk ( queryset , update_bulk_dict ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> else : <EOL> return Response ( errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> def get_update_bulk_dict ( self , serializer , data ) : <EOL> update_bulk_dict = { } <EOL> for field_name , field in serializer . fields . items ( ) : <EOL> if field_name in data and not field . read_only : <EOL> update_bulk_dict [ field . source or field_name ] = data [ field_name ] <EOL> return update_bulk_dict <EOL> def pre_save_bulk ( self , queryset , update_bulk_dict ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def post_save_bulk ( self , queryset , update_bulk_dict ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
