# Buggy code
def post_login_handler(sender, request, user, **kwargs):
    """
    Signal handler for setting user language and
    migrating profile if needed.
    """

    # Warning about setting password
    if (
        getattr(user, "backend", "").endswith(".EmailAuth")
        and not user.has_usable_password()
    ):
        request.session["show_set_password"] = True

    # Ensure user has a profile
    profile = Profile.objects.get_or_create(user=user)[0]

    # Migrate django-registration based verification to python-social-auth
    if (
        user.has_usable_password()
        and not user.social_auth.filter(provider="email").exists()
    ):
        social = user.social_auth.create(
            provider="email",
            uid=user.email,
        )
        VerifiedEmail.objects.create(
            social=social,
            email=user.email,
        )

    # Set language for session based on preferences
    set_lang(request, profile)


# Traceback (IntegrityError)
ERROR Internal Server Error: /accounts/login/
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/share/weblate/weblate/accounts/views.py", line 397, in weblate_login
'title': _('Login'),
File "/usr/lib/python2.7/site-packages/django/contrib/auth/views.py", line 49, in inner
return func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/views.py", line 76, in login
auth_login(request, form.get_user())
File "/usr/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 117, in login
user_logged_in.send(sender=user.__class__, request=request, user=user)
File "/usr/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 192, in send
response = receiver(signal=self, sender=sender, **named)
File "/usr/share/weblate/weblate/accounts/models.py", line 854, in post_login_handler
uid=user.email,
File "/usr/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py", line 584, in create
return super(RelatedManager, self.db_manager(db)).create(**kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 700, in save
force_update=force_update, update_fields=update_fields)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 728, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 812, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 851, in _do_insert
using=using, raw=raw)
File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry 'email-' for key 'social_auth_usersocialauth_provider_e6b5e668_uniq'")

# Fixed code
def post_login_handler(sender, request, user, **kwargs):
    """
    Signal handler for setting user language and
    migrating profile if needed.
    """

    # Warning about setting password
    if (
        getattr(user, "backend", "").endswith(".EmailAuth")
        and not user.has_usable_password()
    ):
        request.session["show_set_password"] = True

    # Ensure user has a profile
    profile = Profile.objects.get_or_create(user=user)[0]

    # Migrate django-registration based verification to python-social-auth
    if (
        user.has_usable_password()
        and user.email
        and not user.social_auth.filter(provider="email").exists()
    ):
        social = user.social_auth.create(
            provider="email",
            uid=user.email,
        )
        VerifiedEmail.objects.create(
            social=social,
            email=user.email,
        )

    # Set language for session based on preferences
    set_lang(request, profile)