From dcf445281df426d53b8c5203db165b01a5b2869c Mon Sep 17 00:00:00 2001
From: "J.-S. Caux" <J.S.Caux@uva.nl>
Date: Sun, 30 Sep 2018 18:17:51 +0200
Subject: [PATCH] Polish

---
 profiles/forms.py                             |  4 ++--
 profiles/templates/profiles/profile_list.html |  2 +-
 profiles/views.py                             |  2 +-
 scipost/migrations/0016_auto_20180930_1801.py | 21 +++++++++++++++++++
 scipost/models.py                             |  2 +-
 5 files changed, 26 insertions(+), 5 deletions(-)
 create mode 100644 scipost/migrations/0016_auto_20180930_1801.py

diff --git a/profiles/forms.py b/profiles/forms.py
index cdb1a8e1d..bcebf4118 100644
--- a/profiles/forms.py
+++ b/profiles/forms.py
@@ -21,10 +21,10 @@ class ProfileForm(forms.ModelForm):
         (via either the email field or the m2m-related alternativeemails).
         """
         cleaned_email = self.cleaned_data['email']
-        if Profile.objects.filter(email=cleaned_email).exists:
+        if Profile.objects.filter(email=cleaned_email).exists():
             raise forms.ValidationError(
                 'A Profile with this email (as primary email) already exists.')
-        elif Profile.objects.filter(alternativeemails__in=cleaned_email).exists():
+        elif AlternativeEmail.objects.filter(email=cleaned_email).exists():
             raise forms.ValidationError(
                 'A Profile with this email (as alternative email) already exists.')
         return cleaned_email
diff --git a/profiles/templates/profiles/profile_list.html b/profiles/templates/profiles/profile_list.html
index fac42a2f6..9881299a8 100644
--- a/profiles/templates/profiles/profile_list.html
+++ b/profiles/templates/profiles/profile_list.html
@@ -31,7 +31,7 @@
 	</li>
 	{% endif %}
 	<li><a href="{% url 'profiles:profile_create' %}">Add a Profile</a></li>
-	<li><a href="{% url 'profiles:profile_create' from_type='contributor' pk=next_contributor_wo_profile.id %}">Create a Profile</a> for Contributors without one ({{ nr_contributors_wo_profile }} to handle)</li>
+	<li>Create a Profile for <a href="{% url 'profiles:profile_create' from_type='contributor' pk=next_contributor_wo_profile.id %}">the next</a>/<a href="{% url 'profiles:profile_create' from_type='contributor' pk=random_contributor_wo_profile.id %}">a random</a> Contributor without one ({{ nr_contributors_wo_profile }} to handle)</li>
       </ul>
       <br/>
       <ul class="list-inline">
diff --git a/profiles/views.py b/profiles/views.py
index be6a8327f..2a7f66baa 100644
--- a/profiles/views.py
+++ b/profiles/views.py
@@ -107,7 +107,7 @@ class ProfileListView(PermissionsMixin, ListView):
         context['contributors_w_duplicate_email'] = contributors_dup_email
         contributors_wo_profile = Contributor.objects.filter(profile=None)
         context['nr_contributors_wo_profile'] = contributors_wo_profile.count()
-        #context['next_contributor_wo_profile'] = random.choice(contributors_wo_profile)
+        context['random_contributor_wo_profile'] = random.choice(contributors_wo_profile)
         context['next_contributor_wo_profile'] = contributors_wo_profile.first()
         context['alternative_email_form'] = AlternativeEmailForm()
         return context
diff --git a/scipost/migrations/0016_auto_20180930_1801.py b/scipost/migrations/0016_auto_20180930_1801.py
new file mode 100644
index 000000000..ad505ef3e
--- /dev/null
+++ b/scipost/migrations/0016_auto_20180930_1801.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.4 on 2018-09-30 16:01
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('scipost', '0015_contributor_profile'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='contributor',
+            name='profile',
+            field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='profiles.Profile'),
+        ),
+    ]
diff --git a/scipost/models.py b/scipost/models.py
index 6e183d558..d56d55fec 100644
--- a/scipost/models.py
+++ b/scipost/models.py
@@ -42,7 +42,7 @@ class Contributor(models.Model):
     All *science* users of SciPost are Contributors.
     username, password, email, first_name and last_name are inherited from User.
     """
-    profile = models.OneToOneField('profiles.Profile', on_delete=models.PROTECT,
+    profile = models.OneToOneField('profiles.Profile', on_delete=models.SET_NULL,
                                    null=True, blank=True)
     user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, unique=True)
     invitation_key = models.CharField(max_length=40, blank=True)
-- 
GitLab