from google.appengine.ext import db from google.appengine.ext import webapp from google.appengine.api import users from google.appengine.ext.webapp.util import run_wsgi_app from google.appengine.ext.webapp import template import os,datetime class data(db.Model): id=db.IntegerProperty(default=0) title=db.StringProperty() author=db.UserProperty() content=db.StringProperty(multiline=True) create_time=db.DateTimeProperty() class mainpage(webapp.RequestHandler): def get(self): article=data.all().order('-create_time').fetch(10) user=users.get_current_user() if user: url=users.create_logout_url('/') dis="welcome %s <a href=%s >logout</a>" %(user,url) else: url=users.create_login_url('/') dis="<a href=%s >login</a>" % url values={'article':article,'dis':dis,'user':user} self.response.out.write(template.render('index.html',values)) class add_article(webapp.RequestHandler): def post(self): tem=data() title=self.request.get('title') content=self.request.get('content') author=users.get_current_user() if title and content and author: tem.id=tem.all().count()+1 tem.title=title tem.author=author tem.content=content tem.create_time=datetime.datetime.now() tem.put() self.response.out.write('save succeful !!!<a href="/">home</a>') else: self.response.out.write('something error') class to_add(webapp.RequestHandler): def get(self): user=users.get_current_user() self.response.out.write(template.render('add.html',{'user':user})) class article_manage(webapp.RequestHandler): def get(self): user=users.get_current_user() articles=data.all().order('-create_time').fetch(10) value={'user':user,'articles':articles} self.response.out.write(template.render('manage.html',value)) class detail(webapp.RequestHandler): def get(self,a): dd=int(a) tem=db.GqlQuery('select * from data where id=:1' ,dd)[0] user=users.get_current_user() value={'tem':tem,'user':user} self.response.out.write(template.render('detail.html',value)) class del_article(webapp.RequestHandler): def get(self,a): dd=int(a) tem=db.GqlQuery('select * from data where id=:1' ,dd)[0] user=users.get_current_user() if user and tem: tem.delete() self.redirect('/manage/') class edit_article(webapp.RequestHandler): def get(self,a): dd=int(a) tem=db.GqlQuery('select * from data where id=:1' ,dd)[0] user=users.get_current_user() value={'tem':tem,'user':user} self.response.out.write(template.render('edit.html',value)) class save_article(webapp.RequestHandler): def post(self,a): dd=int(a) tem=db.GqlQuery('select * from data where id=:1' ,dd)[0] title=self.request.get('title') content=self.request.get('content') author=users.get_current_user() if title and content and author: tem.title=title tem.author=author tem.content=content tem.create_time=datetime.datetime.now() tem.put() self.response.out.write('save succeful !!!<a href="/">home</a>') else: self.response.out.write('something error') application = webapp.WSGIApplication( [('/', mainpage), ('/add/',to_add), ('/sign/',add_article), ('/manage/',article_manage), ('/n%3D(.*)',detail), ('/manage/del/n%3D(.*)',del_article), ('/manage/edit/n%3D(.*)',edit_article), ('/manage/save/n%3D(.*)',save_article), ], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() index.html:
<style>a{text-decoration:none}</style> {{ dis }} {% if user %} <a href='/add/' > 写文章</a> <a href='/manage/'> 管理文章</a> {% endif %} <br> <br> {% if article %} {% for i in article %} <div>title:{{i.title}}</div> <div>author:{{i.author}}</div> <div>create time: {{ i.create_time }}</div> <pre>{{i.content}}</pre> {% endfor %} {% else %} 暂时没有文章 {% endif %}
manage.html:
<style>a{text-decoration:none}</style> {{ n}} {% if user %} welcome {{user}} <a href='/' >主页</a> <a href='/add/' > 写文章</a> <a href='/manage/'> 管理文章</a> <br> {% if articles %} {% for i in articles %} <a href="/n={{ i.id }}" >{{ i.title }}</a> <a href="/manage/del/n={{ i.id }}">删除</a> <a href="/manage/edit/n={{ i.id }}">修改</a> <br> {% endfor %} {% else %} 暂时没有文章<br> {% endif %} {% else %} please login first !!!<a href='/'>home</a> {% endif %}
detail.html:
<style>a{text-decoration:none}</style> {{ n }} {% if user %} <div>welcome {{user}}</div> <a href='/'> 主页</a> <a href='/add/' > 写新文章</a> <a href='/manage/'> 管理文章</a> {% endif %} {% if tem %} <div>title:{{ tem.title }}</div> <div>author:{{ tem.author }}</div> <div>create_time{{ tem.create_time }}</div> <pre>{{ tem.content }}</pre> {% else %} 没有这样的文章 {% endif %}
edit.html:
<style>a{text-decoration:none}</style> {%if user %} welcome {{ user }} <a href='/'>主页</a><br> <form action='/manage/save/n={{ tem.id }}' method='post'> <div>title:<input type='text' name='title' value="{{ tem.title }}"></div> <div><textarea name='content' rows="30" cols="100" >{{ tem.content }}</textarea></div> <input type='submit' value='edit article'> </form> {% else %} you have to login to write aritle <a href='/'>home</a> {% endif %}
add.html:
{%if user %} welcome {{ user }} <a href='/'>主页</a><br> <form action='/sign/' method='post'> <div>title:<input type='text' name='title'></div> <div><textarea name='content' rows="30" cols="100" ></textarea></div> <input type='submit' value='add article'> </form> {% else %} you have to login to write aritle <a href='/'>home</a> {% endif %}
app.yaml:
application: oncyberspace version: 1 runtime: python api_version: 1 handlers: - url: .* script: cms.py
写了一个小小的脚本,用于增加,删除,修改文章。暂时只有这些功能:http://oncyberspace.appspot.com