From fafce2cfc72f4e1cd14ff6cb693c8ec7854159c5 Mon Sep 17 00:00:00 2001 From: Alvin Li Date: Sun, 6 Oct 2013 13:51:17 +0800 Subject: * added settings 'ASYNC_SEND_MAIL' * added parameter 'async' to 'Account.send_activation_email()' method * improved views 'sfaccount.views.activate_view' * adjusted urls 'activate_key' * added view 'sfaccount.views.activate_done_view' --- 97suifangqa/apps/sfaccount/README.txt | 8 +++- 97suifangqa/apps/sfaccount/forms.py | 51 ++++++++++++++++----- 97suifangqa/apps/sfaccount/models.py | 28 +++++++++-- 97suifangqa/apps/sfaccount/tasks.py | 5 ++ .../sfaccount/templates/sfaccount/activate.html | 15 +++++- .../templates/sfaccount/activate_done.html | 36 +++++++++++++++ .../templates/sfaccount/activate_send_mail.html | 36 +++++++++++++++ .../templates/sfaccount/activation_email_body.html | 2 +- .../templates/sfaccount/activation_email_body.txt | 2 +- .../apps/sfaccount/templates/sfaccount/login.html | 11 +++-- .../templates/sfaccount/password_change_done.html | 2 +- 97suifangqa/apps/sfaccount/urls.py | 10 +++- 97suifangqa/apps/sfaccount/views.py | 51 +++++++++++++++++++-- 97suifangqa/isuifangqa.db | Bin 379904 -> 379904 bytes 97suifangqa/settings.py | 14 ++++-- 15 files changed, 234 insertions(+), 37 deletions(-) create mode 100644 97suifangqa/apps/sfaccount/templates/sfaccount/activate_done.html create mode 100644 97suifangqa/apps/sfaccount/templates/sfaccount/activate_send_mail.html diff --git a/97suifangqa/apps/sfaccount/README.txt b/97suifangqa/apps/sfaccount/README.txt index c55a405..651122c 100644 --- a/97suifangqa/apps/sfaccount/README.txt +++ b/97suifangqa/apps/sfaccount/README.txt @@ -15,11 +15,17 @@ HOWTO run: 5) ajust 'SF_MAIL' settings in 'mail_settings.py' 6) $ redis-server 7) $ ./manage.py syncdb -8) $ ./manage.py celeryd worker -E +8) $ ./manage.py celery worker --loglevel=info 9) $ ./manage.py celerycam (for monitoring) TEST: a) ./manage.py shell >>> from sfaccount.tasks import send_mail >>> send_mail(to, subject, body_text, body_html) + # async task + >>> r = send_mail.delay(to, subject, body_text, body_html) + >>> r.ready() + >>> r.result + >>> r.successful() + >>> diff --git a/97suifangqa/apps/sfaccount/forms.py b/97suifangqa/apps/sfaccount/forms.py index d2a3bf1..5502263 100644 --- a/97suifangqa/apps/sfaccount/forms.py +++ b/97suifangqa/apps/sfaccount/forms.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from django import forms +from django.conf import settings from django.template import loader from django.utils.http import int_to_base36 from django.contrib.auth.models import User @@ -34,8 +35,10 @@ class AccountForm(forms.Form): def clean_username(self): username = self.cleaned_data['username'] # check length - if len(username) < 6: + if len(username) < settings.MIN_USERNAME_LENGTH: raise forms.ValidationError(u'用户名长度需大于6位') + if len(username) > settings.MAX_USERNAME_LENGTH: + raise forms.ValidationError(u'用户名长度不能超过30位') # check first letter p = re.compile('[a-zA-Z_]') if p.match(username[0]): @@ -58,8 +61,11 @@ class AccountForm(forms.Form): def clean_password1(self): password1 = self.cleaned_data['password1'] - if len(password1) < 6: + # check length + if len(password1) < settings.MIN_PASSWORD_LENGTH: raise forms.ValidationError(u'密码长度需大于6位') + if len(password1) > settings.MAX_PASSWORD_LENGTH: + raise forms.ValidationError(u'密码长度不能超过30位') return password1 def clean(self): @@ -79,13 +85,19 @@ class SFPasswordResetForm(forms.Form): to use djcelery's async send mail """ error_messages = { - 'unknown': _("That e-mail address doesn't have an associated " - "user account. Are you sure you've registered?"), + 'unknown': u"该邮箱未在系统中注册,请填写您注册时使用的邮箱地址", 'unusable': _("The user account associated with this e-mail " "address cannot reset the password."), } email = forms.EmailField(label=_("E-mail"), max_length=75) + def clean_email(self): + email = self.cleaned_data['email'] + users = User.objects.filter(email__iexact=email) + if not len(users): + raise forms.ValidationError(self.error_messages['unknown']) + return email + def save(self, domain_override=None, subject_template_name='registration/password_reset_subject.txt', email_template_name='registration/password_reset_email.txt', @@ -96,14 +108,8 @@ class SFPasswordResetForm(forms.Form): Generates a one-use only link for resetting password and sends to the user. """ - # validate first - if not self.is_valid(): - return self - # validated: has 'self.cleaned_data' email = self.cleaned_data['email'] users = User.objects.filter(email__iexact=email) - if not len(users): - raise forms.ValidationError(self.error_messages['unknown']) for user in users: # make sure that no email is sent to a user that actually # has a password marked as unusable @@ -135,7 +141,30 @@ class SFPasswordResetForm(forms.Form): body_html = None # send mail to = user.email - send_mail(to, subject, body_text, body_html) + if getattr(settings, 'ASYNC_SEND_MAIL', False): + send_mail.delay(to, subject, body_text, body_html) + else: + send_mail(to, subject, body_text, body_html) +# }}} + + +# SFEmailForm {{{ +class SFEmailForm(forms.Form): + """ + get input email address and validate it + used in sending activation mail + """ + error_messages = { + 'unknown': u"该邮箱未在系统中注册,请填写您注册时使用的邮箱地址", + } + email = forms.EmailField(label=_("E-mail"), max_length=75) + + def clean_email(self): + email = self.cleaned_data['email'] + users = User.objects.filter(email__iexact=email) + if not len(users): + raise forms.ValidationError(self.error_messages['unknown']) + return email # }}} diff --git a/97suifangqa/apps/sfaccount/models.py b/97suifangqa/apps/sfaccount/models.py index 72904c4..5ebba1c 100644 --- a/97suifangqa/apps/sfaccount/models.py +++ b/97suifangqa/apps/sfaccount/models.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from django.core.urlresolvers import reverse from django.conf import settings from django.db import models from django.contrib import admin @@ -150,22 +151,39 @@ class Account(models.Model): # {{{ self.user.date_joined + expiration_days <= now_utc) # }}} - def send_activation_email(self): # {{{ + def get_activation_url(self): + return reverse('activate_key', + kwargs={'activation_key': self.activation_key}) + + # send_activation_email {{{ + def send_activation_email(self, + async=None, + subject_template_name='sfaccount/activation_email_subject.txt', + email_template_name='sfaccount/activation_email_body.txt', + html_email_template_name='sfaccount/activation_email_body.html'): """ send an activation email to the newly registered user """ ctx_dict = { 'username': self.user.username, 'activation_key': self.activation_key, + 'activation_url': self.get_activation_url(), 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS, } - subject = render_to_string('sfaccount/activation_email_subject.txt', ctx_dict) + subject = render_to_string(subject_template_name, ctx_dict) subject = ''.join(subject.splitlines()) - body_text = render_to_string('sfaccount/activation_email_body.txt', ctx_dict) - body_html = render_to_string('sfaccount/activation_email_body.html', ctx_dict) + body_text = render_to_string(email_template_name, ctx_dict) + body_html = render_to_string(html_email_template_name, ctx_dict) to = self.user.email # send email - send_mail.delay(to, subject, body_text, body_html) + if async is None: + async_send_mail = getattr(settings, 'ASYNC_SEND_MAIL', False) + else: + async_send_mail = async + if async_send_mail: + send_mail.delay(to, subject, body_text, body_html) + else: + send_mail(to, subject, body_text, body_html) # }}} def delete_account(self): diff --git a/97suifangqa/apps/sfaccount/tasks.py b/97suifangqa/apps/sfaccount/tasks.py index 1613646..86cf6ea 100644 --- a/97suifangqa/apps/sfaccount/tasks.py +++ b/97suifangqa/apps/sfaccount/tasks.py @@ -11,3 +11,8 @@ def send_mail(to, subject, content_text=None, content_html=None): content_text=content_text, content_html=content_html) + +@task +def test_add(x, y): + return x + y + diff --git a/97suifangqa/apps/sfaccount/templates/sfaccount/activate.html b/97suifangqa/apps/sfaccount/templates/sfaccount/activate.html index a81af6d..1a6c6f9 100644 --- a/97suifangqa/apps/sfaccount/templates/sfaccount/activate.html +++ b/97suifangqa/apps/sfaccount/templates/sfaccount/activate.html @@ -13,7 +13,8 @@ {% block scripts %} +{% endblock %} + +{% block body %} +

