I'm implementing an admin interface for my app (only a few models, so
not a huge deal). Part of the app has blog-like news feed, where the
page for each individual news item has the slug of the new item title
as the url, meaning the slug must be unique. There's probably a
better way to do this, so if anyone wants to critique my code feel
free, but it seems to work fine. Here's the code I use for
adding/editing news items...
def post(self):
content = self.request.get('content')
id = self.request.get('id')
title = self.request.get('title')
slug = slugify(title)
news = db.Query(News)
news.filter('slug =', slug)
foo = news.fetch(limit=1)
if foo:
redir = "/admin/news/error"
else:
if id:
news = News.get_by_id(int(id))
news.content = content
news.updated = datetime.datetime.now()
else:
news = News(author = users.get_current_user(),
content = content, slug = slug,
title = title)
news.put()
redir = "/admin/news/"
self.redirect(redir)