We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Remove'unicode' calls from wafer
Current wafer using python 3 fails on several admin tasks because `UserProfile.__str__` tries to call `unicode`, which is obviously not defined.

We should handle the difference between python 2 and python 3 correctly in this situation.

There are a couple of other calls to unicode() that look dangerous in the error paths in /registration/views.py that should probably be fixed as well.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `wafer/users/models.py`
Content:
```
1 from django.contrib.auth.models import User
2 from django.db import models
3 from django.db.models.signals import post_save
4 from django.utils.encoding import python_2_unicode_compatible
5 
6 from libravatar import libravatar_url
7 try:
8     from urllib2 import urlparse
9 except ImportError:
10     from urllib import parse as urlparse
11 from django.utils.http import urlquote
12 
13 from wafer.talks.models import ACCEPTED, PENDING
14 
15 
16 @python_2_unicode_compatible
17 class UserProfile(models.Model):
18     user = models.OneToOneField(User)
19     contact_number = models.CharField(max_length=16, null=True, blank=True)
20     bio = models.TextField(null=True, blank=True)
21 
22     homepage = models.CharField(max_length=256, null=True, blank=True)
23     # We should probably do social auth instead
24     # And care about other code hosting sites...
25     twitter_handle = models.CharField(max_length=15, null=True, blank=True)
26     github_username = models.CharField(max_length=32, null=True, blank=True)
27 
28     def __str__(self):
29         return unicode(self.user)
30 
31     def accepted_talks(self):
32         return self.user.talks.filter(status=ACCEPTED)
33 
34     def pending_talks(self):
35         return self.user.talks.filter(status=PENDING)
36 
37     def avatar_url(self, size=96, https=True, default='mm'):
38         if not self.user.email:
39             return None
40         return libravatar_url(self.user.email, size=size, https=https,
41                               default=default)
42 
43     def homepage_url(self):
44         """Try ensure we prepend http: to the url if there's nothing there
45 
46            This is to ensure we're not generating relative links in the
47            user templates."""
48         if not self.homepage:
49             return self.homepage
50         parsed = urlparse.urlparse(self.homepage)
51         if parsed.scheme:
52             return self.homepage
53         # Vague sanity check
54         abs_url = ''.join(['http://', self.homepage])
55         if urlparse.urlparse(abs_url).scheme == 'http':
56             return abs_url
57         return self.homepage
58 
59     def display_name(self):
60         return self.user.get_full_name() or self.user.username
61 
62 
63 def create_user_profile(sender, instance, created, raw=False, **kwargs):
64     if raw:
65         return
66     if created:
67         UserProfile.objects.create(user=instance)
68 
69 post_save.connect(create_user_profile, sender=User)
70
```

