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
, logout
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 registration
.forms
import RegistrationForm
25 from registration
.views
import activate
, register
26 from registration
.models
import RegistrationProfile
27 from mygpo
.api
.models
import UserProfile
28 from mygpo
.web
.forms
import RestorePasswordForm
29 from django
.contrib
.sites
.models
import Site
30 from django
.conf
import settings
31 from django
.utils
.translation
import ugettext
as _
35 def login_user(request
):
37 username
= request
.POST
['user']
38 password
= request
.POST
['pwd']
40 current_site
= Site
.objects
.get_current()
41 next
= request
.GET
.get('next', '')
42 return render_to_response('login.html', {
47 user
= authenticate(username
=username
, password
=password
)
50 form
= RestorePasswordForm()
51 return render_to_response('login.html', {
52 'error_message': _('Unknown user or wrong password'),
53 'restore_password_form': form
56 if not user
.is_active
:
57 return render_to_response('login.html', {
58 'error_message': _('Please activate your user first.'),
59 'activation_needed': True
63 current_site
= Site
.objects
.get_current()
66 if user
.get_profile().generated_id
:
67 return render_to_response('migrate.html', {
71 except UserProfile
.DoesNotExist
:
72 profile
, c
= UserProfile
.objects
.get_or_create(user
=user
)
74 if 'next' in request
.POST
and request
.POST
['next'] and request
.POST
['next'] != '/login/':
75 return HttpResponseRedirect(request
.POST
['next'])
77 return HttpResponseRedirect('/')
80 def migrate_user(request
):
82 username
= request
.POST
.get('username', user
.username
)
85 username
= user
.username
87 if user
.username
!= username
:
88 current_site
= Site
.objects
.get_current()
89 if User
.objects
.filter(username__exact
=username
).count() > 0:
90 return render_to_response('migrate.html', {'error_message': '%s is already taken' % username
, 'url': current_site
, 'username': user
.username
})
91 if slugify(username
) != username
.lower():
92 return render_to_response('migrate.html', {'error_message': '%s is not a valid username. Please use characters, numbers, underscore and dash only.' % username
, 'url': current_site
, 'username': user
.username
})
94 user
.username
= username
97 user
.get_profile().generated_id
= 0
98 user
.get_profile().save()
100 return HttpResponseRedirect('/')
102 def get_user(username
, email
):
104 return User
.objects
.get(username
=username
)
106 return User
.objects
.get(email
=email
)
108 raise User
.DoesNotExist('neither username nor email provided')
110 def restore_password(request
):
112 if request
.method
!= 'POST':
113 return HttpResponseRedirect('/login/')
115 form
= RestorePasswordForm(request
.POST
)
116 if not form
.is_valid():
117 return HttpResponseRedirect('/login/')
120 user
= get_user(form
.cleaned_data
['username'], form
.cleaned_data
['email'])
122 except User
.DoesNotExist
:
123 error_message
= _('User does not exist.')
124 return render_to_response('password_reset_failed.html', {
125 'error_message': error_message
128 site
= Site
.objects
.get_current()
129 pwd
= "".join(random
.sample(string
.letters
+string
.digits
, 8))
130 subject
= _('Reset password for your account on %s') % site
131 message
= _('Here is your new password for your account on %(site)s: %(password)s') % {'site': site
, 'password': pwd
}
132 user
.email_user(subject
, message
, settings
.DEFAULT_FROM_EMAIL
)
133 user
.set_password(pwd
)
135 return render_to_response('password_reset.html')