Skip to content

Commit

Permalink
Merge pull request #3 from oarepo/request_tests
Browse files Browse the repository at this point in the history
communities
  • Loading branch information
Alzpeta authored Aug 29, 2024
2 parents d4a1314 + b43b3ad commit 787d4a7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 40 deletions.
42 changes: 34 additions & 8 deletions oarepo_doi/actions/doi.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
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

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:
Expand All @@ -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:
Expand Down
29 changes: 22 additions & 7 deletions oarepo_doi/services/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ pip install -e ".[tests]"

pytest ./thesis/tests -vvv

pytest tests -vvv
pytest tests -vvv
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-doi
version = 1.0.1
version = 1.0.2
description =
authors = Alzbeta Pokorna <[email protected]>
readme = README.md
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 0 additions & 22 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
# )

0 comments on commit 787d4a7

Please sign in to comment.