Path: `wafer/registration/views.py`
Content:
```
1 import urllib
2 
3 from django.contrib.auth import login
4 from django.contrib import messages
5 from django.core.urlresolvers import reverse
6 from django.conf import settings
7 from django.http import Http404, HttpResponseRedirect
8 
9 from wafer.registration.sso import SSOError, debian_sso, github_sso
10 
11 
12 def redirect_profile(request):
13     '''
14     The default destination from logging in, redirect to the actual profile URL
15     '''
16     if request.user.is_authenticated():
17         return HttpResponseRedirect(reverse('wafer_user_profile',
18                                             args=(request.user.username,)))
19     else:
20         return HttpResponseRedirect(reverse('wafer_page', args=('index',)))
21 
22 
23 def github_login(request):
24     if 'github' not in settings.WAFER_SSO:
25         raise Http404()
26 
27     if 'code' not in request.GET:
28         return HttpResponseRedirect(
29             'https://github.com/login/oauth/authorize?' + urllib.urlencode({
30                 'client_id': settings.WAFER_GITHUB_CLIENT_ID,
31                 'redirect_uri': request.build_absolute_uri(
32                     reverse(github_login)),
33                 'scope': 'user:email',
34                 'state': request.META['CSRF_COOKIE'],
35             }))
36 
37     try:
38         if request.GET['state'] != request.META['CSRF_COOKIE']:
39             raise SSOError('Incorrect state')
40 
41         user = github_sso(request.GET['code'])
42     except SSOError as e:
43         messages.error(request, unicode(e))
44         return HttpResponseRedirect(reverse('auth_login'))
45 
46     login(request, user)
47     return redirect_profile(request)
48 
49 
50 def debian_login(request):
51     if 'debian' not in settings.WAFER_SSO:
52         raise Http404()
53 
54     try:
55         user = debian_sso(request.META)
56     except SSOError as e:
57         messages.error(request, unicode(e))
58         return HttpResponseRedirect(reverse('auth_login'))
59 
60     login(request, user)
61     return redirect_profile(request)
62
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/wafer/registration/views.py b/wafer/registration/views.py
--- a/wafer/registration/views.py
+++ b/wafer/registration/views.py
@@ -40,7 +40,7 @@
 
         user = github_sso(request.GET['code'])
     except SSOError as e:
-        messages.error(request, unicode(e))
+        messages.error(request, u'%s' % e)
         return HttpResponseRedirect(reverse('auth_login'))
 
     login(request, user)
@@ -54,7 +54,7 @@
     try:
         user = debian_sso(request.META)
     except SSOError as e:
-        messages.error(request, unicode(e))
+        messages.error(request, u'%s' % e)
         return HttpResponseRedirect(reverse('auth_login'))
 
     login(request, user)
diff --git a/wafer/users/models.py b/wafer/users/models.py
--- a/wafer/users/models.py
+++ b/wafer/users/models.py
@@ -26,7 +26,7 @@
     github_username = models.CharField(max_length=32, null=True, blank=True)
 
     def __str__(self):
-        return unicode(self.user)
+        return u'%s' % self.user
 
     def accepted_talks(self):
         return self.user.talks.filter(status=ACCEPTED)

{"golden_diff": "diff --git a/wafer/registration/views.py b/wafer/registration/views.py\n--- a/wafer/registration/views.py\n+++ b/wafer/registration/views.py\n@@ -40,7 +40,7 @@\n \n         user = github_sso(request.GET['code'])\n     except SSOError as e:\n-        messages.error(request, unicode(e))\n+        messages.error(request, u'%s' % e)\n         return HttpResponseRedirect(reverse('auth_login'))\n \n     login(request, user)\n@@ -54,7 +54,7 @@\n     try:\n         user = debian_sso(request.META)\n     except SSOError as e:\n-        messages.error(request, unicode(e))\n+        messages.error(request, u'%s' % e)\n         return HttpResponseRedirect(reverse('auth_login'))\n \n     login(request, user)\ndiff --git a/wafer/users/models.py b/wafer/users/models.py\n--- a/wafer/users/models.py\n+++ b/wafer/users/models.py\n@@ -26,7 +26,7 @@\n     github_username = models.CharField(max_length=32, null=True, blank=True)\n \n     def __str__(self):\n-        return unicode(self.user)\n+        return u'%s' % self.user\n \n     def accepted_talks(self):\n         return self.user.talks.filter(status=ACCEPTED)\n", "issue": "Remove'unicode' calls from wafer\nCurrent wafer using python 3 fails on several admin tasks because `UserProfile.__str__` tries to call `unicode`, which is obviously not defined.\n\nWe should handle the difference between python 2 and python 3 correctly in this situation.\n\nThere are a couple of other calls to unicode() that look dangerous in the error paths in /registration/views.py that should probably be fixed as well.\n\n", "before_files": [{"content": "from django.contrib.auth.models import User\nfrom django.db import models\nfrom django.db.models.signals import post_save\nfrom django.utils.encoding import python_2_unicode_compatible\n\nfrom libravatar import libravatar_url\ntry:\n    from urllib2 import urlparse\nexcept ImportError:\n    from urllib import parse as urlparse\nfrom django.utils.http import urlquote\n\nfrom wafer.talks.models import ACCEPTED, PENDING\n\n\n@python_2_unicode_compatible\nclass UserProfile(models.Model):\n    user = models.OneToOneField(User)\n    contact_number = models.CharField(max_length=16, null=True, blank=True)\n    bio = models.TextField(null=True, blank=True)\n\n    homepage = models.CharField(max_length=256, null=True, blank=True)\n    # We should probably do social auth instead\n    # And care about other code hosting sites...\n    twitter_handle = models.CharField(max_length=15, null=True, blank=True)\n    github_username = models.CharField(max_length=32, null=True, blank=True)\n\n    def __str__(self):\n        return unicode(self.user)\n\n    def accepted_talks(self):\n        return self.user.talks.filter(status=ACCEPTED)\n\n    def pending_talks(self):\n        return self.user.talks.filter(status=PENDING)\n\n    def avatar_url(self, size=96, https=True, default='mm'):\n        if not self.user.email:\n            return None\n        return libravatar_url(self.user.email, size=size, https=https,\n                              default=default)\n\n    def homepage_url(self):\n        \"\"\"Try ensure we prepend http: to the url if there's nothing there\n\n           This is to ensure we're not generating relative links in the\n           user templates.\"\"\"\n        if not self.homepage:\n            return self.homepage\n        parsed = urlparse.urlparse(self.homepage)\n        if parsed.scheme:\n            return self.homepage\n        # Vague sanity check\n        abs_url = ''.join(['http://', self.homepage])\n        if urlparse.urlparse(abs_url).scheme == 'http':\n            return abs_url\n        return self.homepage\n\n    def display_name(self):\n        return self.user.get_full_name() or self.user.username\n\n\ndef create_user_profile(sender, instance, created, raw=False, **kwargs):\n    if raw:\n        return\n    if created:\n        UserProfile.objects.create(user=instance)\n\npost_save.connect(create_user_profile, sender=User)\n", "path": "wafer/users/models.py"}, {"content": "import urllib\n\nfrom django.contrib.auth import login\nfrom django.contrib import messages\nfrom django.core.urlresolvers import reverse\nfrom django.conf import settings\nfrom django.http import Http404, HttpResponseRedirect\n\nfrom wafer.registration.sso import SSOError, debian_sso, github_sso\n\n\ndef redirect_profile(request):\n    '''\n    The default destination from logging in, redirect to the actual profile URL\n    '''\n    if request.user.is_authenticated():\n        return HttpResponseRedirect(reverse('wafer_user_profile',\n                                            args=(request.user.username,)))\n    else:\n        return HttpResponseRedirect(reverse('wafer_page', args=('index',)))\n\n\ndef github_login(request):\n    if 'github' not in settings.WAFER_SSO:\n        raise Http404()\n\n    if 'code' not in request.GET:\n        return HttpResponseRedirect(\n            'https://github.com/login/oauth/authorize?' + urllib.urlencode({\n                'client_id': settings.WAFER_GITHUB_CLIENT_ID,\n                'redirect_uri': request.build_absolute_uri(\n                    reverse(github_login)),\n                'scope': 'user:email',\n                'state': request.META['CSRF_COOKIE'],\n            }))\n\n    try:\n        if request.GET['state'] != request.META['CSRF_COOKIE']:\n            raise SSOError('Incorrect state')\n\n        user = github_sso(request.GET['code'])\n    except SSOError as e:\n        messages.error(request, unicode(e))\n        return HttpResponseRedirect(reverse('auth_login'))\n\n    login(request, user)\n    return redirect_profile(request)\n\n\ndef debian_login(request):\n    if 'debian' not in settings.WAFER_SSO:\n        raise Http404()\n\n    try:\n        user = debian_sso(request.META)\n    except SSOError as e:\n        messages.error(request, unicode(e))\n        return HttpResponseRedirect(reverse('auth_login'))\n\n    login(request, user)\n    return redirect_profile(request)\n", "path": "wafer/registration/views.py"}], "after_files": [{"content": "from django.contrib.auth.models import User\nfrom django.db import models\nfrom django.db.models.signals import post_save\nfrom django.utils.encoding import python_2_unicode_compatible\n\nfrom libravatar import libravatar_url\ntry:\n    from urllib2 import urlparse\nexcept ImportError:\n    from urllib import parse as urlparse\nfrom django.utils.http import urlquote\n\nfrom wafer.talks.models import ACCEPTED, PENDING\n\n\n@python_2_unicode_compatible\nclass UserProfile(models.Model):\n    user = models.OneToOneField(User)\n    contact_number = models.CharField(max_length=16, null=True, blank=True)\n    bio = models.TextField(null=True, blank=True)\n\n    homepage = models.CharField(max_length=256, null=True, blank=True)\n    # We should probably do social auth instead\n    # And care about other code hosting sites...\n    twitter_handle = models.CharField(max_length=15, null=True, blank=True)\n    github_username = models.CharField(max_length=32, null=True, blank=True)\n\n    def __str__(self):\n        return u'%s' % self.user\n\n    def accepted_talks(self):\n        return self.user.talks.filter(status=ACCEPTED)\n\n    def pending_talks(self):\n        return self.user.talks.filter(status=PENDING)\n\n    def avatar_url(self, size=96, https=True, default='mm'):\n        if not self.user.email:\n            return None\n        return libravatar_url(self.user.email, size=size, https=https,\n                              default=default)\n\n    def homepage_url(self):\n        \"\"\"Try ensure we prepend http: to the url if there's nothing there\n\n           This is to ensure we're not generating relative links in the\n           user templates.\"\"\"\n        if not self.homepage:\n            return self.homepage\n        parsed = urlparse.urlparse(self.homepage)\n        if parsed.scheme:\n            return self.homepage\n        # Vague sanity check\n        abs_url = ''.join(['http://', self.homepage])\n        if urlparse.urlparse(abs_url).scheme == 'http':\n            return abs_url\n        return self.homepage\n\n    def display_name(self):\n        return self.user.get_full_name() or self.user.username\n\n\ndef create_user_profile(sender, instance, created, raw=False, **kwargs):\n    if raw:\n        return\n    if created:\n        UserProfile.objects.create(user=instance)\n\npost_save.connect(create_user_profile, sender=User)\n", "path": "wafer/users/models.py"}, {"content": "import urllib\n\nfrom django.contrib.auth import login\nfrom django.contrib import messages\nfrom django.core.urlresolvers import reverse\nfrom django.conf import settings\nfrom django.http import Http404, HttpResponseRedirect\n\nfrom wafer.registration.sso import SSOError, debian_sso, github_sso\n\n\ndef redirect_profile(request):\n    '''\n    The default destination from logging in, redirect to the actual profile URL\n    '''\n    if request.user.is_authenticated():\n        return HttpResponseRedirect(reverse('wafer_user_profile',\n                                            args=(request.user.username,)))\n    else:\n        return HttpResponseRedirect(reverse('wafer_page', args=('index',)))\n\n\ndef github_login(request):\n    if 'github' not in settings.WAFER_SSO:\n        raise Http404()\n\n    if 'code' not in request.GET:\n        return HttpResponseRedirect(\n            'https://github.com/login/oauth/authorize?' + urllib.urlencode({\n                'client_id': settings.WAFER_GITHUB_CLIENT_ID,\n                'redirect_uri': request.build_absolute_uri(\n                    reverse(github_login)),\n                'scope': 'user:email',\n                'state': request.META['CSRF_COOKIE'],\n            }))\n\n    try:\n        if request.GET['state'] != request.META['CSRF_COOKIE']:\n            raise SSOError('Incorrect state')\n\n        user = github_sso(request.GET['code'])\n    except SSOError as e:\n        messages.error(request, u'%s' % e)\n        return HttpResponseRedirect(reverse('auth_login'))\n\n    login(request, user)\n    return redirect_profile(request)\n\n\ndef debian_login(request):\n    if 'debian' not in settings.WAFER_SSO:\n        raise Http404()\n\n    try:\n        user = debian_sso(request.META)\n    except SSOError as e:\n        messages.error(request, u'%s' % e)\n        return HttpResponseRedirect(reverse('auth_login'))\n\n    login(request, user)\n    return redirect_profile(request)\n", "path": "wafer/registration/views.py"}]}