def export_project(project_id): """Export a zipped project file""" Path(asreview_path(), "tmp").mkdir(exist_ok=True) shutil.make_archive( Path(asreview_path(), f"tmp/export_{project_id}"), "zip", get_project_path(project_id) ) fp_tmp_export = Path(asreview_path(), f"tmp/export_{project_id}.zip") return send_file( fp_tmp_export, as_attachment=True, attachment_filename=f"{project_id}.asreview.zip", cache_timeout=0 )
def init_project(project_id, project_name=None, project_description=None, project_authors=None): """Initialize the necessary files specific to the web app.""" if not project_id and not isinstance(project_id, str) \ and len(project_id) >= 3: raise ValueError("Project name can't be None or empty string") # get the directory with the projects project_dir = asreview_path() / project_id if project_dir.exists(): raise ValueError("Project already exists") try: project_dir.mkdir() fp_data = project_dir / "data" fp_data.mkdir() # create a file with project info with open(get_project_file_path(project_id), "w") as fp: json.dump( { 'version': asreview_version, # todo: Fail without git? 'id': project_id, 'name': project_name, 'description': project_description, 'authors': project_authors }, fp) asr_kwargs = deepcopy(app.config['asr_kwargs']) # remove config with open(get_kwargs_path(project_id), "w") as fp: json.dump(asr_kwargs, fp) # make a copy of the arguments to the state file asr_kwargs['state_file'] = str(get_state_path(project_id)) except Exception as err: # remove all generated folders and raise error shutil.rmtree(project_dir) raise err
def init_project(project_id, project_name=None, project_description=None, project_authors=None): """Initialize the necessary files specific to the web app.""" if not project_id and not isinstance(project_id, str) \ and len(project_id) >= 3: raise ValueError("Project name can't be None or empty string") # get the directory with the projects project_dir = asreview_path() / project_id if project_dir.exists(): raise ValueError("Project already exists") try: project_dir.mkdir() fp_data = project_dir / "data" fp_data.mkdir() project_config = { 'version': asreview_version, # todo: Fail without git? 'id': project_id, 'name': project_name, 'description': project_description, 'authors': project_authors, # project related variables 'projectInitReady': False, 'reviewFinished': False, } # create a file with project info with open(get_project_file_path(project_id), "w") as fp: json.dump(project_config, fp) return project_config except Exception as err: # remove all generated folders and raise error shutil.rmtree(project_dir) raise err
def update_project_info(project_id, project_name=None, project_description=None, project_authors=None): '''Update project info''' project_id_new = re.sub('[^A-Za-z0-9]+', '-', project_name).lower() if not project_id_new and not isinstance(project_id_new, str) \ and len(project_id_new) >= 3: raise ValueError("Project name should be at least 3 characters.") if (project_id != project_id_new) & is_project(project_id_new): raise ValueError("Project name already exists.") try: # read the file with project info with open(get_project_file_path(project_id), "r") as fp: project_info = json.load(fp) project_info["id"] = project_id_new project_info["name"] = project_name project_info["authors"] = project_authors project_info["description"] = project_description # # backwards support <0.10 # if "projectInitReady" not in project_info: # project_info["projectInitReady"] = True # update the file with project info with open(get_project_file_path(project_id), "w") as fp: json.dump(project_info, fp) # rename the folder get_project_path(project_id) \ .rename(Path(asreview_path(), project_id_new)) except Exception as err: raise err return project_info["id"]
def api_update_project_info(project_id): # noqa: F401 """Get info on the article""" logging.info("Update project info") project_name = request.form['name'] project_description = request.form['description'] project_authors = request.form['authors'] project_id_new = re.sub('[^A-Za-z0-9]+', '-', project_name).lower() try: # read the file with project info with open(get_project_file_path(project_id), "r") as fp: project_info = json.load(fp) project_info["id"] = project_id_new project_info["name"] = project_name project_info["authors"] = project_authors project_info["description"] = project_description # # backwards support <0.10 # if "projectInitReady" not in project_info: # project_info["projectInitReady"] = True # update the file with project info with open(get_project_file_path(project_id), "w") as fp: json.dump(project_info, fp) # rename the folder get_project_path(project_id) \ .rename(Path(asreview_path(), project_id_new)) except Exception as err: logging.error(err) response = jsonify(message="project-update-failure") return response, 500 return api_get_project_info(project_id_new)
def update_project_info(project_id, project_name=None, project_description=None, project_authors=None): '''Update project info''' project_id_new = create_project_id(project_name) if (project_id != project_id_new) & is_project(project_id_new): raise ValueError("Project name already exists.") try: # read the file with project info with open(get_project_file_path(project_id), "r") as fp: project_info = json.load(fp) project_info["id"] = project_id_new project_info["name"] = project_name project_info["authors"] = project_authors project_info["description"] = project_description # # backwards support <0.10 # if "projectInitReady" not in project_info: # project_info["projectInitReady"] = True # update the file with project info with open(get_project_file_path(project_id), "w") as fp: json.dump(project_info, fp) # rename the folder get_project_path(project_id) \ .rename(Path(asreview_path(), project_id_new)) except Exception as err: raise err return project_info["id"]
def api_import_project(): """Import uploaded project""" import_project_id = None if 'file' in request.files: project_file = request.files['file'] filename = secure_filename(project_file.filename) try: with zipfile.ZipFile(project_file, "r") as zipObj: FileNames = zipObj.namelist() # check if the zip file contains a ASReview project if sum([fn.endswith("project.json") for fn in FileNames]) == 1: # extract all files to a temporary folder tmpdir = tempfile.TemporaryDirectory() zipObj.extractall(path=tmpdir.name) for fn in FileNames: if fn.endswith("project.json"): fp = Path(tmpdir.name, fn) with open(fp, "r+") as f: project = json.load(f) # if the uploaded project already exists, # then make a copy if is_project(project["id"]): project["id"] += " copy" project["name"] += " copy" f.seek(0) json.dump(project, f) f.truncate() else: response = jsonify( message="No project found within the chosen file.") return response, 404 try: # check if a copy of a project already exists os.rename(tmpdir.name, asreview_path() / f"{project['id']}") import_project_id = project['id'] except Exception as err: logging.error(err) response = jsonify( message=f"A copy of {project['id'][:-5]} already exists.") return response, 400 except Exception as err: logging.error(err) response = jsonify(message=f"Failed to upload file '{filename}'.") return response, 400 else: response = jsonify(message="No file found to upload.") return response, 400 # return the project info in the same format as project_info return api_get_project_info(import_project_id)
def is_project(project_id): project_path = asreview_path() / project_id / "project.json" return project_path.exists()
def api_import_project(): """Import uploaded project""" if 'file' in request.files: project_file = request.files['file'] filename = secure_filename(project_file.filename) # check the file format if not os.path.splitext(filename)[1] == ".asreview": response = jsonify(message="Incorrect file format.") return response, 400 try: with zipfile.ZipFile(project_file, "r") as zipObj: FileNames = zipObj.namelist() # check if the zip file contains a ASReview project if sum([fn.endswith("project.json") for fn in FileNames]) == 1: # extract all files to a temporary folder tmpdir = tempfile.TemporaryDirectory() zipObj.extractall(path=tmpdir.name) for fn in FileNames: if fn.endswith("project.json"): fp = Path(tmpdir.name, fn) with open(fp, "r+") as f: project = json.load(f) # if the uploaded project already exists, then make a copy if is_project(project["id"]): project["id"] += " copy" project["name"] += " copy" f.seek(0) json.dump(project, f) f.truncate() else: response = jsonify(message="No project found within the chosen file.") return response, 404 try: # check if a copy of a project already exists os.rename(tmpdir.name, asreview_path() / f"{project['id']}") except Exception as err: logging.error(err) response = jsonify(message=f"A copy of {project['id'][:-5]} already exists.") return response, 400 except Exception as err: logging.error(err) response = jsonify(message=f"Failed to upload file '{filename}'. {err}") return response, 400 else: response = jsonify(message="No file found to upload.") return response, 400 response = jsonify({'success': True}) response.headers.add('Access-Control-Allow-Origin', '*') return response