Skip to content

Commit

Permalink
Merge pull request #351 from gitnnolabs/add_op_journals
Browse files Browse the repository at this point in the history
Adiciona o modelo e coleta de periódico a partir do OpenAlex
  • Loading branch information
gitnnolabs authored Oct 14, 2024
2 parents 24b3e5d + 527c03b commit f2842d8
Show file tree
Hide file tree
Showing 14 changed files with 597 additions and 5 deletions.
12 changes: 7 additions & 5 deletions article/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from django.utils.translation import gettext as _

from wagtail.contrib.modeladmin.options import (
ModelAdmin,
modeladmin_register,
ModelAdminGroup,
modeladmin_register,
)

from journal.wagtail_hooks import SourceJournalAdmin

from .models import (
Affiliation,
Article,
SourceArticle,
Concepts,
Contributor,
Affiliation,
Journal,
License,
Concepts
SourceArticle,
)


Expand Down Expand Up @@ -223,6 +224,7 @@ class ArticleAdminGroup(ModelAdminGroup):
LicenseAdmin,
ConceptsAdmin,
SourceArticleAdmin,
SourceJournalAdmin
)


Expand Down
2 changes: 2 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"wagtailautocomplete",
# "provided_data",
"article",
"journal",
]

# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
Expand Down Expand Up @@ -374,6 +375,7 @@
URL_API_CROSSREF = env.str("API_CROSSREF", default="https://api.crossref.org/works")
URL_API_OPENALEX = env.str("API_OPENALEX", default="https://api.openalex.org/works")
URL_API_OPENALEX_INSTITUTIONS = env.str("URL_API_OPENALEX_INSTITUTIONS", default="https://api.openalex.org/institutions")
URL_API_OPENALEX_JOURNALS = env.str("URL_API_OPENALEX_JOURNALS", default="https://api.openalex.org/sources")

LANGUAGES = [
("pt-BR", "Portuguese"),
Expand Down
Empty file added journal/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions journal/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions journal/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class JournalConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "journal"
83 changes: 83 additions & 0 deletions journal/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Generated by Django 4.1.6 on 2024-10-14 07:33

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
("core", "0002_language"),
]

operations = [
migrations.CreateModel(
name="SourceJournal",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"specific_id",
models.CharField(max_length=255, verbose_name="Specific Id"),
),
(
"issn",
models.CharField(max_length=255, null=True, verbose_name="DOI"),
),
(
"title",
models.CharField(max_length=1024, null=True, verbose_name="Título"),
),
(
"updated",
models.CharField(
max_length=50, null=True, verbose_name="Source updated date"
),
),
(
"created",
models.CharField(
max_length=50, null=True, verbose_name="Source created date"
),
),
(
"raw",
models.JSONField(
blank=True, null=True, verbose_name="Arquivo JSON"
),
),
(
"source",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="core.source",
verbose_name="Origem",
),
),
],
),
migrations.AddIndex(
model_name="sourcejournal",
index=models.Index(fields=["issn"], name="journal_sou_issn_b43f7e_idx"),
),
migrations.AddIndex(
model_name="sourcejournal",
index=models.Index(
fields=["specific_id"], name="journal_sou_specifi_206501_idx"
),
),
migrations.AddIndex(
model_name="sourcejournal",
index=models.Index(fields=["title"], name="journal_sou_title_cd0c69_idx"),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 4.1.6 on 2024-10-14 08:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("journal", "0001_initial"),
]

operations = [
migrations.RemoveIndex(
model_name="sourcejournal",
name="journal_sou_issn_b43f7e_idx",
),
migrations.RemoveField(
model_name="sourcejournal",
name="issn",
),
migrations.AddField(
model_name="sourcejournal",
name="country_code",
field=models.CharField(
max_length=255, null=True, verbose_name="Country Code"
),
),
migrations.AddField(
model_name="sourcejournal",
name="issn_l",
field=models.CharField(max_length=255, null=True, verbose_name="ISSN_L"),
),
migrations.AddField(
model_name="sourcejournal",
name="issns",
field=models.CharField(max_length=255, null=True, verbose_name="ISSN"),
),
migrations.AddIndex(
model_name="sourcejournal",
index=models.Index(fields=["issns"], name="journal_sou_issns_971d00_idx"),
),
migrations.AddIndex(
model_name="sourcejournal",
index=models.Index(fields=["issn_l"], name="journal_sou_issn_l_971c0c_idx"),
),
]
Empty file added journal/migrations/__init__.py
Empty file.
154 changes: 154 additions & 0 deletions journal/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
from django.db import models
from django.utils.translation import gettext as _

