def create_token(args): from mldatahub.config.privileges import Privileges privileges = Privileges.CREATE_DATASET + Privileges.EDIT_DATASET + Privileges.DESTROY_DATASET + \ Privileges.ADD_ELEMENTS + Privileges.EDIT_ELEMENTS + Privileges.DESTROY_ELEMENTS + \ Privileges.RO_WATCH_DATASET + Privileges.USER_EDIT_TOKEN parser = argparse.ArgumentParser(description="ML Data Hub [--create-token]") parser.add_argument("namespace", type=str, help="Namespace for the token") parser.add_argument("description", type=str, help="Description of the token") parser.add_argument("--maxds", type=int, help="Maximum number of datasets for this token", default=50) parser.add_argument("--maxl", type=int, help="Maximum number of elements for this token", default=10000000) parser.add_argument("--token-duration-days", type=int, dest="token_duration_days", help="Number of days that this token will last active", default=36500) parser.add_argument("--privileges", type=int, help="Privileges for this token", default=privileges) args = parser.parse_args(args) duration_in_days = args.token_duration_days token = __create_token__(args.namespace, args.description, args.maxds, args.maxl, args.privileges, duration_in_days) global_config.get_session().flush() print("*****************************************") print("Characteristics of token") print("*****************************************") print("NAMESPACE: {}".format(args.namespace)) print("DESCRIPTION: {}".format(args.description)) print("MAX DATASETS: {}".format(args.maxds)) print("MAX ELEMENTS PER DATASET: {}".format(args.maxl)) print("PRIVILEGES: {}".format(privileges)) print("END_DATE: {} ({} days remaining)".format(token.end_date, duration_in_days)) print("*****************************************") print("TOKEN:", token.token_gui)
def test_storage_removes_files(self): """ Tests that the storage is able to remove files. :return: """ storage = MongoStorage() content = b"hi" file = FileContentDAO(content=content, size=len(content)) global_config.get_session().flush() self.assertEqual(len(storage), 1) storage.delete_file(file._id) self.assertEqual(len(storage), 0)
def __init__(self): super().__init__() self.get_parser = reqparse.RequestParser() self.delete_parser = self.get_parser self.post_parser = reqparse.RequestParser() self.session = global_config.get_session() arguments = { "token_gui": { "type": str, "required": True, "help": "Global Unique Identifier of the token is required.", "location": "json" } } for argument, kwargs in arguments.items(): self.get_parser.add_argument(argument, **kwargs) arguments = { "description": { "type": str, "help": "Description for the token.", "location": "json" }, "max_dataset_count": { "type": int, "help": "Max number of datasets that it is allowed to create this token.", "location": "json" }, "max_dataset_size": { "type": int, "help": "Max size for a dataset created by this token", "location": "json" }, "end_date": { "type": str, "help": "End date for this token, in format {}".format(now()), "location": "json" }, "privileges": { "type": int, "help": "Privileges integer for this token", "location": "json" }, "token_gui": { "type": str, "help": "Token GUI, if not specified a new one is generated.", "location": "json" }, "url_prefix": { "type": str, "help": "Token URL prefix.", "location": "json" }, } for argument, kwargs in arguments.items(): self.post_parser.add_argument(argument, **kwargs)
def setUp(self): self.session = global_config.get_session() DatasetDAO.query.remove() DatasetCommentDAO.query.remove() DatasetElementDAO.query.remove() DatasetElementCommentDAO.query.remove() TokenDAO.query.remove()
def setUp(self): self.session = global_config.get_session() purge_database() privileges = Privileges.CREATE_DATASET + Privileges.EDIT_DATASET + Privileges.DESTROY_DATASET + \ Privileges.ADD_ELEMENTS + Privileges.EDIT_ELEMENTS + Privileges.DESTROY_ELEMENTS + \ Privileges.RO_WATCH_DATASET + Privileges.USER_EDIT_TOKEN self.token_guid = __create_token__("example", "", 100, 100000, privileges, 1000).token_gui
def __init__(self): super().__init__() self.get_parser = reqparse.RequestParser() self.get_parser.add_argument("elements", type=list, required=True, location="json", help="List of IDs to retrieve.") self.post_parser = self.get_parser self.patch_parser = self.get_parser self.delete_parser = self.get_parser self.session = global_config.get_session()
def __create_token__(namespace, description, max_datasets, max_elements_per_dataset, privileges, duration_in_days, end_date=None): illegal_chars = "/*;:,.ç´`+Ǩ^><¿?'¡¿!\"·$%&()@~¬" if any([i in namespace for i in illegal_chars]): print("[ERROR] The namespace can't hold any of the following chars:\n\"{}\"".format(illegal_chars)) exit(-1) from mldatahub.odm.dataset_dao import DatasetDAO from mldatahub.odm.token_dao import TokenDAO if end_date is not None: end_date = end_date else: end_date = now()+relativedelta(days=+duration_in_days) token = TokenDAO(description=description, max_dataset_count=max_datasets, max_dataset_size=max_elements_per_dataset, url_prefix=namespace, end_date=end_date, privileges=privileges) global_config.get_session().flush() return token
def __init__(self): super().__init__() self.post_parser = reqparse.RequestParser() self.session = global_config.get_session() arguments = { "url_prefix": { "type": str, "required": True, "help": "URL prefix for this dataset. Characters \"{}\" not allowed". format(DatasetFactory.illegal_chars), "location": "json" }, "title": { "type": str, "required": True, "help": "Title for the dataset.", "location": "json" }, "description": { "type": str, "required": True, "help": "Description for the dataset.", "location": "json" }, "reference": { "type": str, "required": True, "help": "Reference data (perhaps a Bibtex in string format?)", "location": "json" }, "tags": { "type": list, "required": False, "help": "Tags for the dataset (ease the searches for this dataset).", "location": "json" }, "options": { "type": dict, "required": False, "location": "json", "help": "options string" } } for argument, kwargs in arguments.items(): self.post_parser.add_argument(argument, **kwargs)
def __init__(self): super().__init__() self.get_parser = reqparse.RequestParser() self.get_parser.add_argument("url_prefix", type=str, required=False, help="URL prefix to get tokens from.") self.post_parser = reqparse.RequestParser() self.session = global_config.get_session() arguments = { "description": { "type": str, "required": True, "help": "Description for the dataset.", "location": "json" }, "max_dataset_count": { "type": int, "required": True, "help": "Max number of datasets that it is allowed to create this token.", "location": "json" }, "max_dataset_size": { "type": int, "required": True, "help": "Max size for a dataset created by this token", "location": "json" }, "end_date": { "type": str, "help": "End date for this token, in format {}".format(now()), "location": "json" }, "privileges": { "type": int, "required": True, "help": "Privileges integer for this token", "location": "json" }, "url_prefix": { "type": str, "help": "Token URL prefix.", "location": "json" }, } for argument, kwargs in arguments.items(): self.post_parser.add_argument(argument, **kwargs)
def __init__(self): super().__init__() self.session = global_config.get_session() self.get_parser = reqparse.RequestParser() arguments = { "elements": { "type": list, "required": True, "help": "List of element ids to retrieve content from (limited to {}).".format(global_config.get_page_size()), "location": "json" }, } for argument, kwargs in arguments.items(): self.get_parser.add_argument(argument, **kwargs)
def __init__(self): super().__init__() self.get_parser = reqparse.RequestParser() self.get_parser.add_argument("page", type=int, required=False, help="Page number to retrieve.", default=0) self.get_parser.add_argument("page-size", type=int, required=False, help="Size of the page to retrieve.", default=global_config.get_page_size()) self.get_parser.add_argument("elements", type=list, required=False, location="json", help="List of IDs to retrieve. Overrides the page attribute") self.get_parser.add_argument("options", type=dict, required=False, location="json", help="options string") self.post_parser = reqparse.RequestParser() self.session = global_config.get_session() arguments = { "title": { "type": str, "required": True, "help": "Title for the dataset.", "location": "json" }, "description": { "type": str, "required": True, "help": "Description for the dataset.", "location": "json" }, "http_ref": { "type": str, "required": False, "help": "Reference data (perhaps a Bibtex in string format?)", "location": "json" }, "tags": { "type": list, "required": False, "help": "Tags for the dataset (ease the searches for this dataset).", "location": "json" }, } for argument, kwargs in arguments.items(): self.post_parser.add_argument(argument, **kwargs)
def __init__(self): super().__init__() self.patch_parser = reqparse.RequestParser() self.session = global_config.get_session() arguments = { "title": { "type": str, "required": False, "help": "Title for the element", "location": "json" }, "description": { "type": str, "required": False, "help": "Description of the element.", "location": "json" }, "http_ref": { "type": str, "required": False, "help": "HTTP link to the resource (if any).", "location": "json" }, "tags": { "type": list, "required": False, "help": "Tags for the dataset (ease the searches for this dataset).", "location": "json" }, } for argument, kwargs in arguments.items(): self.patch_parser.add_argument(argument, **kwargs)
def __init__(self): super().__init__() self.session = global_config.get_session()
if token.end_date < now(): abort(410) return args, token class TokenizedResource(Resource): def __init__(self): super().__init__() self.token_parser = TokenizedRequestParser() self.token_parser.add_token_request() MAX_ACCESS_TIMES = global_config.get_max_access_times() ACCESS_RESET_TIME = global_config.get_access_reset_time() session = global_config.get_session() def control_access(): def func_wrap(func): def args_wrap(*args, **kwargs): remote_ip = request.remote_addr ip_control = RestAPIDAO.query.get(ip=remote_ip) if ip_control is None: ip_control = RestAPIDAO(ip=remote_ip, last_access=now(), num_accesses=0) if (now() - ip_control.last_access
def __init__(self, token): self.token = token self.session = global_config.get_session()
def __init__(self): self.session = global_config.get_session()
def setUp(self): self.session = global_config.get_session()
def __init__(self): super().__init__() self.get_parser = reqparse.RequestParser() self.session = global_config.get_session()