Skip to content

Commit

Permalink
Faz correções em v1.0.4 (#318)
Browse files Browse the repository at this point in the history
* Troca a cor roxa pela vermelha e esconde o logo do wagtail no login

* Corrige valores de pid v3, v2 e aop_pid quando há espaços extras no XML

* Somente verifica se houve troca se o valor do PID foi fornecido

* Melhora os comentários de PidProviderXML._complete_pids

* Altera o modelo PidChange renomeando os atributos old e new e adicionando pkg_name

* Remove related_items pois neste contexto não é necessário
Deve ser tratando no contexto de Article

* Modifica tratamento de exceção de QueryDocumentMultipleObjectsReturnedError, fazendo o registro de quantos e quais são os registros duplicados.
Tentar apagá-los

* Atualiza a versão de packtools para 3.2.1

* Gera migração de base de dados de pid_provider.PidProviderXML e pid_provider.PidChange
  • Loading branch information
robertatakenaka authored Aug 15, 2023
1 parent 7e88a27 commit 73df889
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Generated by Django 4.1.10 on 2023-08-15 14:00

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("pid_provider", "0002_pidproviderxml_origin_date"),
]

operations = [
migrations.RemoveField(
model_name="xmlrelateditem",
name="creator",
),
migrations.RemoveField(
model_name="xmlrelateditem",
name="updated_by",
),
migrations.RemoveIndex(
model_name="pidchange",
name="pid_provide_old_74f4ac_idx",
),
migrations.RemoveIndex(
model_name="pidchange",
name="pid_provide_new_7260e5_idx",
),
migrations.RemoveField(
model_name="pidchange",
name="new",
),
migrations.RemoveField(
model_name="pidchange",
name="old",
),
migrations.RemoveField(
model_name="pidproviderxml",
name="related_items",
),
migrations.AddField(
model_name="pidchange",
name="pid_assigned",
field=models.CharField(
blank=True, max_length=23, null=True, verbose_name="PID assigned"
),
),
migrations.AddField(
model_name="pidchange",
name="pid_in_xml",
field=models.CharField(
blank=True, max_length=23, null=True, verbose_name="PID pid_in_xml"
),
),
migrations.AddField(
model_name="pidchange",
name="pkg_name",
field=models.TextField(blank=True, null=True, verbose_name="Package name"),
),
migrations.AddIndex(
model_name="pidchange",
index=models.Index(
fields=["pid_in_xml"], name="pid_provide_pid_in__da2270_idx"
),
),
migrations.AddIndex(
model_name="pidchange",
index=models.Index(
fields=["pid_assigned"], name="pid_provide_pid_ass_a06263_idx"
),
),
migrations.DeleteModel(
name="XMLRelatedItem",
),
]
125 changes: 63 additions & 62 deletions pid_provider/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,69 +180,42 @@ def register_failure(cls, e, user=None, origin=None, message=None, detail=None):
base_form_class = CoreAdminModelForm


class XMLRelatedItem(CommonControlField):
"""
Tem função de guardar os relacionamentos entre outro Documento (Artigo)
Tem objetivo de identificar o Documento (Artigo)
"""

main_doi = models.TextField(_("DOI"), null=True, blank=True)

class Meta:
indexes = [
models.Index(fields=["main_doi"]),
]

def __str__(self):
return self.main_doi

@classmethod
def get_or_create(cls, main_doi, creator=None):
try:
return cls.objects.get(main_doi=main_doi)
except cls.DoesNotExist:
obj = cls()
obj.main_doi = main_doi
obj.creator = creator
obj.created = utcnow()
obj.save()
return obj


class PidChange(CommonControlField):
pkg_name = models.TextField(_("Package name"), null=True, blank=True)
pid_type = models.CharField(_("PID type"), max_length=23, null=True, blank=True)
old = models.CharField(_("PID old"), max_length=23, null=True, blank=True)
new = models.CharField(_("PID new"), max_length=23, null=True, blank=True)
pid_in_xml = models.CharField(_("PID pid_in_xml"), max_length=23, null=True, blank=True)
pid_assigned = models.CharField(_("PID assigned"), max_length=23, null=True, blank=True)
version = models.ForeignKey(
XMLVersion, null=True, blank=True, on_delete=models.SET_NULL
)

