SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit bdbd8406 authored by Jean-Sébastien Caux's avatar Jean-Sébastien Caux
Browse files

Add has_contributor property to Profile

parent 55beb5d3
No related branches found
No related tags found
No related merge requests found
......@@ -81,6 +81,21 @@ class ProfileMergeForm(forms.Form):
to_merge = forms.IntegerField()
to_merge_into = forms.IntegerField()
def clean(self):
"""
To merge Profiles, they must be distinct, and it must not be the
case that they both are associated to a Contributor instance
(which would mean two Contributor objects for the same person).
"""
data = super().clean()
if self.cleaned_data['to_merge'] == self.cleaned_data['to_merge_into']:
self.add_error(None, 'A Profile cannot be merged into itself.')
profile_to_merge = get_object_or_404(Profile, pk=self.cleaned_data['to_merge'])
profile_to_merge_into = get_object_or_404(Profile, pk=self.cleaned_data['to_merge_into'])
if profile_to_merge.has_contributor and profile_to_merge_into.has_contributor:
self.add_error(None, 'Each of these two Profiles has a Contributor. Cannot merge.')
return data
def save(self):
"""
Perform the actual merge: save all data from to-be-deleted profile
......
......@@ -10,6 +10,7 @@ from scipost.behaviors import orcid_validator
from scipost.constants import (
TITLE_CHOICES, SCIPOST_DISCIPLINES, DISCIPLINE_PHYSICS, SCIPOST_SUBJECT_AREAS)
from scipost.fields import ChoiceArrayField
from scipost.models import Contributor
from comments.models import Comment
from journals.models import Publication, PublicationAuthorsTable
......@@ -73,6 +74,15 @@ class Profile(models.Model):
def email(self):
return getattr(self.emails.filter(primary=True).first(), 'email', '')
@property
def has_contributor(self):
has_contributor = False
try:
has_contributor = (self.contributor is not None)
except Contributor.DoesNotExist:
pass
return has_contributor
def get_absolute_url(self):
return reverse('profiles:profile_detail', kwargs={'pk': self.id})
......
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