Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pesikj committed Dec 31, 2024
1 parent 15ee9a7 commit 455ffd8
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 86 deletions.
2 changes: 2 additions & 0 deletions webclient/arch_z/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ def delete_arch_z_repository_update_connected_records(sender, instance: Archeolo
"arch_z.signals.delete_arch_z_repository_update_connected_records.start",
extra={"record_ident": instance.ident_cely},
)
if instance.lokalita:
instance.lokalita.igsn_delete()
fedora_transaction: FedoraTransaction = instance.active_transaction

def save_metadata(close_transaction=False):
Expand Down
7 changes: 7 additions & 0 deletions webclient/arch_z/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ def archivovat(request, ident_cely):
item: DokumentCast
if item.dokument.stav == AZ_STAV_ARCHIVOVANY:
item.dokument.doi_publish_or_update()
if az.lokalita:
az.lokalita.igsn_publish_or_update()
az.lokalita.set_igsn()
az.lokalita.save()
az.set_archivovany(request.user)
if az.typ_zaznamu == ArcheologickyZaznam.TYP_ZAZNAMU_AKCE:
all_akce = Akce.objects.filter(projekt=az.akce.projekt).exclude(
Expand All @@ -776,6 +780,7 @@ def archivovat(request, ident_cely):
context = {
"object": az,
"title": _("arch_z.views.archivovat.title.text"),
"text": _("dokument.views.archivovat.doi_exists_warning") if az.lokalita and az.lokalita.igsn_exists else None,
"id_tag": "archivovat-akci-form",
"button": _("arch_z.views.archivovat.submitButton.text"),
"form_check": form_check,
Expand Down Expand Up @@ -809,6 +814,8 @@ def vratit(request, ident_cely):
form = VratitForm(request.POST)
if form.is_valid():
fedora_trasnaction = az.create_transaction(request.user)
if az.lokalita and az.stav == AZ_STAV_ARCHIVOVANY:
az.lokalita.igsn_hide()
duvod = form.cleaned_data["reason"]
projekt = None
if az.typ_zaznamu == ArcheologickyZaznam.TYP_ZAZNAMU_AKCE:
Expand Down
48 changes: 25 additions & 23 deletions webclient/doi/client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
import logging

import requests
from core.constants import D_STAV_ARCHIVOVANY
from django.conf import settings
from django.utils.translation import gettext as _
from doi.model_serializers import DokumentSerializer
from doi.model_serializers import DokumentSerializer, LokalitaSerializer, SamostatnyNalezSerializer
from dokument.models import Dokument
from lokalita.models import Lokalita
from pas.models import SamostatnyNalez
from requests.auth import HTTPBasicAuth

from webclient.settings.base import DATACITE_URL, DATACITE_USER, DATACITE_USER_PASSWORD
Expand All @@ -25,10 +27,6 @@ class DoiHasNotBeenPublishedYetError(DoiWriteError):
pass


class DoiIncorrectStatusError(DoiWriteError):
pass


class DoiNoTransactionError(DoiWriteError):
pass

Expand All @@ -38,42 +36,51 @@ class DigitalObjectIdentifierClient:
headers = {"Content-Type": "application/vnd.api+json"}

def __init__(self, record):
if not isinstance(record, Dokument):
raise ValueError(_("doi.client.DigitalObjectIdentifierClient.invalid_record_class"))
self.record = record
if isinstance(self.record, Dokument):
self.serializer = DokumentSerializer(self.record)
self.attribute_name = "doi"
elif isinstance(self.record, Lokalita):
self.serializer = LokalitaSerializer(self.record)
self.attribute_name = "igsn"
elif isinstance(self.record, SamostatnyNalez):
self.serializer = SamostatnyNalezSerializer(self.record)
self.attribute_name = "igsn"
else:
logger.error("doi.client.DigitalObjectIdentifierClient.invalid_record_class")
raise ValueError(_("doi.client.DigitalObjectIdentifierClient.invalid_record_class"))

def _check_response_status(self, response):
if not str(response.status_code).startswith("2"):
logger.error(
"doi.client.DigitalObjectIdentifierClient._check_response_status.error",
extra={
"ident_cely": self.record.ident_cely,
"ident_cely": self.serializer.get_ident_cely(),
"status_code": response.status_code,
"request_rul": response.url,
"response_text": response.text,
},

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This expression logs
sensitive data (secret)
as clear text.
This expression logs
sensitive data (secret)
as clear text.
)
raise DoiWriteError

def _get_record_url(self):
return f"{DATACITE_URL.rstrip(" / ")}/{settings.DOI_PREFIX}/{self.serializer.get_ident_cely()}"
return f"{DATACITE_URL.rstrip('/')}/{settings.DOI_PREFIX}/{self.serializer.get_ident_cely()}"

def check_record_exists(self):
response = requests.get(self._get_record_url(), auth=self.auth)
return str(response.status_code).startswith("2")

def delete_record(self):
if not hasattr(self.record, "active_transaction"):
if not isinstance(self.record, Lokalita) and not hasattr(self.record, "active_transaction"):
raise DoiNoTransactionError
response = requests.put(
self._get_record_url(), headers=self.headers, json=self.serializer.serialize_hide(), auth=self.auth
self._get_record_url(), headers=self.headers, json=self.serializer.serialize_delete(), auth=self.auth
)
self._check_response_status(response)
return response.json()

def hide_record(self):
if not hasattr(self.record, "active_transaction"):
if not isinstance(self.record, Lokalita) and not hasattr(self.record, "active_transaction"):
raise DoiNoTransactionError
response = requests.put(
self._get_record_url(), headers=self.headers, json=self.serializer.serialize_hide(), auth=self.auth
Expand All @@ -82,27 +89,22 @@ def hide_record(self):
return response.json()

def publish_or_update_record(self):
if not self.record.doi and not self.check_record_exists():
if not getattr(self.record, self.attribute_name, None) and not self.check_record_exists():
return self.publish_record()
else:
return self.update_record()

def publish_record(self):
if self.record.doi:
if getattr(self.record, self.attribute_name, None):
logger.error(
"doi.client.DigitalObjectIdentifierClient.publish_record.already_published.error",
extra={"ident_cely": self.record.ident_cely},
)
raise DoiHasBeenAlreadyPublishedError
if not hasattr(self.record, "active_transaction"):
if not isinstance(self.record, Lokalita) and not hasattr(self.record, "active_transaction"):
raise DoiNoTransactionError
if isinstance(self.record, Dokument):
if self.record.stav != D_STAV_ARCHIVOVANY:
logger.error(
"doi.client.DigitalObjectIdentifierClient.publish_record.archived.error",
extra={"ident_cely": self.record.ident_cely},
)
raise DoiIncorrectStatusError
with open("test.json", "w", encoding="utf-8") as f:
json.dump(self.serializer.serialize_publish(), f, ensure_ascii=False, indent=4)
response = requests.post(
DATACITE_URL, headers=self.headers, json=self.serializer.serialize_publish(), auth=self.auth
)
Expand Down
Loading

0 comments on commit 455ffd8

Please sign in to comment.