Skip to content

Commit

Permalink
Fix #33 登陆时, 如果邮件地址/密码错误, 都提示邮件/密码错误;补全登陆页面国际化
Browse files Browse the repository at this point in the history
  • Loading branch information
greatghoul committed Jun 22, 2013
1 parent 86e1bf0 commit 0cf54e7
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 31 deletions.
50 changes: 27 additions & 23 deletions website/scriptfan/forms/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,47 @@

from flask.ext import wtf
from flask.ext.login import current_user
from flask.ext.babel import gettext as _
from scriptfan.models import User
from scriptfan.forms.base import RedirectForm
from flask.ext.openid import COMMON_PROVIDERS

class SigninForm(RedirectForm):

class SignupForm(RedirectForm):
"""用户注册表单"""

email = wtf.TextField('email', validators=[
wtf.Required(message=u'请填写电子邮件'),
wtf.Email(message=u'无效的电子邮件')])
password = wtf.PasswordField('password', validators=[
nickname = wtf.TextField('nickname', validators=[
wtf.Required(message=u'请填写昵称')])
password1 = wtf.PasswordField('password1', validators=[
wtf.Required(message=u'请填写密码')])
password2 = wtf.PasswordField('password2', validators=[
wtf.Required(message=u'再次填写密码'),
wtf.EqualTo('password1', message=u'两次输入的密码不一致')])

def validate_email(form, field):
if User.get_by_email(field.data):
raise wtf.ValidationError(u'该邮箱已被注册')


class SigninForm(RedirectForm):
""" 用户登陆表单 """

email = wtf.TextField('email', validators=[
wtf.Required(message=_('forms.signin.errors.require_email')),
wtf.Email(message=_('forms.signin.errors.invalid_email'))])
password = wtf.PasswordField('password', validators=[
wtf.Required(message=_('forms.signin.errors.require_password'))])
remember = wtf.BooleanField('remember')

def validate(self):
if not super(RedirectForm, self).validate(): return False

user = User.get_by_email(self.email.data)
if not user:
self.email.errors.append(u'该邮箱未注册')
elif not user.check_password(self.password.data):
self.password.errors.append(u'密码错误')
if not user or user.check_password(self.password.data):
self.email.errors.append(_('forms.signin.errors.invalid_email_or_password'))
else:
self.user = user

Expand Down Expand Up @@ -59,23 +80,6 @@ class ResetStep2Form(RedirectForm):
wtf.EqualTo('password', message=u'两次输入的密码不一致')])


class SignupForm(RedirectForm):
email = wtf.TextField('email', validators=[
wtf.Required(message=u'请填写电子邮件'),
wtf.Email(message=u'无效的电子邮件')])
nickname = wtf.TextField('nickname', validators=[
wtf.Required(message=u'请填写昵称')])
password1 = wtf.PasswordField('password1', validators=[
wtf.Required(message=u'请填写密码')])
password2 = wtf.PasswordField('password2', validators=[
wtf.Required(message=u'再次填写密码'),
wtf.EqualTo('password1', message=u'两次输入的密码不一致')])

def validate_email(form, field):
if User.get_by_email(field.data):
raise wtf.ValidationError(u'该邮箱已被注册')


class EditProfileForm(RedirectForm):
nickname = wtf.TextField('nickname', validators=[
wtf.Required(message=u'请填写昵称')])
Expand Down
14 changes: 7 additions & 7 deletions website/scriptfan/templates/users/signin.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "layout.html" %}

{% block title %}会员登陆{% endblock %}
{% block title %}{{ _('views.users.signin.title') }}{% endblock %}

{% block content %}
<div class="content">
Expand All @@ -11,26 +11,26 @@ <h1>{{ self.title() }}</h1>
<form action="{{ url_for('users.signin') }}" class="form-horizontal" id="form-signin" method="post">
{{ form.hidden_tag() }}
<div class="control-group {{ form.email | error_class }}">
<label class="control-label">邮件</label>
<label class="control-label">{{ _('models.user.email') }}</label>
<div class="controls">
{{ form.email }}
<span class="help-inline">{{ form.email | error_text }}</span>
</div>
</div>
<div class="control-group {{ form.password | error_class }}">
<label class="control-label">密码</label>
<label class="control-label">{{ _('models.user.password') }}</label>
<div class="controls">
{{ form.password }}
<span class="help-inline">{{ form.password | error_text }}</span>
<span class="help-inline"><a href="{{ url_for('users.reset_step1') }}">忘记密码?</a></span>
<span class="help-inline"><a href="{{ url_for('users.reset_step1') }}">{{ _('forms.signin.messages.password_lost') }}</a></span>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">用户登陆</button>&nbsp;&nbsp;
<button type="submit" class="btn btn-primary">{{ _('forms.signin.buttons.submit') }}</button>&nbsp;&nbsp;
<label class="checkbox inline">
{{ form.remember }} 记住我的登陆信息
{{ form.remember }} {{ _('forms.signin.fields.remember') }}
</label>
<div>使用 <a href="{{ url_for('users.openid', provider='google') }}">Gmail帐户</a> 登陆</div>
<div><a href="{{ url_for('users.openid', provider='google') }}">{{ _('links.signin_with_google') }}</a></div>
</div>
</form><!-- #form-signin -->
</div><!-- .content -->
Expand Down
42 changes: 42 additions & 0 deletions website/scriptfan/translations/zh_CN/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ msgstr "还没有%%s记录!"
msgid "views.users.signup.confirm_signup"
msgstr "同意用户协议并注册"

msgid "views.users.signin.title"
msgstr "用户登录"

msgid "views.users.signin.signin_success"
msgstr "登录成功!"

msgid "views.users.slug.title"
msgstr "设置个性域名"

Expand Down Expand Up @@ -109,3 +115,39 @@ msgstr "内容"

msgid "submit"
msgstr "提交"

## 实体相关信息

msgid "models.user.email"
msgstr "邮箱"

msgid "models.user.password"
msgstr "密码"

## 表单及相关信息

# SigninForm

msgid "forms.signin.errors.invalid_email_or_password"
msgstr "邮箱或者密码错误"

msgid "forms.signin.errors.require_email"
msgstr "请填写邮箱"

msgid "forms.signin.errors.invalid_email"
msgstr "无效的邮箱"

msgid "forms.signin.errors.require_password"
msgstr "请填写密码"

msgid "forms.signin.fields.remember"
msgstr "记住我的登陆信息"

msgid "forms.signin.buttons.submit"
msgstr "登录"

msgid "forms.signin.messages.password_lost"
msgstr "忘记密码?"

msgid "links.signin_with_google"
msgstr "使用Google帐户登陆"
3 changes: 2 additions & 1 deletion website/scriptfan/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask.ext import login
from flask.ext.login import current_user
from flask.ext.openid import COMMON_PROVIDERS
from flask.ext.babel import gettext as _
from flask.ext.principal import Identity, AnonymousIdentity, identity_changed, identity_loaded
from flask_mail import Message

Expand Down Expand Up @@ -147,7 +148,7 @@ def signin():
app.logger.info('Signin users: %s', form.email.data)
login_user(form.user, remember=form.remember)
identity_changed.send(app._get_current_object(), identity=Identity(current_user.user.id))
flash(u'登陆成功', 'success')
flash(_('views.users.signin.signin_success'), 'success')
return form.redirect('users.profile')

return render_template('users/signin.html', form=form)
Expand Down
1 change: 1 addition & 0 deletions website/start.bat
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
title ScriptFan Development Server
echo Staring ScriptFan dev server...
python manage.py runserver -H localhost
pause

0 comments on commit 0cf54e7

Please sign in to comment.