#NEWLINE# @ csrf_exempt #NEWLINE# def crop( request ) : #NEWLINE# #INDENT# if ( request . method == 'GET' ) : #NEWLINE# #INDENT# return json_error ( request , 'crop' , action = 'cropping image' , errors = [ 'Form submission invalid' ] ) #NEWLINE# #UNINDENT# crop_form = CropForm ( request . POST , request . FILES , prefix = 'crop' ) #NEWLINE# if ( not crop_form . is_valid ( ) ) : #NEWLINE# #INDENT# return json_error ( request , 'crop' , action = 'submitting form' , forms = [ crop_form ] , log = True , exc_info = full_exc_info ( ) ) #NEWLINE# #UNINDENT# crop_data = copy . deepcopy ( crop_form . cleaned_data ) #NEWLINE# db_image = Image ( image = crop_data [ 'orig_image' ] ) #NEWLINE# try : #NEWLINE# #INDENT# pil_image = PIL . Image . open ( db_image . image . path ) #NEWLINE# #UNINDENT# except IOError : #NEWLINE# #INDENT# pil_image = None #NEWLINE# #UNINDENT# FormSet = modelformset_factory ( Thumb , form = ThumbForm , formset = ThumbFormSet ) #NEWLINE# thumb_formset = FormSet ( request . POST , request . FILES , prefix = 'thumbs' ) #NEWLINE# if ( not thumb_formset . is_valid ( ) ) : #NEWLINE# #INDENT# return json_error ( request , 'crop' , action = 'submitting form' , formsets = [ thumb_formset ] , log = True , exc_info = full_exc_info ( ) ) #NEWLINE# #UNINDENT# cropped_thumbs = thumb_formset . save ( commit = False ) #NEWLINE# non_model_fields = ( set ( ThumbForm . declared_fields ) - set ( [ f . name for f in Thumb . _meta . fields ] ) ) #NEWLINE# json_thumb_fields = [ 'id' , 'name' , 'width' , 'height' ] #NEWLINE# thumbs_with_crops = [ t for t in cropped_thumbs if ( t . crop_w and t . crop_h ) ] #NEWLINE# thumbs_data = [ f . cleaned_data for f in thumb_formset ] #NEWLINE# standalone_mode = crop_data [ 'standalone' ] #NEWLINE# for ( i , ( thumb , thumb_form ) ) in enumerate ( zip ( cropped_thumbs , thumb_formset ) ) : #NEWLINE# #INDENT# changed_fields = ( set ( thumb_form . changed_data ) - non_model_fields ) #NEWLINE# thumb_form . _changed_data = list ( changed_fields ) #NEWLINE# thumb_data = thumbs_data [ i ] #NEWLINE# size = thumb_data [ 'size' ] #NEWLINE# if ( changed_fields & set ( [ 'crop_x' , 'crop_y' , 'crop_w' , 'crop_h' ] ) ) : #NEWLINE# #INDENT# thumb . pk = None #NEWLINE# thumb . width = min ( filter ( None , [ thumb . width , thumb . crop_w ] ) ) #NEWLINE# thumb . height = min ( filter ( None , [ thumb . height , thumb . crop_h ] ) ) #NEWLINE# try : #NEWLINE# #INDENT# new_thumbs = db_image . save_size ( size , thumb , tmp = True , standalone = standalone_mode ) #NEWLINE# #UNINDENT# except CropDusterResizeException as e : #NEWLINE# #INDENT# return json_error ( request , 'crop' , action = 'saving size' , errors = [ force_unicode ( e ) ] ) #NEWLINE# #UNINDENT# if ( not new_thumbs ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if standalone_mode : #NEWLINE# #INDENT# thumb = new_thumbs #NEWLINE# new_thumbs = { #NEWLINE# #INDENT# thumb . name : thumb , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# cropped_thumbs [ i ] = thumb = new_thumbs . get ( thumb . name , thumb ) #NEWLINE# update_props = [ 'crop_x' , 'crop_y' , 'crop_w' , 'crop_h' , 'width' , 'height' , 'id' , 'name' ] #NEWLINE# for prop in update_props : #NEWLINE# #INDENT# thumbs_data [ i ] [ prop ] = getattr ( thumb , prop ) #NEWLINE# #UNINDENT# thumbs_data [ i ] . update ( { #NEWLINE# #INDENT# 'changed' : True , #NEWLINE# 'url' : db_image . get_image_url ( thumb . name ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# for ( name , new_thumb ) in six . iteritems ( new_thumbs ) : #NEWLINE# #INDENT# thumb_data = dict ( [ ( k , getattr ( new_thumb , k ) ) for k in json_thumb_fields ] ) #NEWLINE# crop_data [ 'thumbs' ] . update ( { #NEWLINE# #INDENT# name : thumb_data , #NEWLINE# #UNINDENT# } ) #NEWLINE# if new_thumb . reference_thumb_id : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# thumbs_data [ i ] [ 'thumbs' ] . update ( { #NEWLINE# #INDENT# name : thumb_data , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( thumb . pk and thumb . name and thumb . crop_w and thumb . crop_h ) : #NEWLINE# #INDENT# thumb_path = db_image . get_image_path ( thumb . name , tmp = False ) #NEWLINE# tmp_thumb_path = db_image . get_image_path ( thumb . name , tmp = True ) #NEWLINE# if os . path . exists ( thumb_path ) : #NEWLINE# #INDENT# if ( ( not thumb_form . cleaned_data . get ( 'changed' ) ) or ( not os . path . exists ( tmp_thumb_path ) ) ) : #NEWLINE# #INDENT# shutil . copy ( thumb_path , tmp_thumb_path ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( ( not thumb . pk ) and ( not thumb . crop_w ) and ( not thumb . crop_h ) ) : #NEWLINE# #INDENT# if ( not len ( thumbs_with_crops ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# best_fit = thumb_form . cleaned_data [ 'size' ] . fit_to_crop ( thumbs_with_crops [ 0 ] , original_image = pil_image ) #NEWLINE# if best_fit : #NEWLINE# #INDENT# thumbs_data [ i ] . update ( { #NEWLINE# #INDENT# 'crop_x' : best_fit . box . x1 , #NEWLINE# 'crop_y' : best_fit . box . y1 , #NEWLINE# 'crop_w' : best_fit . box . w , #NEWLINE# 'crop_h' : best_fit . box . h , #NEWLINE# 'changed' : True , #NEWLINE# 'id' : None , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# for thumb_data in thumbs_data : #NEWLINE# #INDENT# if isinstance ( thumb_data [ 'id' ] , Thumb ) : #NEWLINE# #INDENT# thumb_data [ 'id' ] = thumb_data [ 'id' ] . pk #NEWLINE# #UNINDENT# #UNINDENT# return HttpResponse ( json . dumps ( { #NEWLINE# #INDENT# 'crop' : crop_data , #NEWLINE# 'thumbs' : thumbs_data , #NEWLINE# 'initial' : True , #NEWLINE# #UNINDENT# } ) , content_type = 'application/json' )