-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from gitnnolabs/add_op_journals
Adiciona o modelo e coleta de periódico a partir do OpenAlex
- Loading branch information
Showing
14 changed files
with
597 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
] |
46 changes: 46 additions & 0 deletions
46
journal/migrations/0002_remove_sourcejournal_journal_sou_issn_b43f7e_idx_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")) |
Oops, something went wrong.