def load_all_pids(config_path=None): """Populate all persistent identifiers.""" if config_path: pids_filepath = os.path.join( config_path, 'pids' ) else: pids_filepath = os.path.join( current_app.config['BASE_DIR'], 'tests', 'myclaimstore', 'config', 'pids' ) for pid_fp in glob.glob("{}/*.json".format(pids_filepath)): with open(pid_fp) as f: json_data = json.load(f) try: validate_json(json_data, 'claims.persistent_id') except ValidationError: print('`{}` could not be loaded. It does not follow the proper' ' JSON schema specification.'.format(pid_fp)) continue create_pid(json_data)
def load_all_claimants(config_path=None): """Fixture that loads all test claimants.""" if config_path: claimants_filepath = os.path.join( config_path, 'claimants' ) else: claimants_filepath = os.path.join( current_app.config['BASE_DIR'], 'tests', 'myclaimstore', 'config', 'claimants' ) for claimant_fp in glob.glob("{}/*.json".format(claimants_filepath)): with open(claimant_fp) as f: json_data = json.load(f) try: validate_json(json_data, 'claims.claimant') except ValidationError: print( '`{}` could not be loaded. It does not follow the proper ' 'JSON schema specification.'.format(claimant_fp) ) continue create_claimant(json_data)
def subscribe(): json_data = request.get_json() try: validate_json(json_data, "claims.claimant") except Exception as e: raise InvalidUsage("JSON data is not valid", details=str(e)) if not Claimant.query.filter_by(name=json_data["name"]).first(): new_claimant = Claimant(name=json_data["name"], url=json_data["url"]) db.session.add(new_claimant) for persistent_id in json_data["persistent_identifiers"]: all_caps_pers_id = persistent_id["type"].upper() existing_persistent_id = IdentifierType.query.filter_by(name=all_caps_pers_id).first() if not existing_persistent_id: new_persistent_id = IdentifierType( name=all_caps_pers_id, description=persistent_id["description"], url=persistent_id["url"], example_value=persistent_id["example_value"], example_url=persistent_id["example_url"], claimant_id=new_claimant.uid, ) db.session.add(new_persistent_id) db.session.commit() return jsonify({"status": "success", "uuid": new_claimant.uuid}) else: return jsonify({"status": "error", "message": "This claimant is already registered"}), 400
def subscribe(): json_data = request.get_json() try: validate_json(json_data, 'claims.claimant') except Exception as e: raise InvalidUsage('JSON data is not valid', details=str(e)) if not Claimant.query.filter_by(name=json_data['name']).first(): new_claimant = Claimant(name=json_data['name'], url=json_data['url']) db.session.add(new_claimant) for persistent_id in json_data['persistent_identifiers']: all_caps_pers_id = persistent_id['type'].upper() existing_persistent_id = IdentifierType.query.filter_by( name=all_caps_pers_id).first() if not existing_persistent_id: new_persistent_id = IdentifierType( name=all_caps_pers_id, description=persistent_id['description'], url=persistent_id['url'], example_value=persistent_id['example_value'], example_url=persistent_id['example_url'], claimant_id=new_claimant.uid) db.session.add(new_persistent_id) db.session.commit() return jsonify({'status': 'success', 'uuid': new_claimant.uuid}) else: return jsonify({ 'status': 'error', 'message': 'This claimant is already registered' }), 400
def validate_json(self, json_data): """Validate that json_data follows the appropiate JSON schema. :param json_data: JSON data to be validated. :raises: :exc:`InvalidJSONData` if the instance is invalid. """ try: validate_json(json_data, self.json_schema) except ValidationError as e: raise InvalidJSONData('JSON data is not valid', extra=str(e))
def submit_claim(): json_data = request.get_json() try: validate_json(json_data, "claims.claim") except Exception as e: raise InvalidUsage("JSON data is not valid", details=str(e)) claimant = Claimant.query.filter_by(name=json_data["claimant"]).first() if not claimant: return jsonify({"status": "error", "message": "Claimant not registered"}), 400 subject_type = IdentifierType.query.filter_by(name=json_data["subject"]["type"]).first() if not subject_type: return jsonify({"status": "error", "message": "Subject Type not registered"}), 400 object_type = IdentifierType.query.filter_by(name=json_data["object"]["type"]).first() if not object_type: return jsonify({"status": "error", "message": "Object Type not registered"}), 400 if subject_type.uid == object_type.uid: return ( jsonify( { "status": "error", "message": "Subject and Object cannot have the same identifier \ type", } ), 400, ) predicate = Predicate.query.filter_by(name=json_data["claim"]["predicate"]).first() if not predicate: return jsonify({"status": "error", "message": "IdentifierType not registered"}), 400 arguments = json_data["claim"].get("arguments", {}) new_claim = Claim( claimant_id=claimant.uid, subject_type_id=subject_type.uid, subject_value=json_data["subject"]["value"], predicate_id=predicate.uid, object_type_id=object_type.uid, object_value=json_data["object"]["value"], certainty=json_data["claim"]["certainty"], human=arguments.get("human", None), actor=arguments.get("actor", None), role=arguments.get("role", None), claim_details=json_data, ) db.session.add(new_claim) db.session.commit() return jsonify({"status": "success", "uuid": new_claim.uuid})
def load_all_pids(config_path=None): """Populate all persistent identifiers.""" if config_path: pids_filepath = os.path.join(config_path, 'pids') else: pids_filepath = os.path.join(current_app.config['BASE_DIR'], 'tests', 'myclaimstore', 'config', 'pids') for pid_fp in glob.glob("{}/*.json".format(pids_filepath)): with open(pid_fp) as f: json_data = json.load(f) try: validate_json(json_data, 'claims.persistent_id') except ValidationError: print('`{}` could not be loaded. It does not follow the proper' ' JSON schema specification.'.format(pid_fp)) continue create_pid(json_data)
def load_all_claimants(config_path=None): """Fixture that loads all test claimants.""" if config_path: claimants_filepath = os.path.join(config_path, 'claimants') else: claimants_filepath = os.path.join(current_app.config['BASE_DIR'], 'tests', 'myclaimstore', 'config', 'claimants') for claimant_fp in glob.glob("{}/*.json".format(claimants_filepath)): with open(claimant_fp) as f: json_data = json.load(f) try: validate_json(json_data, 'claims.claimant') except ValidationError: print( '`{}` could not be loaded. It does not follow the proper ' 'JSON schema specification.'.format(claimant_fp)) continue create_claimant(json_data)
def load_all_predicates(config_path=None): """Populate all predicates.""" if config_path: predicates_filepath = os.path.join(config_path, "predicates") else: predicates_filepath = os.path.join( current_app.config["BASE_DIR"], "tests", "myclaimstore", "config", "predicates" ) for pred_fp in glob.glob("{}/*.json".format(predicates_filepath)): with open(pred_fp) as f: json_data = json.load(f) try: validate_json(json_data, "claims.predicate") except ValidationError: print( "`{}` could not be loaded. It does not follow the proper" " JSON schema specification.".format(pred_fp) ) continue create_predicate(json_data)
def submit_claim(): json_data = request.get_json() try: validate_json(json_data, 'claims.claim') except Exception as e: raise InvalidUsage('JSON data is not valid', details=str(e)) claimant = Claimant.query.filter_by(name=json_data['claimant']).first() if not claimant: return jsonify({ 'status': 'error', 'message': 'Claimant not registered' }), 400 subject_type = IdentifierType.query.filter_by( name=json_data['subject']['type']).first() if not subject_type: return jsonify({ 'status': 'error', 'message': 'Subject Type not registered' }), 400 object_type = IdentifierType.query.filter_by( name=json_data['object']['type']).first() if not object_type: return jsonify({ 'status': 'error', 'message': 'Object Type not registered' }), 400 if subject_type.uid == object_type.uid: return jsonify({ 'status': 'error', 'message': 'Subject and Object cannot have the same identifier \ type' }), 400 predicate = Predicate.query.filter_by( name=json_data['claim']['predicate']).first() if not predicate: return jsonify({ 'status': 'error', 'message': 'IdentifierType not registered' }), 400 arguments = json_data['claim'].get('arguments', {}) new_claim = Claim( claimant_id=claimant.uid, subject_type_id=subject_type.uid, subject_value=json_data['subject']['value'], predicate_id=predicate.uid, object_type_id=object_type.uid, object_value=json_data['object']['value'], certainty=json_data['claim']['certainty'], human=arguments.get('human', None), actor=arguments.get('actor', None), role=arguments.get('role', None), claim_details=json_data, ) db.session.add(new_claim) db.session.commit() return jsonify({'status': 'success', 'uuid': new_claim.uuid})