def clone_building_model(building_model_id): errors = [] if building_model_id == '': return json.dumps([gettext('Building model not found')]), 404 building_model = BuildingModel.query.filter_by(id=building_model_id).first() if building_model is None: return json.dumps([gettext('Building model not found')]), 404 new_building_model = BuildingModel('Clone of ' + building_model.name, building_model.data_file, g.user, addition_information=building_model.addition_information) new_building_model.file_type = building_model.file_type db.session.add(new_building_model) db.session.commit() return json.dumps(new_building_model.to_dict(include_owner=True)), 200
def clone_building_model(building_model_id): errors = [] if building_model_id == '': return json.dumps([gettext('Building model not found')]), 404 building_model = BuildingModel.query.filter_by( id=building_model_id).first() if building_model is None: return json.dumps([gettext('Building model not found')]), 404 new_building_model = BuildingModel( 'Clone of ' + building_model.name, building_model.data_file, g.user, addition_information=building_model.addition_information) new_building_model.file_type = building_model.file_type db.session.add(new_building_model) db.session.commit() return json.dumps(new_building_model.to_dict(include_owner=True)), 200
def add_building_model(): errors = [] if request.method == 'GET': return render_template("users/add_building_model.html", errors=errors) else: addition_information = '' name = request.form.get('name', '') if name.strip() == '': errors.append(gettext('Scenario name is required')) if request.files['data_file'].filename == '': errors.append(gettext('Model file is required')) if len(errors) > 0: return render_template("users/add_building_model.html", errors=errors), 400 uploaded_file = upload_file(request.files['data_file'], 'static/models/building_models', file_type="model") data_file = uploaded_file['filename'] if uploaded_file['extension'] not in config.ALLOWED_FILE_TYPES: errors.append('Unrecognized model file format') try: os.remove(os.path.join(app_dir, 'static/models/building_models', data_file)) except: pass return render_template("users/add_building_model.html", errors=errors), 400 if os.stat(os.path.join(app_dir, 'static/models/building_models', data_file)).st_size >= 1024*1024*10: errors.append('File too big, max file size is 10MB') os.remove(os.path.join(app_dir, 'static/models/building_models', data_file)) return render_template("users/add_building_model.html", errors=errors), 400 #check if file type is zip => unzip it if uploaded_file['extension'] == 'zip': tmp = data_file zip_ref = zipfile.ZipFile(os.path.join(app_dir, 'static/models/building_models', data_file), 'r') zip_ref.extractall(os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension'])) zip_ref.close() #os.rename(os.path.join(app_dir, 'static/models/building_models', uploaded_file['original_filename']), os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension'])) zipped_dir = os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension']) #check if inside of the zip is a dir of contain file, if contain only 1 dir => move file from inside that dir outside tmp_files = os.listdir(zipped_dir) if len(tmp_files) == 1 and not os.path.isfile(os.path.join(zipped_dir, tmp_files[0])): #contain only 1 dir inside_files = os.listdir(os.path.join(zipped_dir, tmp_files[0])) new_name = utilities.generate_random_string(50) os.rename(os.path.join(zipped_dir, tmp_files[0]), os.path.join(zipped_dir, new_name)) for file in inside_files: try: shutil.move(os.path.join(zipped_dir, new_name, file), os.path.join(zipped_dir, file)) except: pass try: shutil.rmtree(os.path.join(zipped_dir, new_name)) #delete unzipped folder except: pass files = [f for f in os.listdir(zipped_dir) if os.path.isfile(os.path.join(zipped_dir, f))] important_file_index = -1 important_file_name = '' file_index = -1 for file in files: filename_parts = file.split('.') current_filename = filename_parts[0] for i in range(1, len(filename_parts)-1): current_filename += '.' + filename_parts[i] extension = filename_parts[len(filename_parts)-1].lower() os.rename(os.path.join(zipped_dir, file), os.path.join(zipped_dir, current_filename+'.'+extension)) if important_file_index < 0: file_index += 1 if extension in ['dae', 'obj']: important_file_name = current_filename important_file_index = file_index file_type = extension if important_file_index < 0: # user uploaded unrecognized file errors.append(gettext('Unrecognized model file format')) os.remove(os.path.join(app_dir, 'static/models/building_models', data_file)) try: shutil.rmtree(os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension'])) #delete unzipped folder except: pass return render_template("users/add_building_model.html", errors=errors), 400 if file_type == 'obj': # important file is obj inside a zipped => objmtl file_type = 'objmtl' addition_information = {'original_filename': important_file_name, 'directory': uploaded_file['filename_without_extension'], 'camera_x': 30, 'camera_y': 250, 'camera_z': 350, "camera_lookat_x": 31, "camera_lookat_y": 222, "camera_lookat_z": 366} else: file_type = uploaded_file['extension'] addition_information = {'original_filename': uploaded_file['filename_without_extension'], 'directory': '', 'camera_x': 30, 'camera_y': 250, 'camera_z': 350, "camera_lookat_x": 31, "camera_lookat_y": 222, "camera_lookat_z": 366} building_model = BuildingModel(name, data_file, g.user, addition_information=addition_information) building_model.file_type = file_type building_model.addition_information = json.dumps(addition_information, ensure_ascii=False) db.session.add(building_model) db.session.commit() return redirect(url_for('sketchup.view_building_model', id=building_model.id))
def add_building_model(): errors = [] if request.method == 'GET': return render_template("users/add_building_model.html", errors=errors) else: addition_information = '' name = request.form.get('name', '') if name.strip() == '': errors.append(gettext('Scenario name is required')) if request.files['data_file'].filename == '': errors.append(gettext('Model file is required')) if len(errors) > 0: return render_template("users/add_building_model.html", errors=errors), 400 uploaded_file = upload_file(request.files['data_file'], 'static/models/building_models', file_type="model") data_file = uploaded_file['filename'] if uploaded_file['extension'] not in config.ALLOWED_FILE_TYPES: errors.append('Unrecognized model file format') try: os.remove( os.path.join(app_dir, 'static/models/building_models', data_file)) except: pass return render_template("users/add_building_model.html", errors=errors), 400 if os.stat( os.path.join(app_dir, 'static/models/building_models', data_file)).st_size >= 1024 * 1024 * 10: errors.append('File too big, max file size is 10MB') os.remove( os.path.join(app_dir, 'static/models/building_models', data_file)) return render_template("users/add_building_model.html", errors=errors), 400 #check if file type is zip => unzip it if uploaded_file['extension'] == 'zip': tmp = data_file zip_ref = zipfile.ZipFile( os.path.join(app_dir, 'static/models/building_models', data_file), 'r') zip_ref.extractall( os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension'])) zip_ref.close() #os.rename(os.path.join(app_dir, 'static/models/building_models', uploaded_file['original_filename']), os.path.join(app_dir, 'static/models/building_models', uploaded_file['filename_without_extension'])) zipped_dir = os.path.join( app_dir, 'static/models/building_models', uploaded_file['filename_without_extension']) #check if inside of the zip is a dir of contain file, if contain only 1 dir => move file from inside that dir outside tmp_files = os.listdir(zipped_dir) if len(tmp_files) == 1 and not os.path.isfile( os.path.join(zipped_dir, tmp_files[0])): #contain only 1 dir inside_files = os.listdir( os.path.join(zipped_dir, tmp_files[0])) new_name = utilities.generate_random_string(50) os.rename(os.path.join(zipped_dir, tmp_files[0]), os.path.join(zipped_dir, new_name)) for file in inside_files: try: shutil.move(os.path.join(zipped_dir, new_name, file), os.path.join(zipped_dir, file)) except: pass try: shutil.rmtree(os.path.join( zipped_dir, new_name)) #delete unzipped folder except: pass files = [ f for f in os.listdir(zipped_dir) if os.path.isfile(os.path.join(zipped_dir, f)) ] important_file_index = -1 important_file_name = '' file_index = -1 for file in files: filename_parts = file.split('.') current_filename = filename_parts[0] for i in range(1, len(filename_parts) - 1): current_filename += '.' + filename_parts[i] extension = filename_parts[len(filename_parts) - 1].lower() os.rename( os.path.join(zipped_dir, file), os.path.join(zipped_dir, current_filename + '.' + extension)) if important_file_index < 0: file_index += 1 if extension in ['dae', 'obj']: important_file_name = current_filename important_file_index = file_index file_type = extension if important_file_index < 0: # user uploaded unrecognized file errors.append(gettext('Unrecognized model file format')) os.remove( os.path.join(app_dir, 'static/models/building_models', data_file)) try: shutil.rmtree( os.path.join( app_dir, 'static/models/building_models', uploaded_file['filename_without_extension']) ) #delete unzipped folder except: pass return render_template("users/add_building_model.html", errors=errors), 400 if file_type == 'obj': # important file is obj inside a zipped => objmtl file_type = 'objmtl' addition_information = { 'original_filename': important_file_name, 'directory': uploaded_file['filename_without_extension'], 'camera_x': 30, 'camera_y': 250, 'camera_z': 350, "camera_lookat_x": 31, "camera_lookat_y": 222, "camera_lookat_z": 366 } else: file_type = uploaded_file['extension'] addition_information = { 'original_filename': uploaded_file['filename_without_extension'], 'directory': '', 'camera_x': 30, 'camera_y': 250, 'camera_z': 350, "camera_lookat_x": 31, "camera_lookat_y": 222, "camera_lookat_z": 366 } building_model = BuildingModel( name, data_file, g.user, addition_information=addition_information) building_model.file_type = file_type building_model.addition_information = json.dumps(addition_information, ensure_ascii=False) db.session.add(building_model) db.session.commit() return redirect( url_for('sketchup.view_building_model', id=building_model.id))