読者です 読者をやめる 読者になる 読者になる

むずかしいことはわかりません

いろいろ書いてるみたいな

ソース

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wsgiref.handlers
import os
import time
import cgi
import datetime
import re
import urllib
import hashlib

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'
article_start = 0

class Article(db.Model):
	author = db.StringProperty(multiline=False)
	title = db.StringProperty(multiline=False)
	body = db.StringProperty(multiline=True)
	date = db.DateTimeProperty(auto_now_add=True)
	akey = db.StringProperty(multiline=False)

class PostHandler(webapp.RequestHandler):
	def post(self):
		article = Article()
		article.body = self.request.get('body').strip()
		delact = self.request.get('delete').strip()

		if article.body != "":
			if self.request.get('akey').strip() != "":
				existcheck = db.GqlQuery ( "SELECT * FROM Article WHERE akey=:1", self.request.get('akey').strip())

				if existcheck.count() <= 0:
					article.akey = self.request.get('akey')
					article.body = re.sub("\n","<br />", article.body)
					article.author = self.request.get('author')
					if self.request.get('title').strip() != "":
						article.title = self.request.get('title').strip()
					else:
						article.title = ''

					article.put()
		elif delact != "":
			dellist = self.request.get_all('delkey')
			dlsiz = len(dellist)

			for dkey in dellist:
				query = db.GqlQuery ( "SELECT * FROM Article WHERE akey = :1", dkey )
				if query.count() >= 1:
					for article in query:
						article.delete()

		self.redirect('/')

class MainHandler(webapp.RequestHandler):
	def get(self):
		global article_limit
		global article_order
		global article_start

		if users.get_current_user():
			next_page = self.request.get('next')

			if next_page.strip() != "" and next_page.isdigit():
				article_start = int(next_page) - 1

			max_size = db.Query(Article).count()

			next_p = prev_p = 0

			if article_start < 0 or article_start > max_size:
				article_start = 0

			if max_size > article_start + article_limit:
				next_p = article_start + article_limit + 1

			if article_start >= article_limit:
				prev_p = article_start - article_limit + 1
			articles = db.GqlQuery ( "SELECT * FROM Article ORDER BY date " + str(article_order) + " LIMIT " + str(article_start) + "," + str(article_limit) )

			akey = hashlib.md5(time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))).hexdigest()
			url = users.create_logout_url(self.request.uri)
			username = users.get_current_user().nickname()

			if users.is_current_user_admin() == True :
				administrator = True 
			else:
				administrator = False

			template_values = {
				'articles': articles,
				'author': username,
				'akey' : akey,
				'administrator' : administrator,
				'prev' : prev_p,
				'next' : next_p,
				'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()
<!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>{% if administrator %} / 管理者モード {% endif %}
<br />
<a href="?next=1">先頭へ</a> / 
{% if prev  %}<a href="?next={{ prev }}">前のページ</a>{% else %}前のページ{% endif %} / 
{% if next  %}<a href="?next={{ next }}">次のページ</a>{% else %}次のページ{% endif %}
<hr />
<form method="POST" action="/post">
{% for article in articles %}
	{% if article.body %}
{{ article.title|default:"(無題)" }} / 投稿者 : {{ article.author }}<br />
{{ article.date|date:"Y/m/d H:i:s" }}  {% if administrator %}/ <input type="checkbox" name="delkey" value="{{article.akey}}">削除{% endif %}<br />
{{ article.body }}<br />
<hr />
	{% endif %}
{% endfor %}
タイトル:<input type="text" name="title"><br />
名前:{{ author }}<br />
本文:<br />
<textarea name="body" cols="50" rows="5"></textarea><br />
<input type="hidden" value="{{author}}" name="author">
<input type="hidden" value="{{akey}}" name="akey">
<input type="submit" value="投稿/reload" name="post">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="書き直し">
{% if administrator %}
<hr />
<input type="password" name="dpasswd"><input type="submit" value="削除" name="delete">
{% endif %}
</form>

</body>

</html>