ひながた
今回は、ほんとに基本的な掲示板を作ります。で、どんなものにするかの仕様を考えました。
- googleアカウントがないと見られない
- 新しい順に10件だけ表示する
- 表示は後々のことを考えて、テンプレートエンジンを使う
こんな感じで作っておけば、改造もしやすいでしょう。
この掲示板に足りない機能はかなりあります。その辺りは今後ちょっとずつ機能追加していきたいと思います。
app.yaml
application: bbs version: 1 runtime: python api_version: 1 handlers: - url: .* script: main.py
main.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja-JP" lang="ja-JP"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!-- link rel="stylesheet" type="text/css" href="/css/main.css" / --> </head> <body> <a href="{{ url }}">ログアウト</a> / <hr /> {% for article in articles %} {% if article.body %} {{ article.title }} / 投稿者 : {{ article.author }}<br /> {{ article.date }} / M U<br /> {{ article.body }}<br /> <hr /> {% endif %} {% endfor %} <form method="POST" action="/post"> タイトル:<input type="text" name="title"><br /> 名前:<input type="text" name="author"><br /> 本文:<br /> <textarea name="body" cols="50" rows="5"></textarea><br /> <input type="submit" value="投稿/reload"> <input type="reset" value="書き直し"> </form> </body> </html>
notlogin.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja-JP" lang="ja-JP"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="/css/main.css" /> </head> <body> 利用するには<a href="{{ url }}">ログイン</a>が必要です。 <hr /> </body> </html>
main.py
#!/usr/bin/env python import wsgiref.handlers import os import time import cgi import datetime import re from google.appengine.api import users from google.appengine.ext import webapp from google.appengine.ext import db from google.appengine.ext.webapp import template article_limit = 10 # article_order = 'DESC' # 'ASC' 'DESC' class Article(db.Model): author = db.UserProperty() title = db.StringProperty(multiline=False) body = db.StringProperty(multiline=True) date = db.DateTimeProperty(auto_now_add=True) class PostHandler(webapp.RequestHandler): def post(self): article = Article() self.response.out.write("post") article.body = self.request.get('body').strip() if article.body != "": article.body = re.sub("\n","<br />", article.body) article.author = users.get_current_user().nickname() article.title = self.request.get('title').strip() article.put() self.response.out.write("redir") self.redirect('/') class MainHandler(webapp.RequestHandler): def get(self): if users.get_current_user(): articles = db.GqlQuery ( "SELECT * FROM Article ORDER BY date " + article_order + " LIMIT " + str(article_limit) ) url = users.create_logout_url(self.request.uri) template_values = { 'articles': articles, 'url': url } path = os.path.join(os.path.dirname(__file__), 'main.html') self.response.out.write(template.render(path, template_values)) else: url = users.create_login_url(self.request.uri) template_values = { 'url': url } path = os.path.join(os.path.dirname(__file__), 'notlogin.html') self.response.out.write(template.render(path, template_values)) def main(): application = webapp.WSGIApplication([('/', MainHandler), ('/post', PostHandler) ], debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main()