2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from django
.shortcuts
import render_to_response
19 from django
.http
import HttpResponseRedirect
20 from django
.contrib
.auth
import authenticate
, login
21 from django
.contrib
.auth
.models
import User
22 from django
.contrib
.auth
.decorators
import login_required
23 from django
.template
.defaultfilters
import slugify
24 from django
.template
import RequestContext
25 from mygpo
.api
.models
import UserProfile
26 from mygpo
.web
.forms
import RestorePasswordForm
27 from django
.contrib
.sites
.models
import Site
28 from django
.conf
import settings
29 from mygpo
.decorators
import manual_gc
, allowed_methods
30 from django
.utils
.translation
import ugettext
as _
31 from registration
.models
import RegistrationProfile
36 from mygpo
.web
.forms
import ResendActivationForm
37 from mygpo
.constants
import DEFAULT_LOGIN_REDIRECT
39 def login_user(request
):
40 # Do not show login page for already-logged-in users
41 if request
.user
.is_authenticated():
42 return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT
)
44 if 'user' not in request
.POST
or 'pwd' not in request
.POST
:
45 if request
.GET
.get('restore_password', False):
46 form
= RestorePasswordForm()
50 return render_to_response('login.html', {
51 'url': Site
.objects
.get_current(),
52 'next': request
.GET
.get('next', ''),
53 'restore_password_form': form
,
54 }, context_instance
=RequestContext(request
))
56 username
= request
.POST
['user']
57 password
= request
.POST
['pwd']
58 user
= authenticate(username
=username
, password
=password
)
61 return render_to_response('login.html', {
62 'error_message': _('Wrong username or password.'),
63 'next': request
.POST
.get('next', ''),
64 }, context_instance
=RequestContext(request
))
66 if not user
.is_active
:
68 p
, c
= UserProfile
.objects
.get_or_create(user
=user
)
71 return render_to_response('login.html', {
72 'error_message': _('You have deleted your account, but you can register again')
73 }, context_instance
=RequestContext(request
))
76 return render_to_response('login.html', {
77 'error_message': _('Please activate your account first.'),
78 'activation_needed': True,
79 }, context_instance
=RequestContext(request
))
84 if user
.get_profile().generated_id
:
85 site
= Site
.objects
.get_current()
86 return render_to_response('migrate.html', {
89 }, context_instance
=RequestContext(request
))
91 except UserProfile
.DoesNotExist
:
92 profile
, c
= UserProfile
.objects
.get_or_create(user
=user
)
94 if 'next' in request
.POST
and request
.POST
['next'] and request
.POST
['next'] != '/login/':
95 return HttpResponseRedirect(request
.POST
['next'])
97 return HttpResponseRedirect(DEFAULT_LOGIN_REDIRECT
)
100 def migrate_user(request
):
102 username
= request
.POST
.get('username', user
.username
)
105 username
= user
.username
107 if user
.username
!= username
:
108 current_site
= Site
.objects
.get_current()
109 if User
.objects
.filter(username__exact
=username
).count() > 0:
110 return render_to_response('migrate.html', {
111 'error_message': '%s is already taken' % username
,
113 'username': user
.username
114 }, context_instance
=RequestContext(request
))
116 if slugify(username
) != username
.lower():
117 return render_to_response('migrate.html', {
118 'error_message': '%s is not a valid username. Please use characters, numbers, underscore and dash only.' % username
,
120 'username': user
.username
121 }, context_instance
=RequestContext(request
))
124 user
.username
= username
127 user
.get_profile().generated_id
= 0
128 user
.get_profile().save()
130 return HttpResponseRedirect('/')
132 def get_user(username
, email
):
134 return User
.objects
.get(username
=username
)
136 return User
.objects
.get(email
=email
)
138 raise User
.DoesNotExist('neither username nor email provided')
141 @allowed_methods(['POST'])
142 def restore_password(request
):
143 form
= RestorePasswordForm(request
.POST
)
144 if not form
.is_valid():
145 return HttpResponseRedirect('/login/')
148 user
= get_user(form
.cleaned_data
['username'], form
.cleaned_data
['email'])
150 except User
.DoesNotExist
:
151 error_message
= _('User does not exist.')
152 return render_to_response('password_reset_failed.html', {
153 'error_message': error_message
154 }, context_instance
=RequestContext(request
))
156 site
= Site
.objects
.get_current()
157 pwd
= "".join(random
.sample(string
.letters
+string
.digits
, 8))
158 subject
= _('Reset password for your account on %s') % site
159 message
= _('Here is your new password for your account %(username)s on %(site)s: %(password)s') % {'username': user
.username
, 'site': site
, 'password': pwd
}
160 user
.email_user(subject
, message
, settings
.DEFAULT_FROM_EMAIL
)
161 user
.set_password(pwd
)
163 return render_to_response('password_reset.html', context_instance
=RequestContext(request
))
167 @allowed_methods(['GET', 'POST'])
168 def resend_activation(request
):
171 if request
.method
== 'GET':
172 form
= ResendActivationForm()
173 return render_to_response('registration/resend_activation.html', {
175 }, context_instance
=RequestContext(request
))
177 site
= Site
.objects
.get_current()
178 form
= ResendActivationForm(request
.POST
)
181 if not form
.is_valid():
182 raise ValueError(_('Invalid Username entered'))
185 user
= get_user(form
.cleaned_data
['username'], form
.cleaned_data
['email'])
186 except User
.DoesNotExist
:
187 raise ValueError(_('User does not exist.'))
189 p
, c
= UserProfile
.objects
.get_or_create(user
=user
)
191 raise ValueError(_('You have deleted your account, but you can regster again.'))
194 profile
= RegistrationProfile
.objects
.get(user
=user
)
195 except RegistrationProfile
.DoesNotExist
:
196 profile
= RegistrationProfile
.objects
.create_profile(user
)
198 if profile
.activation_key
== RegistrationProfile
.ACTIVATED
:
199 user
.is_active
= True
201 raise ValueError(_('Your account already has been activated. Go ahead and log in.'))
203 elif profile
.activation_key_expired():
204 raise ValueError(_('Your activation key has expired. Please try another username, or retry with the same one tomorrow.'))
206 except ValueError, e
:
207 return render_to_response('registration/resend_activation.html', {
210 }, context_instance
=RequestContext(request
))
214 profile
.send_activation_email(site
)
216 except AttributeError:
217 #old versions of django-registration send registration mails from RegistrationManager
218 RegistrationProfile
.objects
.send_activation_email(profile
, site
)
220 return render_to_response('registration/resent_activation.html', context_instance
=RequestContext(request
))