加入97随访   科学了解乙肝治疗

+ +

欢迎加入97随访!

+ +

您的账户已成功激活,现在您可以登录使用。

+

+ +

+{% endblock body %} + + diff --git a/97suifangqa/apps/sfaccount/templates/sfaccount/activate_send_mail.html b/97suifangqa/apps/sfaccount/templates/sfaccount/activate_send_mail.html new file mode 100644 index 0000000..e95a881 --- /dev/null +++ b/97suifangqa/apps/sfaccount/templates/sfaccount/activate_send_mail.html @@ -0,0 +1,36 @@ +{% extends "picture-base.html" %} +{% load staticfiles %} + +{% block title %} +重发激活邮件 | 97 随访 +{% endblock %} + +{% block othercss %} + +{% endblock %} + +{% block body %} +

加入97随访   科学了解乙肝治疗

+ +

+ 还未收到激活邮件? +

+

+ 请在下面输入您注册时使用的邮箱地址, + 我们将为您重新发送激活邮件。 +

+ +
{% csrf_token %} + + + + + +
+ + {{ form.email.errors|join:"" }}
+ +
+{% endblock %} + + diff --git a/97suifangqa/apps/sfaccount/templates/sfaccount/activation_email_body.html b/97suifangqa/apps/sfaccount/templates/sfaccount/activation_email_body.html index 9792ad5..51b1751 100644 --- a/97suifangqa/apps/sfaccount/templates/sfaccount/activation_email_body.html +++ b/97suifangqa/apps/sfaccount/templates/sfaccount/activation_email_body.html @@ -5,7 +5,7 @@

