# -*- coding: utf-8 -*-
#!/usr/bin/env python
from bottle import *
import sae
import sae.kvdb

app = Bottle()
kv = sae.kvdb.Client()

def readDiary():
	
	text = []
	for i in kv.get_by_prefix('key:'):
		# here i is tuple access by index
		text.append(i[1]["content"])
	return '\n'.join(text)

def writeDiary(content,tag):
	diary = {"content":content,"tag":tag}
	# diary includes content and tag
	# (dict) access by key
	kv.set('key:'+tag, diary)

def guide():

	temp="Tpye in help or ? to get guide!\n"\
			+"Type in anything else to write diary!\n"\
			+"Submit to see What happened!" 
	return temp

@app.route('/')
def show():
	text = readDiary()
	return template('showdiary.tpl',content=text)

@app.route('/',method='POST')
def new():
	typein=request.forms.get('typein')
	tag=request.forms.get('tag')
	if typein in ["help","?"]:
		text=guide()
	else:
		writeDiary(typein,tag)
		text = readDiary()
	return template('showdiary.tpl',content=text)

#error pages
@app.error(404)
def error404(error):
	return "Nothing here, sorry!"

'''
if __name__ == "__main__":
    debug(True)
    run(host='localhost',port=8080,reloader=True)
'''

application = sae.create_wsgi_app(app)
@@ -157,12 +157,22 @@
 xt =
+ %5B%5D; tag =
  %5B%5D%0A
-
 %09for
@@ -270,16 +270,132 @@
 tent%22%5D)%0A
+%09%09tag.append(i%5B1%5D%5B%22tag%22%5D)%0A%09log = %5B%5D%0A%09for j in range(len(text)):%0A%09%09log.append('Diary: '+text%5Bj%5D+'%5Cn'+'Tag: '+tag%5Bj%5D)%0A
 %09return 
@@ -404,20 +404,19 @@
 n'.join(
-text
+log
 )%0A%0Adef w

add tag