Adding registration and activation.

This commit is contained in:
Raoul Snyman 2010-01-30 23:55:23 +02:00
commit 00256870f8
14 changed files with 98 additions and 20 deletions

View File

@ -5,3 +5,5 @@ scribeengine.sqlite
posts.sql
*.egg-info
ScrivbeEngine.e4p
build
dist

View File

@ -43,6 +43,7 @@ security.salt = secretsalt
mail.on = false
mail.manager = immediate
mail.transport = smtp
mail.smtp.server = mail.mydomain.com
mail.smtp.username = mymailusername
mail.smtp.password = mymailpassword

View File

@ -1 +0,0 @@
/home/raoul/VirtualEnv/ScribeEngine/feeds/bin/paster serve development.ini --reload

View File

@ -23,6 +23,7 @@
import logging
import string
import random
from datetime import datetime
from scribeengine.lib.base import *
from scribeengine.lib.validation.client import JSString, JSEmail
@ -47,7 +48,7 @@ class AdminController(BaseController):
return {
u'email': JSEmail(required=True, message=u'You haven\'t typed in an e-mail address.'),
u'password': JSString(required=True, message=u'You haven\'t typed in a password.'),
u'confirm-password': JSString(required=True, equalTo=u'password', message=u'Your passwords don\'t match.')
u'confirm-password': JSString(required=True, equalTo=u'#password', message=u'Your passwords don\'t match.')
}
def register_schema(self):
@ -67,8 +68,55 @@ class AdminController(BaseController):
)
Session.add(user)
Session.commit()
blog_mail = Session.query(Variable).get(u'blog mail')
blog_title = Session.query(Variable).get(u'blog title')
blog_host = Session.query(Variable).get(u'blog host')
if not blog_host:
url = u'%s://%s' % (request.environ[u'wsgi.url_scheme'],
request.environ[u'HTTP_HOST'])
blog_host = Variable(key=u'blog host', value=url)
Session.add(blog_host)
Session.commit()
utils.send_mail(u'/email/activate.mako', u'%s <%s>' % (user.nick, user.email),
u'%s <%s>' % (blog_mail.value, blog_title.value),
u'[%s] Activate your account!' % blog_title.value,
{
'user': user,
'blog_title': blog_title.value,
'blog_host': blog_host.value
})
h.flash.set_message(u'An e-mail has been sent to your e-mail address. '
u'Please activate your account by clicking on the link in your '
u'e-mail.', u'success')
h.redirect_to('/')
def activate(self, id=None):
activation_code = request.GET.get('code')
if not activation_code:
h.flash.set_message(u'Your activation code was missing or '
u'incorrect. Please check your activation e-mail.', u'error')
h.redirect_to(h.url_for(action=u'register'))
if not id:
h.flash.set_message(u'Your username was missing or incorrect. '
u'Please check your activation e-mail.', u'error')
h.redirect_to(h.url_for(action=u'register'))
user = Session.query(User)\
.filter_by(id=id)\
.filter_by(activation_key=activation_code)\
.first()
if not user:
h.flash.set_message(u'Your username or activation code is '
u'incorrect. Please check your activation e-mail.', u'error')
h.redirect_to(h.url_for(action=u'register'))
user.activation_key = None
user.modified = datetime.now()
Session.add(user)
Session.commit()
h.flash.set_message(u'Your account has been activated! Please log in '
u'with your e-mail address and the password you typed in during '
u'registration.', u'success')
h.redirect_to(h.url_for(action=u'login'))
def login(self):
c.page_title = u'Login'
return render(u'/admin/login.mako')
@ -90,11 +138,14 @@ class AdminController(BaseController):
log.debug('Logging in as "%s" with password "%s"', c.form_values[u'email'], c.form_values[u'password'])
user = Session.query(User).filter_by(email=c.form_values[u'email']).first()
password = utils.hash_password(c.form_values[u'password'])
log.debug(user)
if not user or user.password != password:
log.debug('Username or password are incorrect.')
h.flash.set_message(u'Your username or password are incorrect.', u'error')
h.redirect_to('/admin/login')
h.redirect_to(h.url_for(action=u'login'))
elif user and user.activation_key is not None:
log.debug('Unactivated account.')
h.flash.set_message(u'Your account has not yet been activated. Please check your e-mail for a link to activate your account.', u'error')
h.redirect_to(h.url_for(action=u'login'))
elif user and user.password == password:
log.debug('Logged in successfully.')
redirect_url = str(session.get(u'redirect_url', u'/'))
@ -109,10 +160,11 @@ class AdminController(BaseController):
del session[u'REMOTE_USER']
session.save()
h.flash.set_message(u'There was a problem logging you in.', u'error')
h.redirect_to('/admin/login')
h.redirect_to(h.url_for(action=u'login'))
def logout(self):
del session[u'REMOTE_USER']
session.save()
h.flash.set_message(u'You have logged out successfully.', u'success')
h.redirect_to('/')

