|
|
|
|
@ -5,7 +5,8 @@ from django.contrib.auth import authenticate, login, logout
|
|
|
|
|
# from django.views.generic.edit import FormView
|
|
|
|
|
from django.views.generic import FormView, RedirectView
|
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
from django.http import HttpResponseRedirect, HttpResponseForbidden
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
|
|
|
|
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
|
|
|
|
@ -16,6 +17,8 @@ from django.shortcuts import redirect
|
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
|
from django.views.decorators.debug import sensitive_post_parameters
|
|
|
|
|
from django.utils.http import is_safe_url
|
|
|
|
|
from DjangoBlog.utils import send_email, get_md5, get_current_site
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
@ -27,10 +30,37 @@ class RegisterView(FormView):
|
|
|
|
|
template_name = 'account/registration_form.html'
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
user = form.save(False)
|
|
|
|
|
user.save(True)
|
|
|
|
|
url = reverse('accounts:login')
|
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
if form.is_valid():
|
|
|
|
|
user = form.save(False)
|
|
|
|
|
user.is_active = False
|
|
|
|
|
user.save(True)
|
|
|
|
|
site = get_current_site().domain
|
|
|
|
|
sign = get_md5(get_md5(settings.SECRET_KEY + str(user.id)))
|
|
|
|
|
|
|
|
|
|
if settings.DEBUG:
|
|
|
|
|
site = '127.0.0.1:8000'
|
|
|
|
|
path = reverse('account:result')
|
|
|
|
|
url = "http://{site}{path}?type=validation&id={id}&sign={sign}".format(site=site, path=path, id=user.id,
|
|
|
|
|
sign=sign)
|
|
|
|
|
|
|
|
|
|
content = """
|
|
|
|
|
<p>请点击下面链接验证您的邮箱</p>
|
|
|
|
|
|
|
|
|
|
<a href="{url}" rel="bookmark">{url}</a>
|
|
|
|
|
|
|
|
|
|
再次感谢您!
|
|
|
|
|
<br />
|
|
|
|
|
如果上面链接无法打开,请将此链接复制至浏览器。
|
|
|
|
|
{url}
|
|
|
|
|
""".format(url=url)
|
|
|
|
|
send_email(emailto=[user.email, ], title='验证您的电子邮箱', content=content)
|
|
|
|
|
|
|
|
|
|
url = reverse('accounts:result') + '?type=register&id=' + str(user.id)
|
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
else:
|
|
|
|
|
return self.render_to_response({
|
|
|
|
|
'form': form
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LogoutView(RedirectView):
|
|
|
|
|
@ -91,3 +121,36 @@ class LoginView(FormView):
|
|
|
|
|
if not is_safe_url(url=redirect_to, allowed_hosts=[self.request.get_host()]):
|
|
|
|
|
redirect_to = self.success_url
|
|
|
|
|
return redirect_to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def account_result(request):
|
|
|
|
|
type = request.GET.get('type')
|
|
|
|
|
id = request.GET.get('id')
|
|
|
|
|
|
|
|
|
|
user = get_object_or_404(get_user_model(), id=id)
|
|
|
|
|
logger.info(type)
|
|
|
|
|
if user.is_active:
|
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
|
if type and type in ['register', 'validation']:
|
|
|
|
|
if type == 'register':
|
|
|
|
|
content = '''
|
|
|
|
|
恭喜您注册成功,一封验证邮件已经发送到您 {email} 的邮箱,请验证您的邮箱后登录本站。
|
|
|
|
|
'''.format(email=user.email)
|
|
|
|
|
title = '注册成功'
|
|
|
|
|
else:
|
|
|
|
|
c_sign = get_md5(get_md5(settings.SECRET_KEY + str(user.id)))
|
|
|
|
|
sign = request.GET.get('sign')
|
|
|
|
|
if sign != c_sign:
|
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
user.is_active = True
|
|
|
|
|
user.save()
|
|
|
|
|
content = '''
|
|
|
|
|
恭喜您已经成功的完成邮箱验证,您现在可以使用您的账号来登录本站。
|
|
|
|
|
'''
|
|
|
|
|
title = '验证成功'
|
|
|
|
|
return render(request, 'account/result.html', {
|
|
|
|
|
'title': title,
|
|
|
|
|
'content': content
|
|
|
|
|
})
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
|
|