您的激活码为 {{ activation_key }},请在 {{ expiration_days }} 天内激活账户,直接打开以下链接进行激活:
-http://www.97suifang.com/accounts/activate/{{ activation_key }}/ +http://www.97suifang.com{{ activation_url }}


diff --git a/97suifangqa/apps/sfaccount/templates/sfaccount/activation_email_body.txt b/97suifangqa/apps/sfaccount/templates/sfaccount/activation_email_body.txt index b479ff0..44a2c4d 100644 --- a/97suifangqa/apps/sfaccount/templates/sfaccount/activation_email_body.txt +++ b/97suifangqa/apps/sfaccount/templates/sfaccount/activation_email_body.txt @@ -3,7 +3,7 @@ 欢迎您加入97随访(97suifang.com)。 您的激活码为 {{ activation_key }},请在 {{ expiration_days }} 天内激活账户,直接打开以下链接进行激活: -http://www.97suifang.com/accounts/activate/{{ activation_key }}/ +http://www.97suifang.com{{ activation_url }} 97随访 团队 diff --git a/97suifangqa/apps/sfaccount/templates/sfaccount/login.html b/97suifangqa/apps/sfaccount/templates/sfaccount/login.html index f5c7942..6e3be80 100644 --- a/97suifangqa/apps/sfaccount/templates/sfaccount/login.html +++ b/97suifangqa/apps/sfaccount/templates/sfaccount/login.html @@ -47,11 +47,14 @@ -   |   - 还没有帐号? -   |   - 忘记密码? +
+ +

+ 还没有帐号? +   |   + 忘记密码? +

{% endblock body%} {# vim: set ts=2 sw=2 tw=0 fenc=utf-8 ft=htmldjango.html: #} diff --git a/97suifangqa/apps/sfaccount/templates/sfaccount/password_change_done.html b/97suifangqa/apps/sfaccount/templates/sfaccount/password_change_done.html index ed91216..ac22668 100644 --- a/97suifangqa/apps/sfaccount/templates/sfaccount/password_change_done.html +++ b/97suifangqa/apps/sfaccount/templates/sfaccount/password_change_done.html @@ -11,7 +11,7 @@ {% block scripts %}