Merged in Edit Post and Draft functionality.

This commit is contained in:
Raoul Snyman 2010-02-03 13:00:33 +02:00
commit 169ded4bdf
6 changed files with 68 additions and 10 deletions

View File

@ -50,6 +50,10 @@ class PostController(BaseController):
if id is None:
h.redirect_to('/post/new')
c.post = Session.query(Post).get(id)
if len(c.post.tags):
c.post.tags_list = u', '.join([tag.name for tag in c.post.tags])
else:
c.post.tags_list = u''
c.page_title = 'Edit Post: %s' % c.post.title
return render(u'/post/edit.mako')
@ -64,7 +68,10 @@ class PostController(BaseController):
post.modified = datetime.now()
post.title = c.form_values[u'title']
post.body = c.form_values[u'body']
post.status = u'published'
if c.form_values[u'action'] == u'Save Draft':
post.status = u'draft'
else:
post.status = u'published'
post.url = url
tags = c.form_values[u'tags']
tag_list = [tag_name.strip() for tag_name in tags.split(u',')]
@ -78,5 +85,26 @@ class PostController(BaseController):
post.tags.append(Tag(name=tag, url=utils.generate_url(tag)))
Session.add(post)
Session.commit()
h.redirect_to(str('/archive/%s/%s' % (post.created.strftime('%Y/%m/%d'), post.url)))
if c.form_values[u'action'] == u'Save Draft':
h.redirect_to(h.url_for(action=u'draft'))
else:
h.redirect_to(str('/archive/%s/%s' % (post.created.strftime('%Y/%m/%d'), post.url)))
def draft(self):
posts = Session.query(Post)\
.filter_by(status=u'draft')\
.order_by(Post.created.desc())
pagination = utils.paginate(posts, 10,
int(request.GET.get(u'page', 1)), '/')
c.posts = pagination[u'records']
if pagination[u'prev'] != pagination[u'page']:
c.first_page = pagination[u'first']
c.prev_page = pagination[u'prev']
if pagination[u'next'] != pagination[u'page']:
c.next_page = pagination[u'next']
c.last_page = pagination[u'last']
c.list_start = pagination[u'start']
c.list_total = pagination[u'total']
c.list_end = pagination[u'end']
return render(u'/post/draft.mako')

View File

@ -33,6 +33,7 @@
% if c.current_user:
% 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='post', action='draft')}">Draft Posts</a></li>
% endif
<li><a href="${h.url_for(controller='admin', action='logout')}">Logout</a></li>
<li>Logged in as <em>${c.current_user.nick}</em></li>

View File

@ -8,14 +8,19 @@
${h.literal(h.teaser(post.body))}
</div>
<p class="meta">
<span class="byline">Posted by ${post.user.nick} on ${post.created.strftime('%B %d, %Y')}</span>
<a href="${h.url_for_post(post)}" class="read-more">Read more</a>
<span class="byline">
Posted by ${post.user.nick} on ${post.created.strftime('%B %d, %Y')}
% if c.current_user and c.current_user.id == post.user.id and c.current_user.has_permission(u'Edit My Posts'):
[<a href="${h.url_for(controller=u'post', action=u'edit', id=post.id)}">Edit</a>]
% endif
</span>
<a href="${h.url_for_post(post)}" class="read-more">Read more</a>
% if len(post.comments) == 0:
<a href="${h.url_for_post(post)}#comments" class="comments">No comments</a>
<a href="${h.url_for_post(post)}#comments" class="comments">No comments</a>
% elif len(post.comments) == 1:
<a href="${h.url_for_post(post)}#comments" class="comments">1 comment</a>
<a href="${h.url_for_post(post)}#comments" class="comments">1 comment</a>
% else:
<a href="${h.url_for_post(post)}#comments" class="comments">${len(post.comments)} comments</a>
<a href="${h.url_for_post(post)}#comments" class="comments">${len(post.comments)} comments</a>
% endif
</p>
</div>

View File

@ -2,7 +2,12 @@
<%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>
<div class="info">
Posted by ${c.post.user.nick} on ${c.post.created.strftime('%B %d, %Y')}
% if c.current_user and c.current_user.id == c.post.user.id and c.current_user.has_permission(u'Edit My Posts'):
[<a href="${h.url_for(controller=u'post', action=u'edit', id=c.post.id)}">Edit</a>]
% endif
</div>
<div class="entry">
${h.literal(c.post.body)}
</div>

View File

@ -0,0 +1,15 @@
<%inherit file="/base.mako"/>
<%include file="/flash.mako"/>
<h2 class="title">Unpublished blog posts</h2>
% for post in c.posts:
<div class="post">
<h3 class="title"><a href="${h.url_for(controller=u'post', action=u'edit', id=post.id)}">${post.title}</a></h3>
<div class="entry">
${h.literal(h.teaser(post.body))}
</div>
<p class="meta">
<span class="byline">Posted by ${post.user.nick} on ${post.created.strftime('%B %d, %Y')}</span>
<a href="${h.url_for(controller=u'post', action=u'edit', id=post.id)}" class="read-more">Edit post</a>
</p>
</div>
% endfor

View File

@ -18,8 +18,12 @@
<input type="text" name="tags" id="post-tags" class="form-text" value="${c.post.tags_list}" />
</div>
<div class="form-item">
<input type="submit" name="action" value="Save Draft"/>
<input type="submit" name="action" value="Save &amp; Publish"/>
% if c.post.status == u'published':
<input type="submit" name="action" value="Save" class="form-button"/>
% else:
<input type="submit" name="action" value="Save Draft" class="form-button"/>
<input type="submit" name="action" value="Save &amp; Publish" class="form-button"/>
% endif
</div>
</fieldset>
</form>