# -*-Encoding: UTF-8 -*-
""" Basic blog using webpy 0.3 """
Import Web
Import Config
Import Controllers
App = Web. Application (config. URLs, globals ())
If _ Name __ = ' _ Main __ ' :
App. Run ()
Class Index:
Def Get (Self ):
Web. header ( ' Content-Type ' , ' Text/html; charsets = UTF-8 ' )
Web. setcookie ( " Age " , 30 )
Posts = Model. get_posts ()
Return Render. Index (posts, Web. Cookies ())
Class View:
Def Get (self, ID ):
""" View sing post """
Post = Model. get_post (INT (ID ))
Print Datetime. datetime. strptime ( ' 2009-9-9 16:56:58 ' , ' % Y-% m-% d % H: % m: % s ' )
Print Datetime. datetime. Now (). strftime ( ' % Y-% m-% d % H: % m: % s ' )
Return Render. View (post)
Class New:
# Vtitle = web. Form. RegEx (R ". {3, 20} $", 'title length: 3 to 20 characters ')
Form = Web. Form. Form (
Web. Form. Textbox ( ' Title ' , Web. Form. notnull,
Size = 30 ,
Description = U " Title " ),
Web. Form. textarea ( ' Content ' , Web. Form. notnull,
Rows = 30 , Cols = 80 ,
Description = U " Content " ),
Web. Form. Button ( ' Submit ' , Type = " Submit " , Html = U ' Submit ' ),
)
Def Get (Self ):
Form = Self. Form ()
Return Render. New (form)
Def Post (Self ):
Form = Self. Form ()
If Not Form. validates ():
Return Render. New (form)
Model. new_post (Form. D. Title, form. D. Content)
Raise Web. seeother ( ' / ' )
Class Delete:
Def Post (self, ID ):
Model. del_post (INT (ID ))
Raise Web. seeother ( ' / ' )
Class Edit:
Def Get (self, ID ):
Post = Model. get_post (INT (ID ))
Form = New. Form ()
Form. Fill (post)
Return Render. Edit (post, form)
Def Post (self, ID ):
Form = New. Form ()
Post = Model. get_post (INT (ID ))
If Not Form. validates ():
Return Render. Edit (post, form)
Model. update_post (INT (ID), form. D. Title, form. D. Content)
Raise Web. seeother ( ' / ' )
Def Notfound ():
Return Web. notfound ( " Sorry, the page you were looking for was not found. " )
Model. py
Import Web, datetime
DB = Web. Database (DBN = ' SQLite ' , DB = ' Blog. DB ' )
Def Get_posts ():
Return DB. Select ( ' Entries ' , Order = ' Id DESC ' )
Def Get_post (ID ):
Try :
Return DB. Select ( ' Entries ' , Where = ' Id = $ ID ' , Vars = Locals () [0]
Except Indexerror:
Return None
Def New_post (title, text ):
DB. insert ( ' Entries ' , Title = Title, content = Text, post_on = Datetime. datetime. utcnow ())
Def Del_post (ID ):
DB. Delete ( ' Entries ' , Where = " Id = $ ID " , Vars = Locals ())
Def Update_post (ID, title, text ):
DB. Update ( ' Entries ' , Where = " Id = $ ID " , Vars = Locals (),
Title = Title, content = text)