Repository: liqd/a4-opin

Issue:
Social login results in internal server error
If you register with an facebook / google account that has the same email address as an existing account this will result in an internal server error.



Discussion:
related to pennersr/django-allauth#520


Resolving patch:
diff --git a/euth/dashboard/forms.py b/euth/dashboard/forms.py
--- a/euth/dashboard/forms.py
+++ b/euth/dashboard/forms.py
@@ -23,9 +23,9 @@ class ProfileForm(forms.ModelForm):
 
     class Meta:
         model = user_models.User
-        fields = ['avatar', 'username']
+        fields = ['_avatar', 'username']
         widgets = {
-            'avatar': widgets.ImageInputWidget()
+            '_avatar': widgets.ImageInputWidget()
         }
 
 
diff --git a/euth/users/migrations/0003_rename_avatar_field.py b/euth/users/migrations/0003_rename_avatar_field.py
new file mode 100644
--- /dev/null
+++ b/euth/users/migrations/0003_rename_avatar_field.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('euth_users', '0002_auto_20160914_2201'),
+    ]
+
+    operations = [
+        migrations.RenameField(
+            model_name='user',
+            old_name='avatar',
+            new_name='_avatar',
+        ),
+    ]
diff --git a/euth/users/models.py b/euth/users/models.py
--- a/euth/users/models.py
+++ b/euth/users/models.py
@@ -42,8 +42,8 @@ class User(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
     is_active = models.BooleanField(_('active'), default=True,
                                     help_text=IS_ACTIVE_HELP)
     date_joined = models.DateTimeField(editable=False, default=timezone.now)
-    avatar = models.ImageField(upload_to='users/images', blank=True,
-                               validators=[euth_validators.validate_logo])
+    _avatar = models.ImageField(upload_to='users/images', blank=True,
+                                validators=[euth_validators.validate_logo])
 
     objects = auth_models.UserManager()
 
@@ -57,6 +57,10 @@ class Meta:
     def __str__(self):
         return self.get_full_name()
 
+    @property
+    def avatar(self):
+        return self._avatar
+
     def get_full_name(self):
         """
         Returns the first_name plus the last_name, with a space in between.


Test patch:
diff --git a/tests/users/test_models.py b/tests/users/test_models.py
--- a/tests/users/test_models.py
+++ b/tests/users/test_models.py
@@ -7,8 +7,8 @@
 
 @pytest.mark.django_db
 def test_delete_user_signal(user_factory, ImagePNG):
-    user = user_factory(avatar=ImagePNG)
-    image_path = os.path.join(settings.MEDIA_ROOT, user.avatar.path)
+    user = user_factory(_avatar=ImagePNG)
+    image_path = os.path.join(settings.MEDIA_ROOT, user._avatar.path)
 
     assert os.path.isfile(image_path)
 
@@ -18,14 +18,14 @@ def test_delete_user_signal(user_factory, ImagePNG):
 
 @pytest.mark.django_db
 def test_image_deleted_after_update(user_factory, ImagePNG):
-    user = user_factory(avatar=ImagePNG)
-    image_path = os.path.join(settings.MEDIA_ROOT, user.avatar.path)
-    thumbnail_path = helpers.createThumbnail(user.avatar)
+    user = user_factory(_avatar=ImagePNG)
+    image_path = os.path.join(settings.MEDIA_ROOT, user._avatar.path)
+    thumbnail_path = helpers.createThumbnail(user._avatar)
 
     assert os.path.isfile(image_path)
     assert os.path.isfile(thumbnail_path)
 
-    user.avatar = None
+    user._avatar = None
     user.save()
 
     assert not os.path.isfile(image_path)