from core.models import Source


class SourceJournal(models.Model):
specific_id = models.CharField(
_("Specific Id"), max_length=255, null=False, blank=False
)
issns = models.CharField(_("ISSN"), max_length=255, null=True, blank=False)
issn_l = models.CharField(_("ISSN_L"), max_length=255, null=True, blank=False)
country_code = models.CharField(_("Country Code"), max_length=255, null=True, blank=False)
title = models.CharField(_("Title"), max_length=1024, null=True, blank=False)
updated = models.CharField(
_("Source updated date"), max_length=50, null=True, blank=False
)
created = models.CharField(
_("Source created date"), max_length=50, null=True, blank=False
)
raw = models.JSONField(_("JSON File"), null=True, blank=True)
source = models.ForeignKey(
Source,
verbose_name=_("Source"),
null=True,
on_delete=models.CASCADE,
)

class Meta:
indexes = [
models.Index(
fields=[
"issns",
]
),
models.Index(
fields=[
"issn_l",
]
),
models.Index(
fields=[
"specific_id",
]
),
models.Index(
fields=[
"title",
]
),
]

def __unicode__(self):
return str("%s") % (self.issns or self.specific_id)

def __str__(self):
return str("%s") % (self.issns or self.specific_id)

@property
def has_specific_id(self):
return bool(self.specific_id)

@classmethod
def get(cls, **kwargs):
"""
This function will try to get the source journal by attributes:
* issn
* specific_id
The kwargs must be a dict, something like this:
{
"specific_id": "https://openalex.org/sources/S183843087",
"issn": "1234-5678",
"source": OPENALEX,
}
return source journal|None
This function can raise:
ValueError
SourceJournal.DoesNotExist
SourceJournal.MultipleObjectsReturned
"""

filters = {}

if (
not kwargs.get("issn")
and not kwargs.get("specific_id")
and not kwargs.get("source")
):
raise ValueError("Param issn or specific_id is required")

if kwargs.get("specific_id"):
filters = {
"specific_id": kwargs.get("specific_id"),
"source": kwargs.get("source"),
}
elif kwargs.get("issn"):
filters = {"issn": kwargs.get("issn"), "source": kwargs.get("source")}

return cls.objects.get(**filters)


@classmethod
def create_or_update(cls, **kwargs):
"""
This function will try to get the journal by issn.
If the journal exists update, otherwise create.
The kwargs must be a dict, something like this:
{
"issns": "1234-5678, 0987-6543",
"issn_l": "1987-9373",
"country_code": "BR",
"title": "Update the record",
"number": "999",
"volume": "9",
"sources": list of <sources> [<source>, <source>]
}
return journal(object), 0|1
0 = updated
1 = created
"""

try:
journal = cls.get(**kwargs)
created = 0
except SourceJournal.DoesNotExist:
journal = cls.objects.create()
created = 1
except SourceJournal.MultipleObjectsReturned as e:
print(_("The source journal table have duplicity...."))
raise (SourceJournal.MultipleObjectsReturned)

journal.issns = kwargs.get("issns")
journal.issn_l = kwargs.get("issn_l")
journal.title = kwargs.get("title")
journal.country_code = kwargs.get("country_code")
journal.specific_id = kwargs.get("specific_id")
journal.updated = kwargs.get("updated")
journal.created = kwargs.get("created")
journal.raw = kwargs.get("raw")
journal.source = kwargs.get("source")
journal.save()

return journal, created
23 changes: 23 additions & 0 deletions journal/scripts/load_journal_openalex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.utils.translation import gettext as _

from journal.tasks import load_journal_openalex

from journal.models import SourceJournal


def run(user_id, country="BR", delete=0, length=None):
"""
Load the journal from OpenAlex to SourceJournal model.
About the OpenAlex API see: https://docs.openalex.org/
"""

if int(delete):
SourceJournal.objects.all().delete()

if user_id and country and length:
load_journal_openalex.apply_async(args=(int(user_id), str(country), int(length)))
elif user_id and country:
load_journal_openalex.apply_async(args=(int(user_id), str(country)))
else:
print(_("Param user_id is required."))
Loading

0 comments on commit f2842d8

Please sign in to comment.