Пример #1
0
 def post(self,request):
     id_image=zoom=''
     if 'image' in request.POST:
         id_image = request.POST['image']
         image = Image.objects.get(id=id_image)
         path_segments = BASE_DIR+'/segment/static/uploads/%s/%s/segments/'%(request.user.id,image.image_type.folder)
         if not os.path.exists(path_segments):
             os.makedirs(path_segments)
         draw_segments='false'
         if 'zoom' in request.POST:
             zoom = float(request.POST['zoom'])
         if 'draw_segments' in request.POST:
             draw_segments = request.POST['draw_segments']
         if 'btn_return' in request.POST:
             return HttpResponseRedirect('/limages/') 
         if 'btn_create_segment' in request.POST:
             post = save_new_tags(request,image)
             form_segment = SegmentForm(post)     
             if form_segment.is_valid():
                 try:   
                     #TODO primer crear imatge i dsp guardar segment, no a l'inreves
                     segment = form_segment.save()   #commit=False  
                     filename = 'segment_'+str(id_image)+"_"+str(segment.id)+'.jpg'
                     segment.filename = 'uploads/%s/%s/segments/%s'%(request.user.id,image.image_type.folder,filename)
                     segment.save() 
                     x1=int(request.POST['x1'])
                     y1=int(request.POST['y1'])
                     x2=int(request.POST['x2'])
                     y2=int(request.POST['y2'])
                     image = Image.objects.get(id=id_image)
                     image_path = BASE_DIR+'/segment/static/'+str(image.filename)
                     i = PIL.Image.open(image_path)
                     filepath_segment = path_segments +filename
                     i.crop((x1,y1,x2,y2)).save(filepath_segment)  
                     segment = form_segment.save()
                 except:
                     traceback.print_exc()
         elif 'btn_generate_images' in request.POST:
             form_generate_images = GenerateImagesForm(user=request.user,image=image,data=request.POST)
             if form_generate_images.is_valid():
                 tags = request.POST['tags']
                 segments = Segment.objects.filter(tags=tags,image_id=id_image)
                 for segment in segments:
                     image = Image()
                     image.name = segment.filename.split('/')[4].split('.jpg')[0]
                     image.filename = segment.filename
                     image.image_type_id = request.POST['image_type']
                     image.parent_segment_id = segment.id
                     image.save()
                 
         elif 'btn_remove_segment' in request.POST:
             id_segment = request.POST['selected_row']
             segment = Segment.objects.get(id = id_segment)
             if segment:
                 filepath = path_segments+'segment_'+str(id_image)+"_"+str(segment.id)+'.jpg'
                 if os.path.isfile(filepath):
                     os.remove(filepath)
                 segment.delete()
         elif 'btn_zoom_in' in request.POST:
             if zoom < 5:
                 zoom = zoom+0.2
             return HttpResponseRedirect('/segment_image/?id='+id_image+'&zoom='+str(zoom)+'&draw_segments='+str(draw_segments))
         elif 'btn_zoom_out' in request.POST:
             if zoom > 0.2:
                 zoom = zoom-0.2
             return HttpResponseRedirect('/segment_image/?id='+id_image+'&zoom='+str(zoom)+'&draw_segments='+str(draw_segments))
     return HttpResponseRedirect('/segment_image/?id='+id_image+'&zoom='+str(zoom)+'&draw_segments='+str(draw_segments))