1
- import post_class , comment_class
1
+ import post_class , comment_class , image_class
2
2
import re , os , cherrypy
3
3
4
4
class Blog (object ):
@@ -8,10 +8,18 @@ def __init__(self, author = "Anonymous", url = "http://127.0.0.1"):
8
8
self .url = url
9
9
self .getPosts ()
10
10
11
+ def isAuth (self ):
12
+ if cherrypy .session .get ('authenticated' ) == True :
13
+ return True
14
+ else :
15
+ return False
16
+
11
17
def getPosts (self ):
12
18
self .posts = []
13
19
expression = re .compile ('\d*' )
14
- for file in os .listdir (os .getcwd () + '/posts/' ):
20
+ post_filenames = os .listdir (os .getcwd () + '/posts/' )
21
+ post_filenames .sort (reverse = True )
22
+ for file in post_filenames :
15
23
if expression .match (file ) != None :
16
24
currentPost = post_class .Post ()
17
25
filehandle = open ('posts/' + file , 'r' )
@@ -22,76 +30,134 @@ def getPosts(self):
22
30
23
31
def index (self ):
24
32
page = []
25
- header = open ('theme/header.php' , 'r' )
26
- page .append (header .read ())
27
- header .close ()
33
+ # read header
34
+ try :
35
+ header = open ('theme/header.html' , 'r' )
36
+ page .append (header .read ())
37
+ finally :
38
+ header .close ()
39
+
40
+ if self .isAuth ():
41
+ page .append ('<div class="createnew">+</div>' )
28
42
43
+ # iterate through post list
29
44
for post in self .posts :
30
45
page .append (post .createPost ())
31
46
32
- footer = open ('theme/footer.php' , 'r' )
33
- page .append (footer .read ())
34
- footer .close ()
47
+ # read footer
48
+ try :
49
+ footer = open ('theme/footer.html' , 'r' )
50
+ page .append (footer .read ())
51
+ finally :
52
+ footer .close ()
53
+
54
+ # join the pieces into a single string and return
35
55
page = '\n \n ' .join (page )
36
56
return page
37
57
index .exposed = True
38
58
39
59
def about (self ):
40
- f = open ('pages/about.html' , 'r' )
41
- contents = f .read ()
42
- f .close ()
60
+ try :
61
+ f = open ('pages/about.html' , 'r' )
62
+ contents = f .read ()
63
+ finally :
64
+ f .close ()
43
65
return contents
44
66
about .exposed = True
45
67
46
68
def resume (self ):
47
- f = open ('pages/resume.html' , 'r' )
48
- contents = f .read ()
49
- f .close ()
69
+ try :
70
+ f = open ('pages/resume.html' , 'r' )
71
+ contents = f .read ()
72
+ finally :
73
+ f .close ()
50
74
return contents
51
75
resume .exposed = True
52
76
53
77
def post (self , id ):
54
- header = open ('theme/header.php' , 'r' )
55
- yield header .read ()
56
- header .close ()
78
+ try :
79
+ header = open ('theme/header.html' , 'r' )
80
+ yield header .read ()
81
+ finally :
82
+ header .close ()
57
83
58
84
for post in self .posts :
59
85
if post .date == id :
60
86
yield post .createPost (True )
61
87
break
62
88
63
- footer = open ('theme/footer.php' , 'r' )
64
- yield footer .read ()
65
- footer .close ()
89
+ try :
90
+ footer = open ('theme/footer.html' , 'r' )
91
+ yield footer .read ()
92
+ finally :
93
+ footer .close ()
66
94
post .exposed = True
67
95
68
96
def ajaxedit (self , id , width , height ):
97
+ if not self .isAuth ():
98
+ raise cherrypy .HTTPRedirect ('/' )
99
+
69
100
for post in self .posts :
70
101
if post .date == id :
71
102
return post .createEditor (width , height )
72
103
ajaxedit .exposed = True
73
104
105
+ def loginwidget (self ):
106
+ try :
107
+ f = open ('theme/loginwidget.html' )
108
+ yield f .read ()
109
+ finally :
110
+ f .close ()
111
+ loginwidget .exposed = True
112
+
113
+ def login (self , username , password ):
114
+ if username == 'paul' and password == 'ponies' :
115
+ cherrypy .session ['authenticated' ] = True
116
+ raise cherrypy .HTTPRedirect ('/' )
117
+ login .exposed = True
118
+
119
+ def logout (self ):
120
+ cherrypy .session .clear ()
121
+ raise cherrypy .HTTPRedirect ('/' )
122
+ logout .exposed = True
123
+
74
124
@cherrypy .tools .staticdir (root = os .getcwd (), dir = 'files' )
75
125
def files (self ):
76
126
yield '<h2>Browsing directory /files</h2>\n '
77
127
for dirpath , dirnames , filenames in os .walk (os .getcwd ()+ '/files' ):
78
128
filenames .sort ()
79
- # yield dirnames
80
- # for dirname in dirnames:
81
- # yeild '<a href="/files/%s">%s</a><br>' % (dirname, dirname)
82
129
for filename in filenames :
83
130
yield '<a href="/files/%s">%s</a><br>' % (filename , filename )
84
131
files .exposed = True
85
132
86
133
def edit (self , post_title , post_body , post_date ):
134
+ if not self .isAuth ():
135
+ raise cherrypy .HTTPRedirect ('/' )
87
136
for post in self .posts :
88
137
if post .date == post_date :
89
138
post .editPost (post_title , post_body )
90
139
return 'Updated.'
140
+
141
+ # if it is not an existing post, then save it as a new post.
142
+ post = post_class .Post ()
143
+ post .editPost (post_title , post_body )
144
+ # add that post to the post cache
145
+ self .getPosts ()
146
+ return 'New Post Saved.'
147
+ #raise cherrypy.HTTPRedirect('/')
91
148
edit .exposed = True
92
149
93
150
def ajaxget (self , id ):
151
+ if not self .isAuth ():
152
+ raise cherrypy .HTTPRedirect ('/' )
94
153
for post in self .posts :
95
154
if post .date == id :
96
155
return post .createPost (False )
97
156
ajaxget .exposed = True
157
+
158
+ def ajaxnewpost (self , width , height ):
159
+ if not self .isAuth ():
160
+ raise cherrypy .HTTPRedirect ('/' )
161
+ post = post_class .Post (title = 'New Post' , body = 'This is a new post' )
162
+ return post .createEditor (width , height )
163
+ ajaxnewpost .exposed = True
0 commit comments