def reindex(): """Process all claims to rebuild the eqid index.""" if click.confirm('Are you sure to reindex eqid?'): EquivalentIdentifier.rebuild() click.echo('Index rebuilt.') else: click.echo('Command aborted')
def drop_eqid(): """Delete all the entries in the eqid index.""" if click.confirm('Are you sure to drop the whole index?'): EquivalentIdentifier.clear() click.echo('Index cleared.') else: click.echo('Command aborted')
def reindex(): """Process all claims to rebuild the eqid index.""" if click.confirm("Are you sure to reindex eqid?"): EquivalentIdentifier.rebuild() click.echo("Index rebuilt.") else: click.echo("Command aborted")
def drop_eqid(): """Delete all the entries in the eqid index.""" if click.confirm("Are you sure to drop the whole index?"): EquivalentIdentifier.clear() click.echo("Index cleared.") else: click.echo("Command aborted")
def post(self): """Record a new claim. .. http:post:: /api/claims This resource is expecting JSON data with all the necessary information of a new claim. **Request**: .. sourcecode:: http POST /api/claims HTTP/1.1 Accept: application/json Content-Length: 336 Content-Type: application/json { "arguments": { "actor": "CDS_submission", "human": 0 }, "certainty": 1.0, "claimant": "CDS", "created": "2015-03-25T11:00:00Z", "object": { "type": "CDS_REPORT_NUMBER", "value": "CMS-PAS-HIG-14-008" }, "predicate": "is_same_as", "subject": { "type": "CDS_RECORD_ID", "value": "2003192" } } :reqheader Content-Type: application/json :json body: JSON with the information of the claimt. The JSON data should be valid according to the `JSON Schema for claims <https://goo.gl/C1f6vw>`_. **Responses**: .. sourcecode:: http HTTP/1.0 200 OK Content-Length: 80 Content-Type: application/json { "status": "success", "uuid": "fad4ec9f-0e95-4a22-b65c-d01f15aba6be" } .. sourcecode:: http HTTP/1.0 400 BAD REQUEST Content-Length: 9616 Content-Type: application/json Date: Tue, 22 Sep 2015 09:02:25 GMT Server: Werkzeug/0.10.4 Python/3.4.3 { "extra": "'claimant' is a required property. Failed validating 'required' in schema...", "message": "JSON data is not valid", "status": 400 } :resheader Content-Type: application/json :statuscode 200: no error - the claim was recorded :statuscode 400: invalid request - problably a malformed JSON :statuscode 403: access denied .. see docs/users.rst for usage documenation. """ json_data = request.get_json() self.validate_json(json_data) try: if not json_data['created'].endswith('Z'): raise InvalidJSONData('Claim\'s `creation datetime` must have ' 'timezone information and must be UTC') created_dt = isodate.parse_datetime(json_data['created']) except isodate.ISO8601Error as e: raise InvalidJSONData( 'Claim\'s `creation datetime` does not follow ISO 8601 Z', extra=str(e) ) claimant = Claimant.query.filter_by(name=json_data['claimant']).first() if not claimant: raise InvalidRequest('Claimant not registered') subject_type = IdentifierType.query.filter_by( name=json_data['subject']['type'] ).first() if not subject_type: raise InvalidRequest('Subject Type not registered') object_type = IdentifierType.query.filter_by( name=json_data['object']['type'] ).first() if not object_type: raise InvalidRequest('Object Type not registered') if subject_type.id == object_type.id: raise InvalidRequest('Subject and Object cannot have the same \ identifier type') predicate = Predicate.query.filter_by( name=json_data['predicate'] ).first() if not predicate: raise InvalidRequest('Predicate not registered') subject_eqid, object_eqid = None, None if json_data['predicate'] in \ current_app.config['CFG_EQUIVALENT_PREDICATES']: subject_eqid, object_eqid = EquivalentIdentifier.set_equivalent_id( subject_type.id, json_data['subject']['value'], object_type.id, json_data['object']['value'] ) arguments = json_data.get('arguments', {}) new_claim = Claim( created=created_dt, claimant=claimant, subject_type_id=subject_type.id, subject_value=json_data['subject']['value'], subject_eqid=subject_eqid.id if subject_eqid else None, predicate_id=predicate.id, object_type_id=object_type.id, object_value=json_data['object']['value'], object_eqid=object_eqid.id if object_eqid else None, certainty=json_data['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 {'status': 'success', 'uuid': new_claim.uuid}
def post(self): """Record a new claim. .. http:post:: /api/claims This resource is expecting JSON data with all the necessary information of a new claim. **Request**: .. sourcecode:: http POST /api/claims HTTP/1.1 Accept: application/json Content-Length: 336 Content-Type: application/json { "arguments": { "actor": "CDS_submission", "human": 0 }, "certainty": 1.0, "claimant": "CDS", "created": "2015-03-25T11:00:00Z", "object": { "type": "CDS_REPORT_NUMBER", "value": "CMS-PAS-HIG-14-008" }, "predicate": "is_same_as", "subject": { "type": "CDS_RECORD_ID", "value": "2003192" } } :reqheader Content-Type: application/json :json body: JSON with the information of the claimt. The JSON data should be valid according to the `JSON Schema for claims <https://goo.gl/C1f6vw>`_. **Responses**: .. sourcecode:: http HTTP/1.0 200 OK Content-Length: 80 Content-Type: application/json { "status": "success", "uuid": "fad4ec9f-0e95-4a22-b65c-d01f15aba6be" } .. sourcecode:: http HTTP/1.0 400 BAD REQUEST Content-Length: 9616 Content-Type: application/json Date: Tue, 22 Sep 2015 09:02:25 GMT Server: Werkzeug/0.10.4 Python/3.4.3 { "extra": "'claimant' is a required property. Failed validating 'required' in schema...", "message": "JSON data is not valid", "status": 400 } :resheader Content-Type: application/json :statuscode 200: no error - the claim was recorded :statuscode 400: invalid request - problably a malformed JSON :statuscode 403: access denied .. see docs/users.rst for usage documenation. """ json_data = request.get_json() self.validate_json(json_data) try: if not json_data['created'].endswith('Z'): raise InvalidJSONData('Claim\'s `creation datetime` must have ' 'timezone information and must be UTC') created_dt = isodate.parse_datetime(json_data['created']) except isodate.ISO8601Error as e: raise InvalidJSONData( 'Claim\'s `creation datetime` does not follow ISO 8601 Z', extra=str(e)) claimant = Claimant.query.filter_by(name=json_data['claimant']).first() if not claimant: raise InvalidRequest('Claimant not registered') subject_type = IdentifierType.query.filter_by( name=json_data['subject']['type']).first() if not subject_type: raise InvalidRequest('Subject Type not registered') object_type = IdentifierType.query.filter_by( name=json_data['object']['type']).first() if not object_type: raise InvalidRequest('Object Type not registered') if subject_type.id == object_type.id: raise InvalidRequest('Subject and Object cannot have the same \ identifier type') predicate = Predicate.query.filter_by( name=json_data['predicate']).first() if not predicate: raise InvalidRequest('Predicate not registered') subject_eqid, object_eqid = None, None if json_data['predicate'] in \ current_app.config['CFG_EQUIVALENT_PREDICATES']: subject_eqid, object_eqid = EquivalentIdentifier.set_equivalent_id( subject_type.id, json_data['subject']['value'], object_type.id, json_data['object']['value']) arguments = json_data.get('arguments', {}) new_claim = Claim( created=created_dt, claimant=claimant, subject_type_id=subject_type.id, subject_value=json_data['subject']['value'], subject_eqid=subject_eqid.id if subject_eqid else None, predicate_id=predicate.id, object_type_id=object_type.id, object_value=json_data['object']['value'], object_eqid=object_eqid.id if object_eqid else None, certainty=json_data['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 {'status': 'success', 'uuid': new_claim.uuid}