def test_get_pid_by_ref_link(app): """Test resolving PID by the given reference link.""" with pytest.raises(Exception) as e: SonarRecord.get_pid_by_ref_link('falsy-link') assert str(e.value) == 'falsy-link is not a valid ref link' link = url_for('invenio_records_rest.doc_item', _external=True, pid_value='10000') pid = SonarRecord.get_pid_by_ref_link(link) assert pid == '10000'
def update_oai_property(sender, record): """Called when a document is created or updated. Update `_oai` property of the record. :param sender: Sender :param record: Document record """ if not isinstance(record, DocumentRecord): return sets = [] for organisation in record.get('organisation', []): sets.append(SonarRecord.get_pid_by_ref_link(organisation['$ref'])) record['_oai'].update({ 'updated': pytz.utc.localize(datetime.utcnow()).isoformat(), 'sets': sets }) # Store the value in `json` property, as it's not more called during object # creation. https://github.com/inveniosoftware/invenio-records/commit/ab7fdc10ddf54249dde8bc968f98b1fdd633610f#diff-51263e1ef21bcc060a5163632df055ef67ac3e3b2e222930649c13865cffa5aeR171 record.model.json = record.model_cls.encode(dict(record))
def has_external_urls_for_files(record): """Check if files point to external website. :param record: Current record. :returns: True if record's organisation is configured to point files to an external URL. """ for organisation in record.get('organisation', []): organisation_pid = SonarRecord.get_pid_by_ref_link( organisation['$ref']) if organisation.get( '$ref') else organisation['pid'] return organisation_pid in current_app.config.get( 'SONAR_DOCUMENTS_ORGANISATIONS_EXTERNAL_FILES') return False
def update_oai_property(sender, record): """Called when a document is created or updated. Update `_oai` property of the record. :param sender: Sender :param record: Document record """ if not isinstance(record, DocumentRecord): return record['_oai']['updated'] = pytz.utc.localize( datetime.utcnow()).isoformat() record['_oai']['sets'] = [ SonarRecord.get_pid_by_ref_link(record['organisation']['$ref']) ] if record.get('organisation') else []