-
Notifications
You must be signed in to change notification settings - Fork 12
/
blog.py
341 lines (288 loc) · 11.8 KB
/
blog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import cgi, os
from functools import wraps
import wsgiref.handlers
from google.appengine.ext.webapp import template, \
WSGIApplication
from google.appengine.api import users
import webapp as webapp2
from google.appengine.ext import webapp
from google.appengine.ext import db
from model import *
def rebuild_cache(blog):
"""
Pre-Render's and cache's html in blog object. Everything
in here doesn't change very often, so we can update it at point of change
"""
pages = Entry.all().filter("entrytype =", "page").filter("published =", True).fetch(20)
archives = Archive.all().order('-date').fetch(10)
recententries = Entry.all().filter('entrytype =','post').filter("published", True).order('-date').fetch(10)
links = Link.all().filter('linktype =','blogroll')
template_vals = {'recententries':recententries,'pages':pages,
'links':links,'archives':archives}
path = os.path.join(os.path.dirname(__file__), 'views/sidebar.html')
blog.sidebar = template.render(path, template_vals)
path = os.path.join(os.path.dirname(__file__), 'views/topmenu.html')
blog.topmenu = template.render(path, template_vals)
blog.save()
def requires_admin(method):
@wraps(method)
def wrapper(self, *args, **kwargs):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
return
raise self.error(403)
elif not users.is_current_user_admin():
return self.error(403)
else:
return method(self, *args, **kwargs)
return wrapper
def printinfo(method):
@wraps(method)
def wrapper(self, *args, **kwargs):
print self #.__name__
print dir(self)
for x in self.__dict__:
print x
return method(self, *args, **kwargs)
return wrapper
class BaseController(webapp2.RequestHandler):
def __init__(self):
self.template = 'index.html'
self.blog = Blog.all().fetch(1)
if self.blog == []:
pass
else:
self.blog = self.blog[0]
current_userisadmin = False
if users.get_current_user() and users.is_current_user_admin():
current_userisadmin = True
self.template_vals = {'current_userisadmin':current_userisadmin}
def __before__(self,*args):
pass
def __after__(self,*args):
pass
def error(self,errorcode,message='an error occured'):
if errorcode == 404:
message = 'Sorry, we were not able to find the requested page. We have logged this error and will look into it.'
elif errorcode == 403:
message = 'Sorry, that page is reserved for administrators. '
elif errorcode == 500:
message = "Sorry, the server encountered an error. We have logged this error and will look into it."
return self.render('views/error.html',{'message':message})
def render(self,template_file,template_vals):
"""
Helper method to render the appropriate template
"""
if self.blog == None and self.request.path != "/admin/setup":
# no blog configured
self.redirect('/admin/setup')
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
self.template_vals.update({'url':url,
'url_linktext':url_linktext,
'blog':self.blog
})
self.template_vals.update(template_vals)
path = os.path.join(os.path.dirname(__file__), template_file)
self.response.out.write(template.render(path, self.template_vals))
class BasePublicPage(BaseController):
"""
Do all the common public page prep such as nav pages etc
"""
def __before__(self,slug=None):
#self.template_vals.update(public_page_gets())
pass
class MainPage(BasePublicPage):
#@printinfo
def get(self,slug=None):
entries = []
if slug == None:
entries = Entry.all().filter('entrytype =','post').\
filter("published =", True).order('-date').fetch(10)
else:
entries = Entry.all().filter('slug', slug).fetch(1)
if not entries or len(entries) == 0:
return self.error(404)
self.render('views/index.html',{'entries':entries,'slug':slug})
class PublicPage(BasePublicPage):
def get(self,slug=None):
entries = Entry.all().filter('slug', slug).filter("published =", True)
self.render('views/page.html',{'entries':entries})
class ArchivePage(BasePublicPage):
def get(self,monthyear=None):
entries = Entry.all().filter('monthyear', monthyear).\
filter("published =", True).filter('entrytype','post').order('-date')
self.render('views/index.html',{'entries':entries})
class TagPage(BasePublicPage):
def get(self,tags=None):
entries = Entry.all().filter("tags =", tags).filter('entrytype =','post').order('-date')
self.render('views/index.html',{'entries':entries})
class FeedHandler(BaseController):
def get(self,tags=None):
entries = Entry.all().filter('entrytype =','post').order('-date').fetch(10)
if entries and entries[0]:
last_updated = entries[0].date
last_updated = last_updated.strftime("%Y-%m-%dT%H:%M:%SZ")
for e in entries:
e.formatted_date = e.date.strftime("%Y-%m-%dT%H:%M:%SZ")
self.response.headers['Content-Type'] = 'application/atom+xml'
self.render('views/atom.xml',{'entries':entries,'last_updated':last_updated})
class AdminConfig(BaseController):
@requires_admin
def get(self):
blogedit = Blog.all().fetch(1)
#blog = Blog.all()
if blogedit:
blogedit = blogedit[0]
else:
blogedit = Blog()
blogedit.save()
blogedit.initialsetup()
self.render('views/setup.html',{'blog':blogedit,'blogclass':Blog})
@requires_admin
def post(self):
key = self.request.get('object_key')
if key == None or key == '': #new
blog = Blog()
else: # edit
blog = db.get(db.Key(key))
if users.get_current_user():
blog.owner = users.get_current_user()
blog.description = self.request.get('description')
blog.title = self.request.get('title')
blog.subtitle = self.request.get('subtitle')
blog.layout = self.request.get('layout')
blog.baseurl = self.request.get('baseurl')
blog.feedurl = self.request.get('feedurl')
blog.area1 = self.request.get('area1')
blog.area2 = self.request.get('area2')
blog.area3 = self.request.get('area3')
blog.analyticsjs = self.request.get('analyticsjs')
blog.commentjs = self.request.get('commentjs')
blog.save()
self.redirect('/admin/entry/list/post')
class AdminEntry(BaseController):
@requires_admin
def get(self,entrytype='post',key=None):
entry = None
if key == None or key == '':
entry = Entry()
entry.entrytype = entrytype
else:
entry = db.get(db.Key(key))
self.render('views/admin.html',{'entry':entry,'entries':[]})
@requires_admin
def post(self,entrytype='post',key=None):
entry = None
if key == None or key == '':
entry = Entry(blog=self.blog)
entry.entrytype = self.request.get('entrytype')
else:
entry = db.get(db.Key(key))
if users.get_current_user():
entry.author = users.get_current_user()
entry.content = self.request.get('content')
entry.published = bool(int(self.request.get('published')))
entry.title = self.request.get('title')
entry.slug = self.request.get('real_permalink')
entry.tagswcommas = self.request.get('tags')
entry.save()
rebuild_cache(self.blog)
self.redirect('/admin/entry/list/%s' % entry.entrytype )
@requires_admin
def delete(self,key=None):
entry = None
if key == None or key == '':
print 'whoops'
else:
entry = db.get(db.Key(key))
entry.delete()
rebuild_cache(self.blog)
class AdminList(BaseController):
@requires_admin
def get(self,entrytype='post',template_vals={}):
entries = Entry.all().filter('entrytype =',entrytype).order('-date')
self.render('views/admin.html',{'entries':entries})
class AdminMigrate(BaseController):
@requires_admin
def get(self,to_version='1.15'):
if to_version == '999.99':
pass
elif to_version == '1.18':
archives = Archive.all()
for a in archives:
a.delete()
entries = Entry.all().filter('entrytype =','post').filter('published',True)
for e in entries:
e.published = False
e.save()
self.blog.entrycount = 0 # reset to start migration
self.blog.put()
for e in entries:
e.published = True
e.save()
print 'update to %s' % (e.monthyear)
self.blog.blogversion = to_version
self.blog.put()
elif to_version == '1.17':
links = Link.all()
for l in links:
l.delete()
elif to_version == '1.16':
archives = Archive.all()
for a in archives:
a.delete()
entries = Entry.all().filter('entrytype =','post')
self.blog.entrycount = 0 # reset to start migration
for e in entries:
e.update_archive()
e.monthyear = e.date.strftime('%b-%Y')
self.blog.entrycount += 1
e.put()
print 'update to %s' % (e.monthyear)
archive = Archive.all()
self.blog.blogversion = to_version
self.blog.put()
class AdminLinks(BaseController):
@requires_admin
def get(self,linktype='blogroll'):
links = Link.all()#.filter('linktype =',linktype)
link = Link(href='',linktext='',linktype='blogroll')
self.render('views/linkadmin.html',{'entries':None,'links':links,
"link":link})
@requires_admin
def post(self,linktype='blogroll',key=None):
link = None
if key == None or key == '':
link = Link(blog=self.blog)
else:
link = db.get(db.Key(key))
link.linktext = self.request.get('linktext')
link.href = self.request.get('href')
link.put()
rebuild_cache(self.blog)
self.response.out.write('link %s added' % link.linktype)
def main():
application = webapp2.WSGIApplication2(
[('/', MainPage),
('/admin', AdminList),('/admin/', AdminList),
('/admin/entry/list/(.*)', AdminList),
('/admin/entry/(.*)/(.*)', AdminEntry),
('/admin/entry/(.*)', AdminEntry),
('/admin/links/(.*)', AdminLinks),
('/admin/setup', AdminConfig),
('/admin/migrate/(.*)', AdminMigrate),
('/tag/(.*)', TagPage),
('/atom', FeedHandler),
(r'/page/(.*)', PublicPage),
(r'/archive/(.*)', ArchivePage),
(r'/entry/(.*)', MainPage),
],debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()