We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Usernames are forced to lowercase on register.
Just witnessed a registration process with a username starting with a capital letter. When user tried to log in after e-mail verification, he could not log in. Changing capital to lowercase did the trick. This is however very unintuitive, so capital letters should be allowed in username, or atleast there should be a notification or warning.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `apps/authentication/views.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 
3 import uuid
4 import re
5 
6 from django.contrib import auth
7 from django.contrib import messages
8 from django.core.mail import send_mail
9 from django.shortcuts import render, redirect, get_object_or_404
10 from django.http import HttpResponseRedirect
11 from django.utils.translation import ugettext as _
12 from django.views.decorators.debug import sensitive_post_parameters
13 
14 from django.conf import settings
15 from apps.authentication.forms import (LoginForm, RegisterForm, 
16                             RecoveryForm, ChangePasswordForm)
17 from apps.authentication.models import OnlineUser as User, RegisterToken, Email
18 
19 
20 @sensitive_post_parameters()
21 def login(request):
22     redirect_url = request.REQUEST.get('next', '')
23     if request.method == 'POST':
24         form = LoginForm(request.POST)
25         if form.login(request):
26             messages.success(request, _(u'Du er nå logget inn.'))
27             if redirect_url:
28                 return HttpResponseRedirect(redirect_url)
29             return HttpResponseRedirect('/')
30         else: form = LoginForm(request.POST, auto_id=True)
31     else:
32         form = LoginForm()
33 
34     response_dict = { 'form' : form, 'next' : redirect_url}
35     return render(request, 'auth/login.html', response_dict)
36 
37 
38 def logout(request):
39     auth.logout(request)
40     messages.success(request, _(u'Du er nå logget ut.'))
41     return HttpResponseRedirect('/')
42 
43 
44 @sensitive_post_parameters()
45 def register(request):
46     if request.user.is_authenticated():
47         messages.error(request, _(u'Registrering av ny konto krever at du er logget ut.'))
48         return HttpResponseRedirect('/')
49     else:
50         if request.method == 'POST':
51             form = RegisterForm(request.POST)
52             if form.is_valid():
53                 cleaned = form.cleaned_data
54 
55                 # Create user
56                 user = User(
57                     username=cleaned['username'].lower(), 
58                     first_name=cleaned['first_name'].title(), 
59                     last_name=cleaned['last_name'].title(),
60                 )
61                 # Set remaining fields
62                 user.phone_number=cleaned['phone']
63                 user.address=cleaned['address'].title()
64                 user.zip_code=cleaned['zip_code']
65                 # Store password properly
66                 user.set_password(cleaned['password'])
67                 # Users need to be manually activated
68                 user.is_active = False
69                 user.save()
70 
71                 # Set email address
72                 email = Email(
73                     user=user,
74                     email=cleaned['email'],
75                 )
76                 email.primary = True
77                 email.save()    
78 
79                 # Create the registration token
80                 token = uuid.uuid4().hex
81                 rt = RegisterToken(user=user, email=cleaned['email'], token=token)
82                 rt.save()
83 
84                 email_message = _(u"""
85 En konto har blitt registrert på online.ntnu.no med denne epostadressen. Dersom du ikke
86 har utført denne handlingen ber vi deg se bort fra denne eposten.
87 
88 For å bruke denne kontoen kreves det at du verifiserer epostadressen. Du kan gjøre
89 dette ved å besøke linken under.
90 
91 http://%s/auth/verify/%s/
92 
93 Denne lenken vil være gyldig i 24 timer. Dersom du behøver å få tilsendt en ny lenke
94 kan dette gjøres med funksjonen for å gjenopprette passord.
95 """) % (request.META['HTTP_HOST'], token)
96 
97                 send_mail(_(u'Verifiser din konto'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])
98 
99                 messages.success(request, _(u'Registreringen var vellykket. Se tilsendt epost for verifiseringsinstrukser.'))
100 
101                 return HttpResponseRedirect('/')        
102             else:
103                 form = RegisterForm(request.POST, auto_id=True)
104         else:
105             form = RegisterForm()
106 
107         return render(request, 'auth/register.html', {'form': form, })
108 
109 
110 def verify(request, token):
111     rt = get_object_or_404(RegisterToken, token=token)
112     
113     if rt.is_valid:
114         email = get_object_or_404(Email, email=rt.email)
115         email.verified = True
116         email.save()
117          
118         user = getattr(rt, 'user')
119 
120         # If it is a stud email, set the ntnu_username for user
121         if re.match(r'[^@]+@stud.ntnu.no', rt.email):
122             user.ntnu_username = rt.email.split("@")[0]
123 
124         user_activated = False
125         if not user.is_active:
126             user.is_active = True
127             user_activated = True
128 
129         user.save()
130         rt.delete()
131 
132         if user_activated:
133             messages.success(request, _(u'Bruker %s ble aktivert. Du kan nå logge inn.') % user.username)
134             return redirect('auth_login')
135         else:
136             messages.success(request, _(u'Eposten %s er nå verifisert.') % email)
137             return redirect('profiles')
138     else:
139         messages.error(request, _(u'Denne lenken er utløpt. Bruk gjenopprett passord for å få tilsendt en ny lenke.'))
140         return HttpResponseRedirect('/')        
141             
142 
143 def recover(request):
144     if request.user.is_authenticated():
145         messages.error(request, _(u'Gjenoppretning av passord krever at du er logget ut.'))
146         return HttpResponseRedirect('/')
147     else:
148         if request.method == 'POST':
149             form = RecoveryForm(request.POST)
150             if form.is_valid():
151                 email_string = form.cleaned_data['email']
152                 emails = Email.objects.filter(email=email_string)
153 
154                 if len(emails) == 0:
155                     messages.error(request, _(u'Denne eposten er ikke registrert i våre systemer.'))
156                     return HttpResponseRedirect('/')        
157 
158                 email = emails[0]
159     
160                 # Create the registration token
161                 token = uuid.uuid4().hex
162                 rt = RegisterToken(user=email.user, email=email.email, token=token)
163                 rt.save()
164 
165                 email_message = _(u"""
166 Vi har mottat forespørsel om å gjenopprette passordet for kontoen bundet til %s.
167 Dersom du ikke har bedt om denne handlingen ber vi deg se bort fra denne eposten.
168 
169 Brukernavn: %s
170 
171 Hvis du ønsker å gjennomføre en gjenoppretning av passord, bruk lenken under.
172 
173 http://%s/auth/set_password/%s/
174 
175 Denne lenken vil være gyldig i 24 timer. Dersom du behøver å få tilsendt en ny lenke
176 kan dette gjøres med funksjonen for å gjenopprette passord.
177 """) % (email.email, email.user.username, request.META['HTTP_HOST'], token)
178 
179                 send_mail(_(u'Gjenoppretning av passord'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])
180 
181                 messages.success(request, _(u'En lenke for gjenoppretning har blitt sendt til %s.') % email.email)
182 
183                 return HttpResponseRedirect('/')        
184             else:
185                 form = RecoveryForm(request.POST, auto_id=True)
186         else:
187             form = RecoveryForm()
188 
189         return render(request, 'auth/recover.html', {'form': form})
190 
191 
192 @sensitive_post_parameters()
193 def set_password(request, token=None): 
194     if request.user.is_authenticated():
195         return HttpResponseRedirect('/')
196     else:
197         rt = get_object_or_404(RegisterToken, token=token)
198        
199         if rt.is_valid:
200             if request.method == 'POST':
201                 form = ChangePasswordForm(request.POST, auto_id=True)
202                 if form.is_valid():
203                     user = getattr(rt, 'user')
204 
205                     user.is_active = True
206                     user.set_password(form.cleaned_data['new_password'])
207                     user.save()
208                     
209                     rt.delete()
210 
211                     messages.success(request, _(u'Bruker %s har gjennomført vellykket gjenoppretning av passord. Du kan nå logge inn.') % user.username)
212                     
213                     return HttpResponseRedirect('/')        
214             else:
215                 
216                 form = ChangePasswordForm()
217 
218                 messages.success(request, _(u'Lenken er akseptert. Vennligst skriv inn ønsket passord.'))
219 
220             return render(request, 'auth/set_password.html', {'form': form, 'token': token})
221 
222         else:
223             messages.error(request, _(u'Lenken er utløpt. Vennligst bruk gjenoppretning av passord for å få tilsendt en ny lenke.'))
224             return HttpResponseRedirect('/')        
225
```
--- 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/apps/authentication/views.py b/apps/authentication/views.py
--- a/apps/authentication/views.py
+++ b/apps/authentication/views.py
@@ -54,7 +54,7 @@
 
                 # Create user
                 user = User(
-                    username=cleaned['username'].lower(), 
+                    username=cleaned['username'], 
                     first_name=cleaned['first_name'].title(), 
                     last_name=cleaned['last_name'].title(),
                 )

