We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Error reports should not contain sensitive data
https://docs.djangoproject.com/en/dev/howto/error-reporting/#filtering-error-reports

Here is a guide how to do it.


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

{"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@@ -9,12 +9,15 @@\n from django.shortcuts import render, redirect, get_object_or_404\n from django.http import HttpResponseRedirect\n from django.utils.translation import ugettext as _\n+from django.views.decorators.debug import sensitive_post_parameters\n \n from django.conf import settings\n from apps.authentication.forms import (LoginForm, RegisterForm, \n                             RecoveryForm, ChangePasswordForm)\n from apps.authentication.models import OnlineUser as User, RegisterToken, Email\n \n+\n+@sensitive_post_parameters()\n def login(request):\n     redirect_url = request.REQUEST.get('next', '')\n     if request.method == 'POST':\n@@ -31,11 +34,14 @@\n     response_dict = { 'form' : form, 'next' : redirect_url}\n     return render(request, 'auth/login.html', response_dict)\n \n+\n def 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()\n def register(request):\n     if request.user.is_authenticated():\n         messages.error(request, _(u'Registrering av ny konto krever at du er logget ut.'))\n@@ -100,6 +106,7 @@\n \n         return render(request, 'auth/register.html', {'form': form, })\n \n+\n def verify(request, token):\n     rt = get_object_or_404(RegisterToken, token=token)\n     \n@@ -181,6 +188,8 @@\n \n         return render(request, 'auth/recover.html', {'form': form})\n \n+\n+@sensitive_post_parameters()\n def set_password(request, token=None): \n     if request.user.is_authenticated():\n         return HttpResponseRedirect('/')\n", "issue": "Error reports should not contain sensitive data\nhttps://docs.djangoproject.com/en/dev/howto/error-reporting/#filtering-error-reports\n\nHere is a guide how to do it.\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 _\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\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\ndef logout(request):\n    auth.logout(request)\n    messages.success(request, _(u'Du er n\u00e5 logget ut.'))\n    return HttpResponseRedirect('/')\n\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\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.username, 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\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'].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.username, 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"}]}