Renamed "AdminController" to "AccountController" and fixed up some bugs relating to url generation.

This commit is contained in:
Raoul Snyman 2010-02-07 00:03:29 +02:00
parent 90621b2051
commit a1af325882
7 changed files with 71 additions and 20 deletions

View File

@ -37,7 +37,7 @@ log = logging.getLogger(__name__)
class AccountController(BaseController): class AccountController(BaseController):
def index(self): def index(self):
h.redirect_to('/account/login') h.redirect_to(h.url_for(controller=u'account', action=u'login'))
def register(self): def register(self):
c.page_title = u'Register' c.page_title = u'Register'
@ -95,11 +95,11 @@ class AccountController(BaseController):
if not activation_code: if not activation_code:
h.flash.set_message(u'Your activation code was missing or ' h.flash.set_message(u'Your activation code was missing or '
u'incorrect. Please check your activation e-mail.', u'error') u'incorrect. Please check your activation e-mail.', u'error')
h.redirect_to(h.url_for(action=u'register')) h.redirect_to(h.url_for(controller=u'account', action=u'register'))
if not id: if not id:
h.flash.set_message(u'Your username was missing or incorrect. ' h.flash.set_message(u'Your username was missing or incorrect. '
u'Please check your activation e-mail.', u'error') u'Please check your activation e-mail.', u'error')
h.redirect_to(h.url_for(action=u'register')) h.redirect_to(h.url_for(controller=u'account', action=u'register'))
user = Session.query(User)\ user = Session.query(User)\
.filter_by(id=id)\ .filter_by(id=id)\
.filter_by(activation_key=activation_code)\ .filter_by(activation_key=activation_code)\
@ -115,7 +115,7 @@ class AccountController(BaseController):
h.flash.set_message(u'Your account has been activated! Please log in ' 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'with your e-mail address and the password you typed in during '
u'registration.', u'success') u'registration.', u'success')
h.redirect_to(h.url_for(action=u'login')) h.redirect_to(h.url_for(controller=u'account', action=u'login'))
def login(self): def login(self):
c.page_title = u'Login' c.page_title = u'Login'
@ -141,11 +141,11 @@ class AccountController(BaseController):
if not user or user.password != password: if not user or user.password != password:
log.debug('Username or password are incorrect.') log.debug('Username or password are incorrect.')
h.flash.set_message(u'Your username or password are incorrect.', u'error') h.flash.set_message(u'Your username or password are incorrect.', u'error')
h.redirect_to(h.url_for(action=u'login')) h.redirect_to(h.url_for(controller=u'account', action=u'login'))
elif user and user.activation_key is not None: elif user and user.activation_key is not None:
log.debug('Unactivated account.') 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.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')) h.redirect_to(h.url_for(controller=u'account', action=u'login'))
elif user and user.password == password: elif user and user.password == password:
log.debug('Logged in successfully.') log.debug('Logged in successfully.')
redirect_url = str(session.get(u'redirect_url', u'/')) redirect_url = str(session.get(u'redirect_url', u'/'))
@ -160,7 +160,7 @@ class AccountController(BaseController):
del session[u'REMOTE_USER'] del session[u'REMOTE_USER']
session.save() session.save()
h.flash.set_message(u'There was a problem logging you in.', u'error') h.flash.set_message(u'There was a problem logging you in.', u'error')
h.redirect_to(h.url_for(action=u'login')) h.redirect_to(h.url_for(controller=u'account', action=u'login'))
def logout(self): def logout(self):
del session[u'REMOTE_USER'] del session[u'REMOTE_USER']

View File

@ -43,19 +43,19 @@ class PostController(BaseController):
@authenticate(u'Add Posts') @authenticate(u'Add Posts')
def new(self): def new(self):
c.page_title = 'New Post' c.page_title = u'New Post'
return render(u'/post/new.mako') return render(u'/post/new.mako')
@authenticate(u'Edit My Posts') @authenticate(u'Edit My Posts')
def edit(self, id=None): def edit(self, id=None):
if id is None: if id is None:
h.redirect_to('/post/new') h.redirect_to(h.url_for(controller=u'post', action=u'new'))
c.post = Session.query(Post).get(id) c.post = Session.query(Post).get(id)
if len(c.post.tags): if len(c.post.tags):
c.post.tags_list = u', '.join([tag.name for tag in c.post.tags]) c.post.tags_list = u', '.join([tag.name for tag in c.post.tags])
else: else:
c.post.tags_list = u'' c.post.tags_list = u''
c.page_title = 'Edit Post: %s' % c.post.title c.page_title = u'Edit Post: %s' % c.post.title
return render(u'/post/edit.mako') return render(u'/post/edit.mako')
@authenticate(u'Edit My Posts') @authenticate(u'Edit My Posts')
@ -87,9 +87,9 @@ class PostController(BaseController):
Session.add(post) Session.add(post)
Session.commit() Session.commit()
if c.form_values[u'action'] == u'Save Draft': if c.form_values[u'action'] == u'Save Draft':
h.redirect_to(h.url_for(action=u'draft')) h.redirect_to(h.url_for(controller=u'post', action=u'draft'))
else: else:
h.redirect_to(str('/archive/%s/%s' % (post.created.strftime('%Y/%m/%d'), post.url))) h.redirect_to(h.url_for_post(post))
def draft(self): def draft(self):
posts = Session.query(Post)\ posts = Session.query(Post)\