{"golden_diff": "diff --git a/apps/authentication/views.py b/apps/authentication/views.py\n--- a/apps/authentication/views.py\n+++ b/apps/authentication/views.py\n@@ -54,7 +54,7 @@\n \n                 # Create user\n                 user = User(\n-                    username=cleaned['username'].lower(), \n+                    username=cleaned['username'], \n                     first_name=cleaned['first_name'].title(), \n                     last_name=cleaned['last_name'].title(),\n                 )\n", "issue": "Usernames are forced to lowercase on register.\nJust witnessed a registration process with a username starting with a capital letter. When user tried to log in after e-mail verification, he could not log in. Changing capital to lowercase did the trick. This is however very unintuitive, so capital letters should be allowed in username, or atleast there should be a notification or warning.\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\nimport uuid\nimport re\n\nfrom django.contrib import auth\nfrom django.contrib import messages\nfrom django.core.mail import send_mail\nfrom django.shortcuts import render, redirect, get_object_or_404\nfrom django.http import HttpResponseRedirect\nfrom django.utils.translation import ugettext as _\nfrom django.views.decorators.debug import sensitive_post_parameters\n\nfrom django.conf import settings\nfrom apps.authentication.forms import (LoginForm, RegisterForm, \n                            RecoveryForm, ChangePasswordForm)\nfrom apps.authentication.models import OnlineUser as User, RegisterToken, Email\n\n\n@sensitive_post_parameters()\ndef login(request):\n    redirect_url = request.REQUEST.get('next', '')\n    if request.method == 'POST':\n        form = LoginForm(request.POST)\n        if form.login(request):\n            messages.success(request, _(u'Du er n\u00e5 logget inn.'))\n            if redirect_url:\n                return HttpResponseRedirect(redirect_url)\n            return HttpResponseRedirect('/')\n        else: form = LoginForm(request.POST, auto_id=True)\n    else:\n        form = LoginForm()\n\n    response_dict = { 'form' : form, 'next' : redirect_url}\n    return render(request, 'auth/login.html', response_dict)\n\n\ndef logout(request):\n    auth.logout(request)\n    messages.success(request, _(u'Du er n\u00e5 logget ut.'))\n    return HttpResponseRedirect('/')\n\n\n@sensitive_post_parameters()\ndef register(request):\n    if request.user.is_authenticated():\n        messages.error(request, _(u'Registrering av ny konto krever at du er logget ut.'))\n        return HttpResponseRedirect('/')\n    else:\n        if request.method == 'POST':\n            form = RegisterForm(request.POST)\n            if form.is_valid():\n                cleaned = form.cleaned_data\n\n                # Create user\n                user = User(\n                    username=cleaned['username'].lower(), \n                    first_name=cleaned['first_name'].title(), \n                    last_name=cleaned['last_name'].title(),\n                )\n                # Set remaining fields\n                user.phone_number=cleaned['phone']\n                user.address=cleaned['address'].title()\n                user.zip_code=cleaned['zip_code']\n                # Store password properly\n                user.set_password(cleaned['password'])\n                # Users need to be manually activated\n                user.is_active = False\n                user.save()\n\n                # Set email address\n                email = Email(\n                    user=user,\n                    email=cleaned['email'],\n                )\n                email.primary = True\n                email.save()    \n\n                # Create the registration token\n                token = uuid.uuid4().hex\n                rt = RegisterToken(user=user, email=cleaned['email'], token=token)\n                rt.save()\n\n                email_message = _(u\"\"\"\nEn konto har blitt registrert p\u00e5 online.ntnu.no med denne epostadressen. Dersom du ikke\nhar utf\u00f8rt denne handlingen ber vi deg se bort fra denne eposten.\n\nFor \u00e5 bruke denne kontoen kreves det at du verifiserer epostadressen. Du kan gj\u00f8re\ndette ved \u00e5 bes\u00f8ke linken under.\n\nhttp://%s/auth/verify/%s/\n\nDenne lenken vil v\u00e6re gyldig i 24 timer. Dersom du beh\u00f8ver \u00e5 f\u00e5 tilsendt en ny lenke\nkan dette gj\u00f8res med funksjonen for \u00e5 gjenopprette passord.\n\"\"\") % (request.META['HTTP_HOST'], token)\n\n                send_mail(_(u'Verifiser din konto'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])\n\n                messages.success(request, _(u'Registreringen var vellykket. Se tilsendt epost for verifiseringsinstrukser.'))\n\n                return HttpResponseRedirect('/')        \n            else:\n                form = RegisterForm(request.POST, auto_id=True)\n        else:\n            form = RegisterForm()\n\n        return render(request, 'auth/register.html', {'form': form, })\n\n\ndef verify(request, token):\n    rt = get_object_or_404(RegisterToken, token=token)\n    \n    if rt.is_valid:\n        email = get_object_or_404(Email, email=rt.email)\n        email.verified = True\n        email.save()\n         \n        user = getattr(rt, 'user')\n\n        # If it is a stud email, set the ntnu_username for user\n        if re.match(r'[^@]+@stud.ntnu.no', rt.email):\n            user.ntnu_username = rt.email.split(\"@\")[0]\n\n        user_activated = False\n        if not user.is_active:\n            user.is_active = True\n            user_activated = True\n\n        user.save()\n        rt.delete()\n\n        if user_activated:\n            messages.success(request, _(u'Bruker %s ble aktivert. Du kan n\u00e5 logge inn.') % user.username)\n            return redirect('auth_login')\n        else:\n            messages.success(request, _(u'Eposten %s er n\u00e5 verifisert.') % email)\n            return redirect('profiles')\n    else:\n        messages.error(request, _(u'Denne lenken er utl\u00f8pt. Bruk gjenopprett passord for \u00e5 f\u00e5 tilsendt en ny lenke.'))\n        return HttpResponseRedirect('/')        \n            \n\ndef recover(request):\n    if request.user.is_authenticated():\n        messages.error(request, _(u'Gjenoppretning av passord krever at du er logget ut.'))\n        return HttpResponseRedirect('/')\n    else:\n        if request.method == 'POST':\n            form = RecoveryForm(request.POST)\n            if form.is_valid():\n                email_string = form.cleaned_data['email']\n                emails = Email.objects.filter(email=email_string)\n\n                if len(emails) == 0:\n                    messages.error(request, _(u'Denne eposten er ikke registrert i v\u00e5re systemer.'))\n                    return HttpResponseRedirect('/')        \n\n                email = emails[0]\n    \n                # Create the registration token\n                token = uuid.uuid4().hex\n                rt = RegisterToken(user=email.user, email=email.email, token=token)\n                rt.save()\n\n                email_message = _(u\"\"\"\nVi har mottat foresp\u00f8rsel om \u00e5 gjenopprette passordet for kontoen bundet til %s.\nDersom du ikke har bedt om denne handlingen ber vi deg se bort fra denne eposten.\n\nBrukernavn: %s\n\nHvis du \u00f8nsker \u00e5 gjennomf\u00f8re en gjenoppretning av passord, bruk lenken under.\n\nhttp://%s/auth/set_password/%s/\n\nDenne lenken vil v\u00e6re gyldig i 24 timer. Dersom du beh\u00f8ver \u00e5 f\u00e5 tilsendt en ny lenke\nkan dette gj\u00f8res med funksjonen for \u00e5 gjenopprette passord.\n\"\"\") % (email.email, email.user.username, request.META['HTTP_HOST'], token)\n\n                send_mail(_(u'Gjenoppretning av passord'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])\n\n                messages.success(request, _(u'En lenke for gjenoppretning har blitt sendt til %s.') % email.email)\n\n                return HttpResponseRedirect('/')        \n            else:\n                form = RecoveryForm(request.POST, auto_id=True)\n        else:\n            form = RecoveryForm()\n\n        return render(request, 'auth/recover.html', {'form': form})\n\n\n@sensitive_post_parameters()\ndef set_password(request, token=None): \n    if request.user.is_authenticated():\n        return HttpResponseRedirect('/')\n    else:\n        rt = get_object_or_404(RegisterToken, token=token)\n       \n        if rt.is_valid:\n            if request.method == 'POST':\n                form = ChangePasswordForm(request.POST, auto_id=True)\n                if form.is_valid():\n                    user = getattr(rt, 'user')\n\n                    user.is_active = True\n                    user.set_password(form.cleaned_data['new_password'])\n                    user.save()\n                    \n                    rt.delete()\n\n                    messages.success(request, _(u'Bruker %s har gjennomf\u00f8rt vellykket gjenoppretning av passord. Du kan n\u00e5 logge inn.') % user.username)\n                    \n                    return HttpResponseRedirect('/')        \n            else:\n                \n                form = ChangePasswordForm()\n\n                messages.success(request, _(u'Lenken er akseptert. Vennligst skriv inn \u00f8nsket passord.'))\n\n            return render(request, 'auth/set_password.html', {'form': form, 'token': token})\n\n        else:\n            messages.error(request, _(u'Lenken er utl\u00f8pt. Vennligst bruk gjenoppretning av passord for \u00e5 f\u00e5 tilsendt en ny lenke.'))\n            return HttpResponseRedirect('/')        \n", "path": "apps/authentication/views.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\nimport uuid\nimport re\n\nfrom django.contrib import auth\nfrom django.contrib import messages\nfrom django.core.mail import send_mail\nfrom django.shortcuts import render, redirect, get_object_or_404\nfrom django.http import HttpResponseRedirect\nfrom django.utils.translation import ugettext as _\nfrom django.views.decorators.debug import sensitive_post_parameters\n\nfrom django.conf import settings\nfrom apps.authentication.forms import (LoginForm, RegisterForm, \n                            RecoveryForm, ChangePasswordForm)\nfrom apps.authentication.models import OnlineUser as User, RegisterToken, Email\n\n\n@sensitive_post_parameters()\ndef login(request):\n    redirect_url = request.REQUEST.get('next', '')\n    if request.method == 'POST':\n        form = LoginForm(request.POST)\n        if form.login(request):\n            messages.success(request, _(u'Du er n\u00e5 logget inn.'))\n            if redirect_url:\n                return HttpResponseRedirect(redirect_url)\n            return HttpResponseRedirect('/')\n        else: form = LoginForm(request.POST, auto_id=True)\n    else:\n        form = LoginForm()\n\n    response_dict = { 'form' : form, 'next' : redirect_url}\n    return render(request, 'auth/login.html', response_dict)\n\n\ndef logout(request):\n    auth.logout(request)\n    messages.success(request, _(u'Du er n\u00e5 logget ut.'))\n    return HttpResponseRedirect('/')\n\n\n@sensitive_post_parameters()\ndef register(request):\n    if request.user.is_authenticated():\n        messages.error(request, _(u'Registrering av ny konto krever at du er logget ut.'))\n        return HttpResponseRedirect('/')\n    else:\n        if request.method == 'POST':\n            form = RegisterForm(request.POST)\n            if form.is_valid():\n                cleaned = form.cleaned_data\n\n                # Create user\n                user = User(\n                    username=cleaned['username'], \n                    first_name=cleaned['first_name'].title(), \n                    last_name=cleaned['last_name'].title(),\n                )\n                # Set remaining fields\n                user.phone_number=cleaned['phone']\n                user.address=cleaned['address'].title()\n                user.zip_code=cleaned['zip_code']\n                # Store password properly\n                user.set_password(cleaned['password'])\n                # Users need to be manually activated\n                user.is_active = False\n                user.save()\n\n                # Set email address\n                email = Email(\n                    user=user,\n                    email=cleaned['email'],\n                )\n                email.primary = True\n                email.save()    \n\n                # Create the registration token\n                token = uuid.uuid4().hex\n                rt = RegisterToken(user=user, email=cleaned['email'], token=token)\n                rt.save()\n\n                email_message = _(u\"\"\"\nEn konto har blitt registrert p\u00e5 online.ntnu.no med denne epostadressen. Dersom du ikke\nhar utf\u00f8rt denne handlingen ber vi deg se bort fra denne eposten.\n\nFor \u00e5 bruke denne kontoen kreves det at du verifiserer epostadressen. Du kan gj\u00f8re\ndette ved \u00e5 bes\u00f8ke linken under.\n\nhttp://%s/auth/verify/%s/\n\nDenne lenken vil v\u00e6re gyldig i 24 timer. Dersom du beh\u00f8ver \u00e5 f\u00e5 tilsendt en ny lenke\nkan dette gj\u00f8res med funksjonen for \u00e5 gjenopprette passord.\n\"\"\") % (request.META['HTTP_HOST'], token)\n\n                send_mail(_(u'Verifiser din konto'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])\n\n                messages.success(request, _(u'Registreringen var vellykket. Se tilsendt epost for verifiseringsinstrukser.'))\n\n                return HttpResponseRedirect('/')        \n            else:\n                form = RegisterForm(request.POST, auto_id=True)\n        else:\n            form = RegisterForm()\n\n        return render(request, 'auth/register.html', {'form': form, })\n\n\ndef verify(request, token):\n    rt = get_object_or_404(RegisterToken, token=token)\n    \n    if rt.is_valid:\n        email = get_object_or_404(Email, email=rt.email)\n        email.verified = True\n        email.save()\n         \n        user = getattr(rt, 'user')\n\n        # If it is a stud email, set the ntnu_username for user\n        if re.match(r'[^@]+@stud.ntnu.no', rt.email):\n            user.ntnu_username = rt.email.split(\"@\")[0]\n\n        user_activated = False\n        if not user.is_active:\n            user.is_active = True\n            user_activated = True\n\n        user.save()\n        rt.delete()\n\n        if user_activated:\n            messages.success(request, _(u'Bruker %s ble aktivert. Du kan n\u00e5 logge inn.') % user.username)\n            return redirect('auth_login')\n        else:\n            messages.success(request, _(u'Eposten %s er n\u00e5 verifisert.') % email)\n            return redirect('profiles')\n    else:\n        messages.error(request, _(u'Denne lenken er utl\u00f8pt. Bruk gjenopprett passord for \u00e5 f\u00e5 tilsendt en ny lenke.'))\n        return HttpResponseRedirect('/')        \n            \n\ndef recover(request):\n    if request.user.is_authenticated():\n        messages.error(request, _(u'Gjenoppretning av passord krever at du er logget ut.'))\n        return HttpResponseRedirect('/')\n    else:\n        if request.method == 'POST':\n            form = RecoveryForm(request.POST)\n            if form.is_valid():\n                email_string = form.cleaned_data['email']\n                emails = Email.objects.filter(email=email_string)\n\n                if len(emails) == 0:\n                    messages.error(request, _(u'Denne eposten er ikke registrert i v\u00e5re systemer.'))\n                    return HttpResponseRedirect('/')        \n\n                email = emails[0]\n    \n                # Create the registration token\n                token = uuid.uuid4().hex\n                rt = RegisterToken(user=email.user, email=email.email, token=token)\n                rt.save()\n\n                email_message = _(u\"\"\"\nVi har mottat foresp\u00f8rsel om \u00e5 gjenopprette passordet for kontoen bundet til %s.\nDersom du ikke har bedt om denne handlingen ber vi deg se bort fra denne eposten.\n\nBrukernavn: %s\n\nHvis du \u00f8nsker \u00e5 gjennomf\u00f8re en gjenoppretning av passord, bruk lenken under.\n\nhttp://%s/auth/set_password/%s/\n\nDenne lenken vil v\u00e6re gyldig i 24 timer. Dersom du beh\u00f8ver \u00e5 f\u00e5 tilsendt en ny lenke\nkan dette gj\u00f8res med funksjonen for \u00e5 gjenopprette passord.\n\"\"\") % (email.email, email.user.username, request.META['HTTP_HOST'], token)\n\n                send_mail(_(u'Gjenoppretning av passord'), email_message, settings.DEFAULT_FROM_EMAIL, [email.email,])\n\n                messages.success(request, _(u'En lenke for gjenoppretning har blitt sendt til %s.') % email.email)\n\n                return HttpResponseRedirect('/')        \n            else:\n                form = RecoveryForm(request.POST, auto_id=True)\n        else:\n            form = RecoveryForm()\n\n        return render(request, 'auth/recover.html', {'form': form})\n\n\n@sensitive_post_parameters()\ndef set_password(request, token=None): \n    if request.user.is_authenticated():\n        return HttpResponseRedirect('/')\n    else:\n        rt = get_object_or_404(RegisterToken, token=token)\n       \n        if rt.is_valid:\n            if request.method == 'POST':\n                form = ChangePasswordForm(request.POST, auto_id=True)\n                if form.is_valid():\n                    user = getattr(rt, 'user')\n\n                    user.is_active = True\n                    user.set_password(form.cleaned_data['new_password'])\n                    user.save()\n                    \n                    rt.delete()\n\n                    messages.success(request, _(u'Bruker %s har gjennomf\u00f8rt vellykket gjenoppretning av passord. Du kan n\u00e5 logge inn.') % user.username)\n                    \n                    return HttpResponseRedirect('/')        \n            else:\n                \n                form = ChangePasswordForm()\n\n                messages.success(request, _(u'Lenken er akseptert. Vennligst skriv inn \u00f8nsket passord.'))\n\n            return render(request, 'auth/set_password.html', {'form': form, 'token': token})\n\n        else:\n            messages.error(request, _(u'Lenken er utl\u00f8pt. Vennligst bruk gjenoppretning av passord for \u00e5 f\u00e5 tilsendt en ny lenke.'))\n            return HttpResponseRedirect('/')        \n", "path": "apps/authentication/views.py"}]}