View File

@ -27,10 +27,10 @@ import string
from random import choice
from datetime import datetime
from pylons import config
from pylons import config, c
from turbomail import Message
from scribeengine.lib.base import render, c
from scribeengine.lib.base import render
def send_mail(template, mail_to, mail_from, subject, variables={}, attachments=[]):
"""

View File

@ -100,10 +100,10 @@ users_table = Table(u'users', metadata,
Column(u'email', Unicode(200), nullable=False, index=True),
Column(u'password', Unicode(64), nullable=False),
Column(u'nick', Unicode(50), nullable=False, index=True),
Column(u'first_name', Unicode(100)),
Column(u'last_name', Unicode(100)),
Column(u'homepage', Unicode(200)),
Column(u'activation_key', Unicode(40))
Column(u'first_name', Unicode(100), default=u''),
Column(u'last_name', Unicode(100), default=u''),
Column(u'homepage', Unicode(200), default=u''),
Column(u'activation_key', Unicode(40), default=None)
)
# Definition of the "variables" table

View File

@ -404,6 +404,10 @@ fieldset {
margin-bottom: 1em;
}
#register-now {
margin-left: 1em;
}
/* Message areas */
#form-errors {
@ -414,8 +418,7 @@ fieldset {
}
#message {
margin-top: 1.8em;
/*padding: 0.3em 0.5em;*/
margin-bottom: 1.8em;
}
#message p {

View File

@ -1,7 +1,7 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<div class="post">
<h2 class="title">Log in</h2>
<%include file="/flash.mako"/>
<%include file="/errors.mako"/>
<form id="post-new" action="${h.url_for(controller='admin', action='login')}" method="post">
<fieldset>
@ -15,6 +15,7 @@
</div>
<div class="form-item">
<input type="submit" name="action" value="Login" class="form-button"/>
<span id="register-now">No account? <a href="${h.url_for(controller=u'admin', action=u'register')}" title="register now">Register now!</a></span>
</div>
</fieldset>
</form>

View File

@ -1,7 +1,7 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<div class="post">
<h2 class="title">Register</h2>
<%include file="/flash.mako"/>
<%include file="/errors.mako"/>
<form id="post-new" action="${h.url_for(controller='admin', action='register')}" method="post">
<fieldset>
@ -22,7 +22,7 @@
<input type="password" name="confirm-password" id="register-confirm-password" class="form-text" />
</div>
<div class="form-item">
<input type="submit" name="action" value="Register"/>
<input type="submit" name="action" value="Register" class="form-button" />
</div>
</fieldset>
</form>

View File

@ -1,5 +1,4 @@
<div class="post">
<%include file="/flash.mako"/>
<h2 class="title"><a href="${h.url_for_post(post)}">${post.title}</a></h2>
<div class="entry">
${h.literal(post.body)}

View File

@ -1,8 +1,8 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<div class="post">
<h2 class="title">${c.post.title}</h2>
<div class="info">Posted by ${c.post.user.nick} on ${c.post.created.strftime('%B %d, %Y')}</div>
<%include file="/flash.mako"/>
<div class="entry">
${h.literal(c.post.body)}
</div>

View File

@ -0,0 +1,21 @@
Dear ${c.user.nick},
You have just registered on ${c.blog_title}, but before you continue, you will
need to activate your account. You can do this by simply clicking on the link
below, or copying and pasting it into your browser.
${c.blog_host}${h.url_for(controller=u'admin', action=u'activate', id=c.user.id, code=c.user.activation_key)}
If this is not you, simply leave this e-mail, and the account will expire after
72 hours. If you left this e-mail for too long and want to continue with the
registration process, click on the link below, type in your e-mail addres, and
another e-mail will be sent to you to activate your account.
${c.blog_host}${h.url_for(controller=u'admin', action=u'reset', id=c.user.id)}
Once you have completed the registration process you will be able to comment on
the posts on the site.
Kind regards,
${c.blog_title} Team

View File

@ -1,7 +1,7 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<div class="post">
<h2 class="title">Edit Post: ${c.post.title}</h2>
<%include file="/flash.mako"/>
<%include file="/errors.mako"/>
<form id="post-new" action="${h.url_for(controller='post',action='edit', id=c.post.id)}" method="post">
<fieldset>

View File

@ -1,7 +1,7 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<div class="post">
<h2 class="title">New Post</h2>
<%include file="/flash.mako"/>
<%include file="/errors.mako"/>
<form id="post-new" action="${h.url_for('/post/edit')}" method="post">
<fieldset>