Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprints author rework #2079

Merged
merged 17 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/events/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# we need this for strict type checking on the event destroyer
from submission import models as submission_models
from django.conf import settings


class Events:
Expand Down Expand Up @@ -211,10 +212,14 @@ class Events:
# raised when a new preprint article is submitted
ON_PREPRINT_SUBMISSION = 'on_preprint_submission'

# kwargs: request, article
# kwargs: request, preprint
# raised when a preprint is published in the repo
ON_PREPRINT_PUBLICATION = 'on_preprint_publication'

# kwargs: request, preprint, email_content
# raised when a preprint is published in the repo
ON_PREPRINT_NOTIFICATION = 'on_preprint_notification'

# kwargs: request, article, comment
# raised when a new comment is submitted for a preprint
ON_PREPRINT_COMMENT = 'on_preprint_comment'
Expand All @@ -237,7 +242,8 @@ def raise_event(event_name, task_object=None, **kwargs):
:param kwargs: the arguments to pass to the event
:return: None
"""

if settings.DEBUG:
print('Firing event {}'.format(event_name))
# destroy/complete tasks that have registered for this event
if event_name != "destroy_tasks" and task_object is not None and isinstance(task_object,
submission_models.Article):
Expand Down
4 changes: 2 additions & 2 deletions src/events/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
event_logic.Events.register_for_event(event_logic.Events.ON_PREPRINT_SUBMISSION,
transactional_emails.preprint_submission)

event_logic.Events.register_for_event(event_logic.Events.ON_PREPRINT_PUBLICATION,
transactional_emails.preprint_publication)
event_logic.Events.register_for_event(event_logic.Events.ON_PREPRINT_NOTIFICATION,
transactional_emails.preprint_notification)

event_logic.Events.register_for_event(event_logic.Events.ON_PREPRINT_COMMENT,
transactional_emails.preprint_comment)
Expand Down
10 changes: 7 additions & 3 deletions src/repository/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,16 @@ class PreprintAccessAdmin(admin.ModelAdmin):
save_as = True


class PreprintAuthorAdmin(admin.ModelAdmin):
list_display = ('account', 'preprint', 'order')
list_filter = ('account', 'preprint',)
raw_id_fields = ('preprint', 'preprint')


class PreprintSupplementaryFileAdmin(admin.ModelAdmin):
pass



admin_list = [
(models.Repository, RepositoryAdmin),
(models.PreprintVersion, VersionAdmin),
Expand All @@ -74,8 +79,7 @@ class PreprintSupplementaryFileAdmin(admin.ModelAdmin):
(models.Preprint, PreprintAdmin),
(models.PreprintFile, PreprintFileAdmin),
(models.PreprintSupplementaryFile, PreprintSupplementaryFileAdmin),
(models.Author,),
(models.PreprintAuthor,),
(models.PreprintAuthor, PreprintAuthorAdmin),
(models.RepositoryField,),
(models.RepositoryFieldAnswer,),
(models.PreprintAccess, PreprintAccessAdmin),
Expand Down
91 changes: 65 additions & 26 deletions src/repository/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from django import forms
from django.forms import modelformset_factory
from django.utils.translation import ugettext_lazy as _
from django_summernote.widgets import SummernoteWidget
from django.conf import settings
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.utils.text import slugify
from django.contrib import messages

from submission import models as submission_models
from repository import models
from press import models as press_models
from review.forms import render_choices
from core import models as core_models


class PreprintInfo(forms.ModelForm):
Expand Down Expand Up @@ -174,13 +175,12 @@ def save(self, commit=True):
class PreprintSupplementaryFileForm(forms.ModelForm):
class Meta:
model = models.PreprintSupplementaryFile
fields = ( 'label', 'url',)
fields = ('label', 'url',)

def __init__(self, *args, **kwargs):
self.preprint = kwargs.pop('preprint')
super(PreprintSupplementaryFileForm, self).__init__(*args, **kwargs)


def save(self, commit=True):
link = super(PreprintSupplementaryFileForm, self).save(commit=False)
link.preprint = self.preprint
Expand All @@ -191,32 +191,71 @@ def save(self, commit=True):
return link


class AuthorForm(forms.Form):
email_address = forms.EmailField(required=True)
first_name = forms.CharField(max_length=200)
middle_name = forms.CharField(max_length=200, required=False)
last_name = forms.CharField(max_length=200)
affiliation = forms.CharField(max_length=200, required=False)

def __init__(self, *args, **kwargs):
self.instance = kwargs.pop('instance')
self.request = kwargs.pop('request')
self.preprint = kwargs.pop('preprint')
super(AuthorForm, self).__init__(*args, **kwargs)

if self.instance:
self.fields['email_address'].initial = self.instance.account.email
self.fields['first_name'].initial = self.instance.account.first_name
self.fields['middle_name'].initial = self.instance.account.middle_name
self.fields['last_name'].initial = self.instance.account.last_name
self.fields['affiliation'].initial = self.instance.affiliation or self.instance.account.institution

def save(self):
cleaned_data = self.cleaned_data
if self.instance:
account = self.instance.account
account.email = cleaned_data.get('email_address')
account.first_name = cleaned_data.get('first_name')
account.middle_name = cleaned_data.get('middle_name')
account.last_name = cleaned_data.get('last_name')
self.instance.affiliation = cleaned_data.get('affiliation')

account.save()
self.instance.save()
return self.instance
else:
account, ac = core_models.Account.objects.get_or_create(
email=cleaned_data.get('email_address'),
defaults={
'first_name': cleaned_data.get('first_name'),
'middle_name': cleaned_data.get('middle_name'),
'last_name': cleaned_data.get('last_name'),
}
)
preprint_author, pc = models.PreprintAuthor.objects.get_or_create(
account=account,
preprint=self.preprint,
defaults={
'affiliation': cleaned_data.get('affiliation'),
'order': self.preprint.next_author_order()
}
)

class AuthorForm(forms.ModelForm):
class Meta:
model = models.Author
fields = (
'email_address',
'first_name',
'middle_name',
'last_name',
'affiliation',
'orcid',
)

if not ac:
messages.add_message(
self.request,
messages.WARNING,
'A user with this email address was found. They have been added.'
)
else:
messages.add_message(
self.request,
messages.SUCCESS,
'User added as Author.',
)

AuthorFormSet = modelformset_factory(
models.Author,
fields=(
'email_address',
'first_name',
'middle_name',
'last_name',
'affiliation',
'orcid',
)
)
return preprint_author


class CommentForm(forms.ModelForm):
Expand Down
13 changes: 8 additions & 5 deletions src/repository/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
from django.urls import reverse
from django.contrib import messages
from django.db.models import Q
from django.forms import formset_factory

from production.logic import save_galley
from core import models as core_models, files
from utils import render_template, shared
from utils.function_cache import cache
from events import logic as event_logic
from repository import models
from repository import models, forms
from metrics.logic import get_iso_country_code, iso_to_country_object


Expand Down Expand Up @@ -441,11 +442,11 @@ def check_duplicates(version_queue):
def search_for_authors(request, preprint):
search_term = request.POST.get('search')
try:
search_author = models.Author.objects.get(
Q(email_address=search_term) | Q(orcid=search_term)
search_author = core_models.Account.objects.get(
Q(email=search_term) | Q(orcid=search_term)
)
pa, created = models.PreprintAuthor.objects.get_or_create(
author=search_author,
account=search_author,
preprint=preprint,
defaults={'order': preprint.next_author_order()},
)
Expand All @@ -458,7 +459,9 @@ def search_for_authors(request, preprint):
request.repository.object_name,
)
)
except models.Author.DoesNotExist:

return pa
except core_models.Account.DoesNotExist:
messages.add_message(
request,
messages.INFO,
Expand Down
25 changes: 24 additions & 1 deletion src/repository/migrations/0021_merge_20201116_1559.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# Generated by Django 1.11.29 on 2020-11-16 23:59
from __future__ import unicode_literals

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


class Migration(migrations.Migration):
Expand All @@ -13,4 +15,25 @@ class Migration(migrations.Migration):
]

operations = [
migrations.AddField(
model_name='preprintauthor',
name='account',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='preprintauthor',
name='affiliation',
field=models.TextField(blank=True, null=True),
),
migrations.AlterModelOptions(
name='author',
options={},
),
migrations.AlterField(
model_name='preprintauthor',
name='author',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
to='repository.Author'),
),
]
47 changes: 47 additions & 0 deletions src/repository/migrations/0022_auto_20210303_1450.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2021-03-03 14:50
from __future__ import unicode_literals

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


def create_accounts_for_authors(apps, schema_editor):
Account = apps.get_model('core', 'Account')
PreprintAuthor = apps.get_model('repository', 'PreprintAuthor')

for preprint_author in PreprintAuthor.objects.all():
acc, c = Account.objects.get_or_create(
username=preprint_author.author.email_address.lower(),
defaults={
'first_name': preprint_author.author.first_name,
'middle_name': preprint_author.author.middle_name,
'last_name': preprint_author.author.middle_name,
'institution': preprint_author.affiliation if preprint_author.affiliation else 'n/a',
'orcid': preprint_author.author.orcid,
'is_active': True,
'email': preprint_author.author.email_address.lower(),
}
)
preprint_author.account = acc
preprint_author.save()

if c:
print('New account created for Preprint author {}'.format(preprint_author))
else:
print('Account found and linked to Preprint author {}'.format(preprint_author))


class Migration(migrations.Migration):

dependencies = [
('repository', '0021_merge_20201116_1559'),
]

operations = [
migrations.RunPython(
create_accounts_for_authors,
reverse_code=migrations.RunPython.noop,
),
]
22 changes: 22 additions & 0 deletions src/repository/migrations/0023_auto_20210811_1317.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2021-08-11 13:17
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('repository', '0022_auto_20210303_1450'),
]

operations = [
migrations.AlterUniqueTogether(
name='preprintauthor',
unique_together=set([('account', 'preprint')]),
),
]
Loading