From bdbd840684ed8a227070d9549efc7337a0bd56a0 Mon Sep 17 00:00:00 2001 From: "J.-S. Caux" <J.S.Caux@uva.nl> Date: Sat, 3 Nov 2018 22:12:40 +0100 Subject: [PATCH] Add has_contributor property to Profile --- profiles/forms.py | 15 +++++++++++++++ profiles/models.py | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/profiles/forms.py b/profiles/forms.py index cfccd92ff..5f2f66090 100644 --- a/profiles/forms.py +++ b/profiles/forms.py @@ -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 diff --git a/profiles/models.py b/profiles/models.py index 95fefb225..612a2738f 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -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}) -- GitLab