SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 9226568b authored by Boris Ponsioen's avatar Boris Ponsioen
Browse files

Adds metacore admin task to add journals to citables after import

parent 4aa624cc
No related branches found
No related tags found
No related merge requests found
from django.contrib import admin
from django.contrib import messages
from .models import Citable, CitableWithDOI, Journal
from .services import get_crossref_test, import_journal_full, get_crossref_work_count
from .services import get_crossref_test, import_journal_full, get_crossref_work_count, add_journal_to_existing
# Register your models here.
class JournalAdmin(admin.ModelAdmin):
fields = ('name', 'ISSN_digital', 'last_full_sync')
list_display = ('name', 'ISSN_digital', 'last_full_sync', 'count_metacore', 'count_crossref')
actions = ['import_full', 'update_counts']
actions = ['import_full', 'update_counts', 'add_journal_to_items']
def import_full(self, request, queryset):
""" Starts background task to import all works by this journal """
for journal in queryset:
t = import_journal_full(journal.ISSN_digital)
messages.add_message(request, messages.INFO, 'Import task for journal {} added. Go to Background Tasks -> Tasks in admin to view them'.format(journal.name))
messages.add_message(request, messages.INFO, 'Import task for journal {} added. Go to Background Tasks -> Tasks in admin to view'.format(journal.name))
messages.add_message(request, messages.WARNING, 'Make sure to start the tasks by running ./manage.py process_tasks')
messages.add_message(request, messages.WARNING, 'Make sure that "./manage.py process_tasks" is running (otherwise start it).')
def update_counts(self, request, queryset):
for journal in queryset:
journal.count_metacore = Citable.objects(metadata__ISSN=journal.ISSN_digital).count()
......@@ -25,6 +25,13 @@ class JournalAdmin(admin.ModelAdmin):
messages.add_message(request, messages.INFO, 'Counts updated.')
def add_journal_to_items(self, request, queryset):
for journal in queryset:
add_journal_to_existing(journal.ISSN_digital)
messages.add_message(request, messages.INFO, '"Add journal" task for journal {} added. Go to Background Tasks -> Tasks in admin to view'.format(journal.name))
messages.add_message(request, messages.WARNING, 'Make sure that "./manage.py process_tasks" is running (otherwise start it).')
def get_actions(self, request):
actions = super().get_actions(request)
if 'delete_selected' in actions:
......
......@@ -10,7 +10,7 @@ class CitableQuerySet(QuerySet):
return self.only('references').filter(references=dois)
def simple(self):
return self.only('doi', 'title', 'authors', 'metadata.is-referenced-by-count', 'publication_date', 'publisher', 'metadata.container-title')
return self.only('doi', 'title', 'authors', 'metadata.is-referenced-by-count', 'publication_date', 'publisher', 'metadata.container-title', 'journal')
def prl(self):
return self.filter(metadata__ISSN='0031-9007')
......
......@@ -151,6 +151,30 @@ def convert_doi_to_lower_case():
if i % 1000 == 0:
print(i)
@background()
def add_journal_to_existing(journal_issn=None):
# Take journal from metadata ('container-title') and put it in top-level 'journal' field
# for all existing citables
i = 0
errors = 0
if journal_issn:
print('Using given journal ISSN ', journal_issn)
cits = Citable.objects(metadata__ISSN=journal_issn, journal__exists=False)
else:
cits = Citable.objects(journal__exists=False)
for cit in cits.only('metadata', 'journal'):
i = i + 1
if 'container-title' in cit.metadata:
journal = cit.metadata['container-title'][0]
cit.modify(journal=journal)
else:
errors = errors + 1
if i % 1000 == 0:
print(i)
print(errors, ' errors')
print('-------')
def parse_crossref_citable(citable_item):
if not citable_item['type'] == 'journal-article':
......
......@@ -11,10 +11,10 @@
Cited {{ citable.crossref_ref_count }} times (CrossRef) / {{ citable.times_cited}} times (SciPost Meta)
| DOI <a href='https://doi.org/{{ citable.doi }}'> {{ citable.doi }} </a>
<br>
Published {{ citable.publication_date|date:"d-m-Y" }} by {{ citable.publisher }}
Published {{ citable.publication_date|date:"d-m-Y" }} by <b>{{ citable.publisher }}</b>
{% if citable.journal %}
<br>
in {{ citable.journal }}
<!-- <br> -->
in <b>{{ citable.journal }}</b>
{% endif %}
</p>
{% endblock %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment