-
Notifications
You must be signed in to change notification settings - Fork 12
/
blog.py
241 lines (199 loc) · 7.97 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
import cgi, os
from functools import wraps
import wsgiref.handlers
from google.appengine.ext.webapp import template
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 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():
#self.response.clear()
#self.response.set_status(403).
self.error(403)
template_vals = {'entries': None,'message':'This User does not have priveleges to the Admin Section'}
self.render('views/error.html',template_vals)
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]
self.template_vals = {}
def __before__(self,*args):
pass
def __after__(self,*args):
pass
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'
template_vals.update({'url':url,
'url_linktext':url_linktext,
'blog':self.blog
})
template_vals.update(self.template_vals)
path = os.path.join(os.path.dirname(__file__), template_file)
self.response.out.write(template.render(path, template_vals))
class BasePublicPage(BaseController):
"""
Do all the common public page prep such as nav pages etc
"""
def __before__(self,slug=None):
pages = Entry.all().filter("entrytype =", "page").filter("published =", True).fetch(20)
recententries = Entry.all().filter('entrytype =','post').order('-date').fetch(10)
links = Link.all().filter('linktype =','blogroll')
self.template_vals.update({'recententries':recententries,
'pages':pages,'links':links})
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).filter("published =", True)
self.render('views/index.html',{'entries':entries})
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 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)
self.response.headers['Content-Type'] = 'application/atom+xml'
self.render('views/atom.xml',{'entries':entries})
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',{'blogedit':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.area1 = self.request.get('area1')
blog.area2 = self.request.get('area2')
blog.area3 = self.request.get('area3')
blog.analyticsjs = self.request.get('analyticsjs')
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(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()
self.redirect('/admin/entry/list/%s' % entry.entrytype )
class AdminList(BaseController):
@requires_admin
def get(self,entrytype='post',template_vals={}):
entries = Entry.all().filter('entrytype =',entrytype)
self.render('views/admin.html',{'entries':entries})
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('title')
link.put()
self.response.out.write('link %s added' % link.linktype)
def main():
application = webapp2.WSGIApplication2(
[('/', MainPage),
('/admin/entry/list/(.*)', AdminList),
('/admin/entry/(.*)/(.*)', AdminEntry),
('/admin/entry/(.*)', AdminEntry),
('/admin/links/(.*)', AdminLinks),
('/admin/setup', AdminConfig),
('/tag/(.*)', TagPage),
('/atom', FeedHandler),
(r'/page/(.*)', PublicPage),
(r'/entry/(.*)', MainPage),
],debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()