diff --git a/oarepo_doi/actions/doi.py b/oarepo_doi/actions/doi.py index 80e1faa..49d225a 100644 --- a/oarepo_doi/actions/doi.py +++ b/oarepo_doi/actions/doi.py @@ -1,6 +1,6 @@ from invenio_requests.customizations import actions from flask import current_app -from oarepo_doi.api import create_doi +from oarepo_doi.api import create_doi, edit_doi class CreateDoiAction(actions.CreateAction): log_event = True @@ -8,15 +8,29 @@ class CreateDoiAction(actions.CreateAction): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.username = current_app.config.get("DATACITE_USERNAME") - self.password = current_app.config.get("DATACITE_PASSWORD") + self.mode = current_app.config.get("DATACITE_MODE") self.url = current_app.config.get("DATACITE_URL") - self.prefix = current_app.config.get("DATACITE_PREFIX") self.mapping = current_app.config.get("DATACITE_MAPPING") + self.username = None + self.password = None + self.prefix = None + + def credentials(self, community): + credentials_def = current_app.config.get("DATACITE_CREDENTIALS") + + community_credentials = getattr(credentials_def, community, None) + if community_credentials is None and "DATACITE_CREDENTIALS_DEFAULT" in current_app.config: + community_credentials = current_app.config.get("DATACITE_CREDENTIALS_DEFAULT") + self.username = community_credentials["username"] + self.password = community_credentials["password"] + self.prefix = community_credentials["prefix"] + def execute(self, identity, uow, *args, **kwargs): topic = self.request.topic.resolve() + self.credentials(topic['parent']['communities']['default']) + if topic.is_draft: create_doi(self, topic, topic["metadata"], None) else: @@ -30,16 +44,28 @@ class EditDoiAction(actions.AcceptAction): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.username = current_app.config.get("DATACITE_USERNAME") - self.password = current_app.config.get("DATACITE_PASSWORD") + self.mode = current_app.config.get("DATACITE_MODE") self.url = current_app.config.get("DATACITE_URL") - self.prefix = current_app.config.get("DATACITE_PREFIX") self.mapping = current_app.config.get("DATACITE_MAPPING") + self.username = None + self.password = None + self.prefix = None + + def credentials(self, community): + credentials_def = current_app.config.get("DATACITE_CREDENTIALS") + + community_credentials = getattr(credentials_def, community, None) + if community_credentials is None and "DATACITE_CREDENTIALS_DEFAULT" in current_app.config: + community_credentials = current_app.config.get("DATACITE_CREDENTIALS_DEFAULT") + self.username = community_credentials["username"] + self.password = community_credentials["password"] + self.prefix = community_credentials["prefix"] + def execute(self, identity, uow, *args, **kwargs): topic = self.request.topic.resolve() - + self.credentials(topic['parent']['communities']['default']) if topic.is_draft: edit_doi(self, topic, None) else: diff --git a/oarepo_doi/services/components/__init__.py b/oarepo_doi/services/components/__init__.py index 3d56fd9..59a6d50 100644 --- a/oarepo_doi/services/components/__init__.py +++ b/oarepo_doi/services/components/__init__.py @@ -13,30 +13,45 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.mode = current_app.config.get("DATACITE_MODE") - - #this will be changed after oarepo-communities release - self.username = current_app.config.get("DATACITE_USERNAME") - self.password = current_app.config.get("DATACITE_PASSWORD") self.url = current_app.config.get("DATACITE_URL") - self.prefix = current_app.config.get("DATACITE_PREFIX") - # - self.mapping = current_app.config.get("DATACITE_MAPPING") + self.username = None + self.password = None + self.prefix = None + + + + def credentials(self, community): + credentials_def = current_app.config.get("DATACITE_CREDENTIALS") + + community_credentials = getattr(credentials_def, community, None) + if community_credentials is None and "DATACITE_CREDENTIALS_DEFAULT" in current_app.config: + community_credentials = current_app.config.get("DATACITE_CREDENTIALS_DEFAULT") + self.username = community_credentials["username"] + self.password = community_credentials["password"] + self.prefix = community_credentials["prefix"] + def create(self, identity, data=None, record=None, **kwargs): + if self.mode == "AUTOMATIC_DRAFT": + self.credentials(data['parent']['communities']['default']) create_doi(self, record,data, None) def update_draft(self, identity, data=None, record=None, **kwargs): if self.mode == "AUTOMATIC_DRAFT": + self.credentials(data['parent']['communities']['default']) edit_doi(self, record) def update(self, identity, data=None, record=None, **kwargs): if self.mode == "AUTOMATIC_DRAFT" or self.mode == "AUTOMATIC": + self.credentials(data['parent']['communities']['default']) edit_doi(self, record) def publish(self, identity, data=None, record=None, **kwargs): if self.mode == "AUTOMATIC": + self.credentials(data['parent']['communities']['default']) create_doi(self, record, data, "publish") if self.mode == "AUTOMATIC_DRAFT": + self.credentials(data['parent']['communities']['default']) edit_doi(self, record, "publish") diff --git a/run-tests.sh b/run-tests.sh index 5552f09..70202ef 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -36,4 +36,4 @@ pip install -e ".[tests]" pytest ./thesis/tests -vvv -pytest tests -vvv +pytest tests -vvv \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 38a9bd2..a8885e2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-doi -version = 1.0.1 +version = 1.0.2 description = authors = Alzbeta Pokorna readme = README.md diff --git a/tests/conftest.py b/tests/conftest.py index c3164ab..a52f0e6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -209,4 +209,4 @@ def request_with_receiver_user( identity_creator, {}, type_, receiver=receiver, topic=example_topic ) request = Request.get_record(request_item.id) - return request_item + return request_item \ No newline at end of file diff --git a/tests/test_requests.py b/tests/test_requests.py index 0399c0c..7816f92 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -9,25 +9,3 @@ def test_datacite_config(app): assert "DATACITE_PREFIX" in app.config - - -# def test_request(app, client_with_login): -# with client_with_login.get(f"/thesis/") as c: -# assert c.status_code == 200 -# -# -# def test_create(app, db, record_service, sample_metadata_list, search_clear): -# created_records = [] -# for sample_metadata_point in sample_metadata_list: -# created_records.append( -# record_service.create(system_identity, sample_metadata_point) -# ) -# for sample_metadata_point, created_record in zip( -# sample_metadata_list, created_records -# ): -# created_record_reread = record_service.read( -# system_identity, created_record["id"] -# ) -# assert ( -# created_record_reread.data["metadata"] == sample_metadata_point["metadata"] -# )