#NEWLINE# @ transaction . atomic #NEWLINE# def save( self ) : #NEWLINE# #INDENT# if ( self . instance . pk is None ) : #NEWLINE# #INDENT# raise Exception ( 'Cannot save a GroupPagePermissionFormSet for an unsaved group instance' ) #NEWLINE# #UNINDENT# forms_to_save = [ form for form in self . forms if ( form not in self . deleted_forms ) ] #NEWLINE# final_permission_records = set ( ) #NEWLINE# for form in forms_to_save : #NEWLINE# #INDENT# for permission_type in form . cleaned_data [ 'permission_types' ] : #NEWLINE# #INDENT# final_permission_records . add ( ( form . cleaned_data [ 'page' ] , permission_type ) ) #NEWLINE# #UNINDENT# #UNINDENT# permission_ids_to_delete = [ ] #NEWLINE# permission_records_to_keep = set ( ) #NEWLINE# for pp in self . instance . page_permissions . all ( ) : #NEWLINE# #INDENT# if ( ( pp . page , pp . permission_type ) in final_permission_records ) : #NEWLINE# #INDENT# permission_records_to_keep . add ( ( pp . page , pp . permission_type ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# permission_ids_to_delete . append ( pp . pk ) #NEWLINE# #UNINDENT# #UNINDENT# self . instance . page_permissions . filter ( pk__in = permission_ids_to_delete ) . delete ( ) #NEWLINE# permissions_to_add = ( final_permission_records - permission_records_to_keep ) #NEWLINE# GroupPagePermission . objects . bulk_create ( [ GroupPagePermission ( group = self . instance , page = page , permission_type = permission_type ) for ( page , permission_type ) in permissions_to_add ] )