class Meta:
indexes = [
models.Index(fields=["old"]),
models.Index(fields=["new"]),
models.Index(fields=["pid_in_xml"]),
models.Index(fields=["pid_assigned"]),
models.Index(fields=["pid_type"]),
models.Index(fields=["version"]),
]

def __str__(self):
return f"{self.pid_type} {self.old} -> {self.new}"
return f"{self.pid_type} {self.pid_in_xml} -> {self.pid_assigned}"

@classmethod
def get_or_create(cls, pid_type, old, new, version, user):
def get_or_create(cls, pid_type, pid_in_xml, pid_assigned, version, user, pkg_name):
try:
return cls.objects.get(
pid_type=pid_type,
old=old,
new=new,
pid_in_xml=pid_in_xml,
pid_assigned=pid_assigned,
version=version,
)
except cls.DoesNotExist:
obj = cls()
obj.creator = user
obj.pkg_name = pkg_name
obj.pid_type = pid_type
obj.old = old
obj.new = new
obj.pid_in_xml = pid_in_xml
obj.pid_assigned = pid_assigned
obj.version = version
obj.save()
return obj
Expand All @@ -260,7 +233,6 @@ class PidProviderXML(CommonControlField):
issue = models.ForeignKey(
XMLIssue, on_delete=models.SET_NULL, null=True, blank=True
)
related_items = models.ManyToManyField(XMLRelatedItem)
current_version = models.ForeignKey(
XMLVersion, on_delete=models.SET_NULL, null=True, blank=True
)
Expand Down Expand Up @@ -315,7 +287,6 @@ class PidProviderXML(CommonControlField):
FieldPanel("z_links"),
FieldPanel("z_partial_body"),
FieldPanel("current_version"),
FieldPanel("related_items"),
]

