diff --git a/accounts/forms.py b/accounts/forms.py index a95e353..d8621b0 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -16,6 +16,7 @@ from django.contrib.auth.forms import AuthenticationForm, UserCreationForm from django.forms import widgets from django.conf import settings from django.contrib.auth import get_user_model +from django.core.exceptions import ValidationError class LoginForm(AuthenticationForm): @@ -37,6 +38,12 @@ class RegisterForm(UserCreationForm): self.fields['password2'].widget = widgets.PasswordInput( attrs={'placeholder': "repeat password", "class": "form-control"}) + def clean_email(self): + email = self.cleaned_data['email'] + if get_user_model().objects.filter(email=email).exists(): + raise ValidationError("该邮箱已经存在.") + return email + class Meta: model = get_user_model() fields = ("username", "email") diff --git a/accounts/tests.py b/accounts/tests.py index 0d196d6..37bbcee 100644 --- a/accounts/tests.py +++ b/accounts/tests.py @@ -6,6 +6,7 @@ import datetime from accounts.models import BlogUser from django.urls import reverse from DjangoBlog.utils import * +from django.conf import settings # Create your tests here. @@ -53,6 +54,12 @@ class AccountTest(TestCase): 'password2': 'password123!q@wE#R$T', }) self.assertEquals(1, len(BlogUser.objects.filter(email='user123@user.com'))) + user = BlogUser.objects.filter(email='user123@user.com')[0] + sign = get_md5(get_md5(settings.SECRET_KEY + str(user.id))) + path = reverse('accounts:result') + url = '{path}?type=validation&id={id}&sign={sign}'.format(path=path, id=user.id, sign=sign) + response = self.client.get(url) + self.assertEqual(response.status_code, 200) self.client.login(username='user1233', password='password123!q@wE#R$T') user = BlogUser.objects.filter(email='user123@user.com')[0] diff --git a/accounts/urls.py b/accounts/urls.py index 8f812e6..a8d1832 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -15,14 +15,15 @@ from django.conf.urls import url from django.contrib.auth import views as auth_view - +from django.urls import path from . import views from .forms import LoginForm -app_name="accounts" +app_name = "accounts" urlpatterns = [ url(r'^login/$', views.LoginView.as_view(success_url='/'), name='login', kwargs={'authentication_form': LoginForm}), url(r'^register/$', views.RegisterView.as_view(success_url="/"), name='register'), - url(r'^logout/$', views.LogoutView.as_view(), name='logout') + url(r'^logout/$', views.LogoutView.as_view(), name='logout'), + path(r'account/result.html', views.account_result, name='result') ] diff --git a/accounts/views.py b/accounts/views.py index 0e64e16..4845782 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -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 = """ +
请点击下面链接验证您的邮箱
+ + {url} + + 再次感谢您! +