Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

Dark archive protocol json metadata #856

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from django.conf import settings
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.management import call_command
from django.urls import reverse
Expand All @@ -30,6 +31,7 @@
from papers.models import Researcher
from publishers.models import Journal
from publishers.models import Publisher
from upload.models import UploadedPDF


@pytest.fixture
Expand Down Expand Up @@ -213,6 +215,16 @@ def blank_pdf(blank_pdf_path):
pdf = f.read()
return pdf

@pytest.fixture
def uploaded_pdf(user_leibniz, blank_pdf):
"""
A simple uploaded pdf of user leibniz.
"""
pdf = UploadedPDF.objects.create(
user=user_leibniz,
)
pdf.file.save('blank.pdf', ContentFile(blank_pdf))
return pdf

"""
Depending on the environment variable DISSEMIN_TEST_ALL_LANGAUAGES sets the languages to be tested. If not set, use english, otherwise all languages from settings.POSSIBLE_LANGUAGE_CODES
Expand Down
28 changes: 15 additions & 13 deletions deposit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def depositing_user(db, request, user_leibniz):
orcid=request.param,
)

user_leibniz.orcid = request.param

return user_leibniz


Expand Down Expand Up @@ -85,18 +87,6 @@ def embargo(request):
return request.param


@pytest.fixture
def dummy_journal(dummy_publisher):
"""
Empty Journal with FK to Publisher
"""
j = Journal.objects.create(
publisher=dummy_publisher,
)

return j


@pytest.fixture
def dummy_oairecord(dummy_paper, dummy_oaisource):
"""
Expand Down Expand Up @@ -133,6 +123,18 @@ def dummy_publisher():
return p


@pytest.fixture
def dummy_journal(dummy_publisher):
"""
Empty Journal with FK to Publisher
"""
j = Journal.objects.create(
publisher=dummy_publisher,
)

return j


@pytest.fixture
def empty_user_preferences(db, user_isaac_newton):
"""
Expand Down Expand Up @@ -163,7 +165,7 @@ def license_standard(db):
Returns a standard test license
"""

license = License.objects.get_or_create(
license = License.objects.create(
name="Standard License",
uri="https://dissem.in/deposit/license/standard"
)
Expand Down
Empty file added deposit/darkarchive/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions deposit/darkarchive/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest

from deposit.darkarchive.protocol import DarkArchiveProtocol
from deposit.models import Repository


@pytest.fixture
def dark_archive_repository(simple_logo, oaisource):
"""
Returns a repository for dark archive tests
"""
repository = Repository.objects.create(
name='Dark Archivce Test Repository',
description='Dark archive test repository',
logo=simple_logo,
protocol='DarkArchive',
oaisource=oaisource.dummy_oaisource(),
)

return repository


@pytest.fixture
def dark_archive_protocol(request, dark_archive_repository):
"""
Sets the dark archive protocol as protocol to be used
"""
request.cls.protocol = DarkArchiveProtocol(dark_archive_repository)
18 changes: 18 additions & 0 deletions deposit/darkarchive/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django import forms

from django.utils.translation import ugettext_lazy as _

from deposit.forms import BaseMetadataForm

class DarkArchiveForm(BaseMetadataForm):
"""
Form that extends BaseMetadataForm for extra fields needed by DarkArchive protocol
"""

field_order = ['email', 'abstract', 'license']

email = forms.EmailField(
label=_('E-mail'),
required=True,
max_length=255,
)
128 changes: 128 additions & 0 deletions deposit/darkarchive/protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from django.contrib.sites.models import Site

from deposit.darkarchive.forms import DarkArchiveForm
from deposit.protocol import RepositoryProtocol


class DarkArchiveProtocol(RepositoryProtocol):
"""
A protocol that does does directly sends data to a repository, but to an intermediata database
"""

# The class of the form the protocol uses
form_class = DarkArchiveForm

def __str__(self):
"""
Return human readable class name
"""
return "Dark Archive Protocol"


def _get_authors(self):
"""
Returns a list of authors, containing a dict with:
first
last
orcid
"""
authors = [
{
'first' : author['name']['first'],
'last' : author['name']['last'],
'orcid' : author['orcid'],
}
for author in self.paper.authors_list
]

return authors


def _get_depositor(self):
"""
Returns a dictionary with first and last name
"""
d = {
'first' : self.user.first_name,
'last' : self.user.last_name,
}

return d


def _get_eissn(self):
"""
Returns the eissn / essn if available or `None`
"""
if self.publication.journal is not None:
return self.publication.journal.essn


def _get_embargo(self, form):
"""
Takes the embargo date from the form and returns it as isoformatted string or None
:param form: django form (BaseMetadataForm)
:returns: string
"""
embargo = form.cleaned_data.get('embargo', None)
if embargo is not None:
embargo = embargo.isoformat()
return embargo


def _get_issn(self):
"""
Returns the issn if available or `None`
"""
if self.publication.journal is not None:
return self.publication.journal.issn


def _get_license(self, license_chooser):
"""
Returns a dictionary with fields about name, URI and transmit id
"""
if license_chooser:
d = {
'name' : license_chooser.license.name,
'uri' : license_chooser.license.uri,
'transmit_id' : license_chooser.transmit_id
}
return d


def _get_metadata(self, form, pdf):
"""
Creates metadata ready to be converted into JSON.
Mainly we create a dictionary with some types as content that serialize well into JSON
"""

md = {
'abstract' : form.cleaned_data.get('abstract', None),
'authors' : self._get_authors(),
'date' : self.paper.pubdate,
'depositor' : {
'first' : self.user.first_name,
'last' : self.user.last_name,
'email' : form.cleaned_data.get('email', None),
'orcid' : self._get_depositor_orcid(),
'is_contributor' : self.paper.is_owned_by(self.user, flexible=True),
},
'dissemin_id' : self.paper.pk,
'doctype' : self.paper.doctype,
'doi' : self.publication.doi,
'eissn' : self._get_eissn(),
'embargo' : self._get_embargo(form),
'file_url' : 'https://{}{}'.format(Site.objects.get_current(), pdf.get_absolute_url()),
'issn' : self._get_issn(),
'issue' : self.publication.issue,
'journal' : self.publication.full_journal_title(),
'license' : self._get_license(form.cleaned_data.get('license', None)),
'page' : self.publication.pages,
'publisher' : self._get_publisher_name(),
'sherpa_romeo_id' : self._get_sherpa_romeo_id(),
'title' : self.paper.title,
'volume' : self.publication.volume,
}

return md