def grab_repository_metadata(): try: res_type = request.json["res_type"] res_name = request.json["res_name"] manager = get_manager_for_type(res_type, is_repository=True) mdata = manager.grab_metadata(res_name) if mdata is None: return jsonify({ "success": False, "message": "No repository metadata found", "alert_type": "alert-warning" }) else: if "datetime" in mdata: datestring = current_user.get_timestrings(mdata["datetime"])[0] else: datestring = "" return jsonify({ "success": True, "res_name": res_name, "datestring": datestring, "tags": mdata["tags"], "notes": mdata["notes"] }) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error getting repository metadata")
def overwrite_common_tags(): try: res_type = request.json["res_type"] res_names = request.json["res_names"] tags = request.json["tags"].split() manager = get_manager_for_type(res_type) common_tags = grab_m_mdata(res_type, res_names)["common_tags"].split() updated_tags = {} for res_name in res_names: mdata = manager.grab_metadata(res_name) old_tags = mdata["tags"].split() new_tags = [] for tag in old_tags: if tag not in common_tags and tag not in tags: new_tags.append(tag) new_tags += tags new_tags_string = " ".join(new_tags) updated_tags[res_name] = new_tags_string manager.save_metadata(res_name, new_tags_string, mdata["notes"]) res_tags = manager.get_tag_list() return jsonify({ "success": True, "res_tags": res_tags, "updated_tags": updated_tags, "message": "Saved metadata", "alert_type": "alert-success" }) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error saving metadata")
def add_tags(): try: res_type = request.json["res_type"] res_names = request.json["res_names"] tags = request.json["tags"] manager = get_manager_for_type(res_type) updated_tags = {} for res_name in res_names: mdata = manager.grab_metadata(res_name) old_tags = mdata["tags"].split() new_tags = list(set(old_tags + tags)) new_tags_string = " ".join(new_tags) updated_tags[res_name] = new_tags_string manager.save_metadata(res_name, new_tags_string, mdata["notes"]) res_tags = manager.get_tag_list() return jsonify({ "success": True, "res_tags": res_tags, "updated_tags": updated_tags, "message": "Saved metadata", "alert_type": "alert-success" }) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error adding tags")
def get_tag_list(): try: res_type = request.json["res_type"] is_repository = request.json["is_repository"] manager = get_manager_for_type(res_type, is_repository=is_repository) tag_list = manager.get_tag_list() return jsonify({"success": True, "tag_list": tag_list}) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error getting tag list")
def grab_multi_metadata(): try: res_type = request.json["res_type"] res_name_list = request.json["res_name_list"] is_repository = request.json["is_repository"] result_dict = grab_m_mdata(res_type, res_name_list, is_repository) return jsonify(result_dict) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error getting metadata")
def delete_tag(): try: res_type = request.json["res_type"] tag = request.json["tag"] manager = get_manager_for_type(res_type) manager.delete_tag(tag) return jsonify({ "success": True, "message": "Deleted tag", "alert_type": "alert-success" }) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error deleting a tag")
def rename_tag(): try: res_type = request.json["res_type"] tag_changes = request.json["tag_changes"] manager = get_manager_for_type(res_type) manager.rename_tag(tag_changes) res_tags = manager.get_tag_list() return jsonify({ "success": True, "res_tags": res_tags, "message": "renamed tag tag", "alert_type": "alert-success" }) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error renaming a tag")
def copy_between_accounts(source_user, dest_user, res_type, new_res_name, res_name): try: name_field = name_keys[res_type] collection_name = source_user.resource_collection_name(res_type) old_dict = db[collection_name].find_one({name_field: res_name}) new_res_dict = {name_field: new_res_name} for (key, val) in old_dict.items(): if (key == "_id") or (key == name_field): continue new_res_dict[key] = val if "metadata" not in new_res_dict: mdata = { "datetime": datetime.datetime.utcnow(), "updated": datetime.datetime.utcnow(), "tags": "", "notes": "" } new_res_dict["metadata"] = mdata else: new_res_dict["metadata"]["datetime"] = datetime.datetime.utcnow() project_dict = read_project_dict(fs, old_dict["file_id"]) project_dict["user_id"] = dest_user.get_id() pdict = make_jsonizable_and_compress(project_dict) new_res_dict["file_id"] = fs.put(pdict) new_collection_name = dest_user.resource_collection_name(res_type) db[new_collection_name].insert_one(new_res_dict) metadata = new_res_dict["metadata"] overall_res = [ metadata, jsonify({ "success": True, "message": "Resource Successfully Copied", "alert_type": "alert-success" }) ] return overall_res except Exception as ex: overall_res = [ None, generic_exception_handler.get_exception_for_ajax( ex, "Error copying resource") ] return overall_res
def save_metadata(): try: res_type = request.json["res_type"] res_name = request.json["res_name"] tags = request.json["tags"] notes = request.json["notes"] # module_id = request.json["module_id"] manager = get_manager_for_type(res_type) manager.save_metadata(res_name, tags, notes) res_tags = manager.get_tag_list() return jsonify({ "success": True, "res_tags": res_tags, "message": "Saved metadata", "alert_type": "alert-success" }) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error saving metadata")
def grab_metadata(): try: res_type = request.json["res_type"] res_name = request.json["res_name"] is_repository = request.json["is_repository"] manager = get_manager_for_type(res_type, is_repository=is_repository) mdata = manager.grab_metadata(res_name) if mdata is None: return jsonify({ "success": False, "message": "No metadata found", "alert_type": "alert-warning" }) else: if "datetime" in mdata: datestring = current_user.get_timestrings(mdata["datetime"])[0] else: datestring = "" additional_mdata = copy.copy(mdata) standard_mdata = ["datetime", "tags", "notes", "_id", "name"] for field in standard_mdata: if field in additional_mdata: del additional_mdata[field] if "updated" in additional_mdata: additional_mdata["updated"] = current_user.get_timestrings( additional_mdata["updated"])[0] if "collection_name" in additional_mdata: additional_mdata[ "collection_name"] = current_user.short_collection_name( additional_mdata["collection_name"]) return jsonify({ "success": True, "res_name": res_name, "datestring": datestring, "tags": mdata["tags"], "notes": mdata["notes"], "additional_mdata": additional_mdata }) except Exception as ex: return generic_exception_handler.get_exception_for_ajax( ex, "Error getting metadata")