View File

@ -80,8 +80,8 @@ def teaser(text):
def url_for_post(post): def url_for_post(post):
#TODO: this is hard coded. #TODO: this is hard coded.
return url_for( return url_for(
controller='blog', controller=u'blog',
action='view', action=u'view',
year=post.created.strftime('%Y'), year=post.created.strftime('%Y'),
month=post.created.strftime('%m'), month=post.created.strftime('%m'),
day=post.created.strftime('%d'), day=post.created.strftime('%d'),

View File

@ -0,0 +1,22 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<div class="post">
<h2 class="title">Log in</h2>
<%include file="/errors.mako"/>
<form id="post-new" action="${h.url_for(controller=u'account', action=u'login')}" method="post">
<fieldset>
<div class="form-item">
<label for="login-email">E-mail:</label>
<input type="text" name="email" id="login-email" class="form-text" />
</div>
<div class="form-item">
<label for="login-password">Password:</label>
<input type="password" name="password" id="login-password" class="form-text" />
</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'account', action=u'register')}" title="register now">Register now!</a></span>
</div>
</fieldset>
</form>
</div>

View File

@ -0,0 +1,29 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<div class="post">
<h2 class="title">Register</h2>
<%include file="/errors.mako"/>
<form id="post-new" action="${h.url_for(controller=u'account', action=u'register')}" method="post">
<fieldset>
<div class="form-item">
<label for="register-nick">Nick:</label>
<input type="text" name="nick" id="register-nick" class="form-text" />
</div>
<div class="form-item">
<label for="register-email">E-mail:</label>
<input type="text" name="email" id="register-email" class="form-text" />
</div>
<div class="form-item">
<label for="register-password">Password:</label>
<input type="password" name="password" id="register-password" class="form-text" />
</div>
<div class="form-item">
<label for="register-confirm-password">Confirm Password:</label>
<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" class="form-button" />
</div>
</fieldset>
</form>
</div>

View File

@ -32,13 +32,13 @@
% endfor % endfor
% if c.current_user: % if c.current_user:
% if c.current_user.has_permission('Add Posts'): % if c.current_user.has_permission('Add Posts'):
<li><a href="${h.url_for(controller='post', action='new')}">New Post</a></li> <li><a href="${h.url_for(controller=u'post', action=u'new')}">New Post</a></li>
<li><a href="${h.url_for(controller='post', action='draft')}">Draft Posts</a></li> <li><a href="${h.url_for(controller=u'post', action=u'draft')}">Draft Posts</a></li>
% endif % endif
<li><a href="${h.url_for(controller='admin', action='logout')}">Logout</a></li> <li><a href="${h.url_for(controller=u'account', action=u'logout')}">Logout</a></li>
<li>Logged in as <em>${c.current_user.nick}</em></li> <li>Logged in as <em>${c.current_user.nick}</em></li>
% else: % else:
<li><a href="${h.url_for(controller='admin', action='login')}">Login</a></li> <li><a href="${h.url_for(controller=u'account', action=u'login')}">Login</a></li>
% endif % endif
</ul> </ul>
</div> </div>

View File

@ -53,7 +53,7 @@
% if c.post.comment_status == u'open': % if c.post.comment_status == u'open':
<h3 id="respond">Leave a Reply</h3> <h3 id="respond">Leave a Reply</h3>
% if not c.current_user: % if not c.current_user:
<p>You must be <a href="${h.url_for(controller='admin', action='login')}">logged in</a> to post a comment.</p> <p>You must be <a href="${h.url_for(controller=u'account', action=u'login')}">logged in</a> to post a comment.</p>
% else: % else:
<form action="${h.url_for(controller='blog',action='comment', id= c.post.id)}" method="post" id="commentform"> <form action="${h.url_for(controller='blog',action='comment', id= c.post.id)}" method="post" id="commentform">
<p class="user-details">Logged in as <em>${c.current_user.nick}</em>. <a href="${h.url_for(controller='admin',action='logout')}" title="Log out of this account">Logout &raquo;</a></p> <p class="user-details">Logged in as <em>${c.current_user.nick}</em>. <a href="${h.url_for(controller='admin',action='logout')}" title="Log out of this account">Logout &raquo;</a></p>