def form_valid(self, form): try: request_obj = Request() request_obj.socketid = self.request.POST['socketid-hidden'] self.object = form.save() serialize(self.object) data = {'files': []} old_save_dir = os.path.dirname(conf.PIC_DIR) folder_name = str(shortuuid.uuid()) save_dir = os.path.join(conf.PIC_DIR, folder_name) # Make the new directory based on time if not os.path.exists(save_dir): os.makedirs(save_dir) os.makedirs(os.path.join(save_dir, 'results')) all_files = self.request.FILES.getlist('file') for file in all_files: log_to_terminal(str('Saving file:' + file.name), request_obj.socketid) a = Picture() tick = time.time() strtick = str(tick).replace('.', '_') fileName, fileExtension = os.path.splitext(file.name) file.name = fileName + strtick + fileExtension a.file.save(file.name, file) file.name = a.file.name # imgstr = base64.b64encode(file.read()) # img_file = Image.open(BytesIO(base64.b64decode(imgstr))) # img_file.thumbnail(size, Image.ANTIALIAS) imgfile = Image.open(os.path.join(old_save_dir, file.name)) size = (500, 500) imgfile.thumbnail(size, Image.ANTIALIAS) imgfile.save(os.path.join(save_dir, file.name)) thumbPath = os.path.join(folder_name, file.name) data['files'].append({ 'url': conf.PIC_URL + thumbPath, 'name': file.name, 'type': 'image/png', 'thumbnailUrl': conf.PIC_URL + thumbPath, 'size': 0, }) request_obj.run_executable(save_dir, os.path.join(save_dir, 'results/'), os.path.join(conf.PIC_URL, folder_name, 'results/result_stitch.jpg')) response = JSONResponse(data, mimetype=response_mimetype(self.request)) response['Content-Disposition'] = 'inline; filename=files.json' return response except: r.publish('chat', json.dumps({'message': str(traceback.format_exc()), 'socketid': str(self.socketid)}))
def matlabReadRequest(request): redis.StrictRedis(host=config.REDIS_HOST, port=6379, db=0) if request.method == 'POST': post_dict = parser.parse(request.POST.urlencode()) try: job_obj = Job(params_obj=post_dict) log_to_terminal('Server: Post Request Recieved', job_obj.socketid) response = saveFilesAndProcess(request, job_obj) except: log_and_exit(str(traceback.format_exc()), job_obj.socketid) return HttpResponse('Error at server side') return HttpResponse(str(response)) else: response = JSONResponse({}, {}, response_mimetype(request)) response['Content-Disposition'] = 'inline; filename=files.json' return response
def form_valid(self, form): try: request_obj = Request() request_obj.socketid = self.request.POST['socketid-hidden'] self.object = form.save() serialize(self.object) data = {'files': []} # List of Images: Url, Name, Type print data old_save_dir = os.path.dirname(conf.PIC_DIR) folder_name = str(shortuuid.uuid()) save_dir = os.path.join(conf.PIC_DIR, folder_name) # Make the new directory based on time if not os.path.exists(save_dir): os.makedirs(save_dir) os.makedirs(os.path.join(save_dir, 'results')) all_files = self.request.FILES.getlist('file') for file in all_files: log_to_terminal(str('Saving file:' + file.name), request_obj.socketid) a = Picture() tick = time.time() strtick = str(tick).replace('.','_') fileName, fileExtension = os.path.splitext(file.name) file.name = fileName + strtick + fileExtension a.file.save(file.name, file) file.name = a.file.name # imgstr = base64.b64encode(file.read()) # img_file = Image.open(BytesIO(base64.b64decode(imgstr))) # img_file.thumbnail(size, Image.ANTIALIAS) imgfile = Image.open(os.path.join(old_save_dir, file.name)) size = (500,500) imgfile.thumbnail(size,Image.ANTIALIAS) imgfile.save(os.path.join(save_dir, file.name)) thumbPath = os.path.join(folder_name, file.name) data['files'].append({ 'url': conf.PIC_URL+thumbPath, 'name': file.name, 'type': 'image/png', 'thumbnailUrl': conf.PIC_URL+thumbPath, 'size': 0, }) path, dirs, files = os.walk(save_dir).next() file_count = len(files) list = [os.path.join(conf.EXEC_DIR, 'stitch_full'), '--img', save_dir, '--verbose', '1', '--output', os.path.join(save_dir, 'results/'), ] print list request_obj.run_executable(list, os.path.join(conf.PIC_URL, folder_name, 'results/result_stitch.jpg')) response = JSONResponse(data, mimetype=response_mimetype(self.request)) response['Content-Disposition'] = 'inline; filename=files.json' return response except Exception as e: print traceback.format_exc() r.publish('chat', json.dumps({'message': str(traceback.format_exc()), 'socketid': str(self.socketid)}))