class Meta:
Expand Down Expand Up @@ -450,10 +421,22 @@ def register(
data["xml_changed"] = bool(changed_pids)
return data

except (
exceptions.QueryDocumentMultipleObjectsReturnedError,
) as e:
data = json.loads(str(e))
pid_request = PidRequest.create_or_update(
user=user,
origin=filename,
result_type=str(type(e)),
result_msg=_("Found {} records for {}").format(
len(data["items"]), data["params"]),
detail=data,
)
return pid_request.data
except (
exceptions.ForbiddenPidProviderXMLRegistrationError,
exceptions.NotEnoughParametersToGetDocumentRecordError,
exceptions.QueryDocumentMultipleObjectsReturnedError,
exceptions.InvalidPidError,
) as e:
pid_request = PidRequest.register_failure(
Expand Down Expand Up @@ -502,7 +485,6 @@ def _save(
registered.save()

registered._add_pid_changes(changed_pids, user)
registered._add_related_items(xml_adapter, user)

return registered

Expand Down Expand Up @@ -620,9 +602,18 @@ def _query_document(cls, xml_adapter):
except cls.MultipleObjectsReturned as e:
# seria inesperado já que os dados informados devem encontrar
# ocorrência única ou None
logging.info(f"params={adapted_params} | e={e}")
items = []
for item in cls.objects.filter(**adapted_params).iterator():
items.append(item.data)
try:
cls.objects.filter(**adapted_params).delete()
deleted = True
except Exception as e:
logging.exception(e)
deleted = str(e)

raise exceptions.QueryDocumentMultipleObjectsReturnedError(
_("Found more than one document matching to {}").format(adapted_params)
str({"params": adapted_params, "items": items, "deleted": deleted})
)

def _add_data(self, xml_adapter, user):
Expand Down Expand Up @@ -665,14 +656,6 @@ def _add_issue(self, xml_adapter, journal):
def _add_current_version(self, xml_adapter, user):
self.current_version = XMLVersion.get_or_create(user, xml_adapter.xml_with_pre)

def _add_related_items(self, xml_adapter, creator):
if xml_adapter.related_items:
self.save()
for related in xml_adapter.related_items:
self.related_items.add(
XMLRelatedItem.get_or_create(related["href"], creator)
)

def _add_pid_changes(self, changed_pids, user):
# requires registered.current_version is set
if not changed_pids:
Expand All @@ -682,11 +665,12 @@ def _add_pid_changes(self, changed_pids, user):
"PidProviderXML._add_pid_changes requires current_version is set"
)
for change_args in changed_pids:
if change_args["old"]:
# somente registra as mudanças de um old não vazio
if change_args["pid_in_xml"]:
# somente registra as mudanças de um pid_in_xml não vazio
change_args["user"] = user
change_args["version"] = self.current_version
change = PidChange.get_or_create(**change_args)
change_args["pkg_name"] = self.pkg_name
PidChange.get_or_create(**change_args)

@classmethod
def _get_unique_v3(cls):
Expand Down Expand Up @@ -745,8 +729,16 @@ def _get_unique_v2(cls, xml_adapter):
@classmethod
def _complete_pids(cls, xml_adapter, registered):
"""
Update `xml_adapter` pids with `registered` pids or
create `xml_adapter` pids
No XML pode conter ou não os PIDS v2, v3 e aop_pid.
Na base de dados o documento do XML pode ou não estar registrado.
O resultado deste procedimento é que seja garantido que os
valores dos PIDs no XML sejam inéditos ou recuperados da base de dados.
Se no XML existir os PIDs, os valores são verificados na base de dados,
se são inéditos, não haverá mudança no XML, mas se os PIDs do XML
conflitarem com os PIDs registrados ou seus valores forem inválidos,
haverá mudança nos PIDs.
Parameters
----------
Expand All @@ -764,7 +756,15 @@ def _complete_pids(cls, xml_adapter, registered):
aop_pid=xml_adapter.aop_pid,
)

# adiciona os pids faltantes aos dados de entrada
# garante que não há espaços extras
if xml_adapter.v3:
xml_adapter.v3 = xml_adapter.v3.strip()
if xml_adapter.v2:
xml_adapter.v2 = xml_adapter.v2.strip()
if xml_adapter.aop_pid:
xml_adapter.aop_pid = xml_adapter.aop_pid.strip()

# adiciona os pids faltantes ao XML fornecido
cls._add_pid_v3(xml_adapter, registered)
cls._add_pid_v2(xml_adapter, registered)
cls._add_aop_pid(xml_adapter, registered)
Expand All @@ -777,14 +777,15 @@ def _complete_pids(cls, xml_adapter, registered):

LOGGER.info("%s %s" % (before, after))

# verifica se houve mudança nos PIDs do XML
changes = []
for k, v in before.items():
if v != after[k]:
if v and v != after[k]:
changes.append(
dict(
pid_type=k,
old=v,
new=after[k],
pid_in_xml=v,
pid_assigned=after[k],
)
)
return changes
Expand Down
9 changes: 5 additions & 4 deletions pid_provider/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ class PidChangeAdmin(ModelAdmin):
exclude_from_explorer = False

list_display = (
"old",
"new",
"pkg_name",
"pid_in_xml",
"pid_assigned",
"pid_type",
)
list_filter = ("pid_type",)
search_fields = (
"old",
"new",
"pid_in_xml",
"pid_assigned",
)


Expand Down
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ minio==7.1.15
# ------------------------------------------------------------------------------
lxml==4.9.3 # https://github.com/lxml/lxml
tornado>=6.3.2 # not directly required, pinned by Snyk to avoid a vulnerability
-e git+https://github.com/scieloorg/packtools#egg=packtools
-e git+https://git@github.com/scieloorg/packtools@3.2.1#egg=packtools

# Sickle
# ------------------------------------------------------------------------------
Expand Down

0 comments on commit 73df889

Please sign in to comment.