diff --git a/src/ptf_tools/doi.py b/src/ptf_tools/doi.py index a13ebb417d31ba5970c2bf49c50e794e21487425..0e635b4fe918e45a0d8467a329e1ef4d8ddd4962 100644 --- a/src/ptf_tools/doi.py +++ b/src/ptf_tools/doi.py @@ -2,14 +2,13 @@ import os from datetime import datetime import requests -from lxml import etree - from django.conf import settings from django.core.exceptions import ObjectDoesNotExist from django.template.loader import render_to_string +from lxml import etree +from ptf.display.resolver import find_id_type from mersenne_tools.models import DOIBatch -from ptf.display.resolver import find_id_type def get_doibatch(resource): @@ -169,9 +168,14 @@ def recordDOI(resource, testing=False): doibatch = DOIBatch(resource=bookpart, status="En cours") doibatch.save() - elif resource.ctype == "issue": - # TODO - template = "issue.xml" + elif resource.ctype == "issue_special": + template = "crossref/issue_doi_register.xml" + # abstract_en = resource.abstract_set.all().get(lang='en').value_xml.replace("<abstract", "<jats:abstract").replace("</abstract>", "</jats:abstract>").replace('xml:lang="en"', "") + # abstract_en = abstract_en.replace('<p xml:space="preserve">', '<jats:p>').replace('</p>', '</jats:p>') + # context["abstract"] = abstract_en + title = resource.title_html + context["title"] = title + date = resource.year try: date = datetime.strptime(date, "%Y") @@ -265,6 +269,36 @@ def recordDOI(resource, testing=False): os.path.join(settings.LOG_DIR, "record_doi.log"), "a", encoding="utf-8" ) as file_: file_.write(doibatch.status + "\n") + else: + if resource.classname == "TranslatedArticle": + crossref_user, crossref_pwd = get_user_pwd_crossref(resource.original_article) + + with open( + os.path.join(settings.LOG_DIR, "record_doi.log"), "a", encoding="utf-8" + ) as file_: + file_.write("Call crossref\n") + + elif resource.classname == "Container" and resource.ctype.startswith("book"): + # pas de doi niveau container, alors pour obtenir les identifiants crossref on part sur le 1er book part + crossref_user, crossref_pwd = get_user_pwd_crossref(resource.article_set.first()) + else: + crossref_user, crossref_pwd = get_user_pwd_crossref(resource) + crossref_batch_url = settings.CROSSREF_BATCHURL_TPL % (crossref_user, crossref_pwd) + r = requests.post(crossref_batch_url, files=files) + body = r.text.encode("utf8") + if r.status_code == 200: + xml = etree.XML(body) + title = xml.xpath("//*/title")[0].text + if title == "SUCCESS": + data["status"] = r.status_code + elif r.status_code == 401: + doibatch.status = "Erreur" + doibatch.log = "Pb d'authentification" + doibatch.save() + else: + doibatch.status = "Erreur" + doibatch.save() + data["message"] = body[:1000].decode("utf-8") return data diff --git a/src/ptf_tools/views/base_views.py b/src/ptf_tools/views/base_views.py index 50add96495e2a8b4055c847b083f0d84d61f0454..b50b27f05f8caef8967dd287af0387c6ed7fff2a 100644 --- a/src/ptf_tools/views/base_views.py +++ b/src/ptf_tools/views/base_views.py @@ -2,7 +2,6 @@ import io import json import os import re -import csv from datetime import datetime from itertools import groupby @@ -66,7 +65,6 @@ from history import models as history_models from history import views as history_views from ptf_tools.doaj import doaj_pid_register from ptf_tools.doi import get_or_create_doibatch, recordDOI -from ptf_tools.indexingChecker import ReferencingChecker from ptf_tools.forms import ( BibItemIdForm, CollectionForm, @@ -84,6 +82,7 @@ from ptf_tools.forms import ( ResourceIdForm, get_article_choices, ) +from ptf_tools.indexingChecker import ReferencingChecker from ptf_tools.models import ResourceInNumdam from ptf_tools.tasks import ( archive_numdam_collection, @@ -1027,6 +1026,12 @@ class DeployJatsResourceAPIView(View): # We only need to copy files from /mersenne_test_data to # /mersenne_prod_data during an upload to prod if site == "website": + # TODO mettre ici le record doi pour un issue publié + if container.doi: + result = recordDOI(container, testing=True) + if result.status_code >= 300: + raise RuntimeError("a Problem occured with crossref xml deposit") + for art in articles_to_deploy: # record DOI automatically when deploying in prod @@ -1537,11 +1542,11 @@ class CreatedVolumesDashboardView(BaseMersenneDashboardView): class ReferencingDashboardView(BaseMersenneDashboardView): template_name = "dashboard/referencing.html" - def get(self, request, *args, **kwargs): + def get(self, request, *args, **kwargs): colid = self.kwargs.get("colid", None) comp = ReferencingChecker() journal = comp.check_references(colid) - return render(request, self.template_name, {'journal': journal}) + return render(request, self.template_name, {"journal": journal}) class BaseCollectionView(TemplateView): diff --git a/src/ptf_tools/views/cms_views.py b/src/ptf_tools/views/cms_views.py index 1d006cdf802938b07967de32ac07e175d07f4bc8..0ebbdf8a7c37895a8a59b96bcc36d1492b830925 100644 --- a/src/ptf_tools/views/cms_views.py +++ b/src/ptf_tools/views/cms_views.py @@ -896,7 +896,7 @@ class SpecialIssueEditAPIView(HandleCMSMixin, TemplateView): # TODO 2: In VueJS, titleFr = title trans_title_html = request.POST["title"] title_html = request.POST["trans_title"] - + last_doi = collection.last_doi if pid != "create": # TODO: do not use the pk, but the pid in the URLs container = get_object_or_404(Container, pid=pid) @@ -908,12 +908,23 @@ class SpecialIssueEditAPIView(HandleCMSMixin, TemplateView): special_issue.number = container.number volume = container.volume special_issue_pid = pid + # used for first special issues created withou a proper doi + # can be remove when no doi's less special issue existe + if not special_issue.doi: + doi_int = last_doi + 1 + special_issue.doi = f"10.5802/{colid}.sp.{doi_int}" + collection.last_doi = doi_int + 1 + collection.save() else: lang = "en" container = None container = None trans_lang = "fr" xpub = create_publisherdata() + doi_int = last_doi + 1 + special_issue.doi = f"10.5802/{colid}.sp.{doi_int}" + collection.last_doi = doi_int + 1 + collection.save() volume = "" issues = collection.content.all().order_by("-year") # if cras_issues.exists(): @@ -1009,54 +1020,6 @@ class SpecialIssueEditAPIView(HandleCMSMixin, TemplateView): index += 1 special_issue.contributors = contribs - # This part handle pdf files included in special issue. Can be editor of full pdf version - # Both are stored in same directory - - # if "IssuesIllustration" in self.request.FILES: - # icon_name = os.path.basename(self.request.FILES["IssuesIllustration"].name) - # file_extension = icon_name.split(".")[1] - # media = container.pid - # icon_file_path = resolver.get_disk_location( - # f"{settings.MEDIA_ROOT}", - # f"{collection.pid}", - # file_extension, - # media, - # None, - # True, - # ) - # path = os.path.dirname(icon_file_path) - # if os.path.isdir(path): - # shutil.rmtree(path) - # os.mkdir(path) - # with open(icon_file_path, "wb+") as destination: - # for chunk in self.request.FILES["IssuesIllustration"].chunks(): - # destination.write(chunk) - # icon = media + "." + file_extension - # location = location = f"{collection.pid}/{container.pid}/{icon}" - # try: - # extlink = ExtLink.objects.get(resource=container, rel="icon") - # extlink.location = location - # except ExtLink.DoesNotExist: - # extlink = ExtLink( - # resource=container, - # rel="icon", - # location=location, - # ) - # extlink.save() - # elif "icon_present" in request.POST: - # pass - - # else: - # try: - # location = settings.MEDIA_ROOT + collection.pid + "/" + container.pid - # if os.path.isdir(location): - # for icon in os.listdir(location): - # os.remove(location + "/" + icon) - # extlink = ExtLink.objects.get(resource=container, rel="icon") - # extlink.delete() - # except ExtLink.DoesNotExist: - # pass - # Part of the code that handle forwords and lastwords head_fr_html = xml_utils.replace_html_entities(request.POST["head_fr"]) @@ -1093,6 +1056,10 @@ class SpecialIssueEditAPIView(HandleCMSMixin, TemplateView): "value_xml": head_en_xml, }, ] + + # This part handle pdf files included in special issue. Can be editor of full pdf version + # Both are stored in same directory + pdf_file_path = resolver.get_disk_location( f"{settings.RESOURCES_ROOT}", f"{collection.pid}", diff --git a/src/templates/crossref/abstract.xml b/src/templates/crossref/abstract.xml new file mode 100644 index 0000000000000000000000000000000000000000..1a987e74890f221fc8112e4ad90541d46b81ae52 --- /dev/null +++ b/src/templates/crossref/abstract.xml @@ -0,0 +1 @@ +{{abstract|safe}} \ No newline at end of file diff --git a/src/templates/crossref/base.xml b/src/templates/crossref/base.xml index e47227e5c4968c0d575cea9241e335d3c08c655f..62254a18c5dff5c6f89bf47e8e49ae2320b4063b 100644 --- a/src/templates/crossref/base.xml +++ b/src/templates/crossref/base.xml @@ -2,9 +2,11 @@ <doi_batch xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:fr="http://www.crossref.org/fundref.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.crossref.org/schema/4.4.2 http://www.crossref.org/schema/deposit/crossref4.4.2.xsd" xmlns="http://www.crossref.org/schema/4.4.2" - version="4.4.2" - xsi:schemaLocation="http://www.crossref.org/schema/4.4.2 http://www.crossref.org/schema/deposit/crossref4.4.2.xsd"> + xmlns:jats="http://www.ncbi.nlm.nih.gov/JATS1" + version="4.4.2"> + <head> <doi_batch_id>{{doi_batch_id}}</doi_batch_id> <timestamp>{{timestamp}}</timestamp> diff --git a/src/templates/crossref/issue_doi_register.xml b/src/templates/crossref/issue_doi_register.xml new file mode 100644 index 0000000000000000000000000000000000000000..55e77f892b74f86d41c89c56c229fd8efcbc02a9 --- /dev/null +++ b/src/templates/crossref/issue_doi_register.xml @@ -0,0 +1,47 @@ +{% extends "crossref/base.xml" %} +{% load stringsfilter %} +{% load helpers %} +{% block body %} +<journal> + <journal_metadata language="en"> + <full_title>{{ resource.my_collection.title_tex|striptags}}</full_title> + {% if resource.my_collection.e_issn %} + <issn media_type="electronic">{{ resource.my_collection.e_issn }}</issn> + {% else %} + <doi_data> + <doi>{{ resource.my_collection.doi }}</doi> + <resource>{{ resource.my_collection.website }}</resource> + </doi_data> + {% endif %} + </journal_metadata> + <journal_issue> + + + + {% include "crossref/contributors.xml" with role="editor" %} + + <titles> + <title>{{title|safe}}</title> + </titles> + + <publication_date media_type="online"> + <year>{{resource.year}}</year> + </publication_date> + + <doi_data> + <doi>{{ resource.doi }}</doi> + <resource>{{ resource.get_url_absolute }}</resource> + <collection property="crawler-based"> + <item crawler="iParadigms"> + <resource>{{ resource.my_collection.website}}{{ resource.get_binary_files_href.self.pdf }}</resource> + </item> + </collection> + <collection property="text-mining"/> + </doi_data> + + + + </journal_issue> +</journal> + +{% endblock body %} \ No newline at end of file