-
Notifications
You must be signed in to change notification settings - Fork 1
/
post_handlers.py
119 lines (99 loc) · 3.96 KB
/
post_handlers.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
# Copyright 2012 Hai Thanh Nguyen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from webapp2_extras.i18n import _
from requesthandler import RequestHandler
import utils
def can_user_edit_post(handler, model):
return (
( handler.current_user_check_permission("edit_own_post")
and (handler.get_current_user().key == model.author_key())
)
or handler.current_user_check_permission("edit_all_post")
)
class MasterPostEditHandler(RequestHandler):
settings = utils.ObjectSettings()
def _check_permission(self):
return can_user_edit_post(self, self.the_post())
@property
def post_cls(self):
return self.settings.post_cls
@utils.cache_to_property("_the_post")
def the_post(self, slug):
if slug:
return self.post_cls.query(self.post_cls.slug == slug).get()
else:
post = self.post_cls(title="", content="")
if self.logged_in():
post.author = self.get_current_user().key
return post
def _handler_init(self, slug=""):
if self.the_post(slug) is None:
self.render("noticepage", {
"message": _(u"This post does not exist."),
})
self.stop()
def template_values(self):
return {
"model": self.the_post(),
}
def _get(self, *args, **kwds):
return self.render(self.settings.template_name, self.template_values())
def _post(self, slug=""):
post = self.the_post()
post.assign(self)
if post.validate():
if not post.slug:
post.make_slug()
post.put()
if not slug:
self.redirect(self.uri_for(self.settings.uri_id, slug=post.slug))
else:
return self.render(self.settings.template_name, self.template_values())
class PostViewHandler(RequestHandler):
def _handler_init(self, *args, **kwds):
self.settings()
def _settings(self, template, model_cls, slave_model_cls, edit_uri_id):
self._template_name = template
self._model_cls = model_cls
self._slave_model_cls = slave_model_cls
self._edit_uri_id = edit_uri_id
def settings(self):
raise Exception("Must be overridden.")
def _check_permission(self):
return self.current_user_check_permission("view_post")
def _additional_values(self):
return {}
def _render_values(self, post, slug, slave_model=None, additional_values={}):
"""Don't-Repeat-Myself helper"""
d = {
"model": post,
"can_edit": lambda: can_user_edit_post(self, post),
"edit_url": self.uri_for(self._edit_uri_id, slug=slug),
"slave_pagin": self._slave_pagination(post),
"slave_model": slave_model or self._slave_model_cls(),
}
d.update(additional_values or self._additional_values())
return d
def _get(self, slug=""):
post = self._model_cls.query(self._model_cls.slug == slug).get()
if post == None:
return self.render("noticepage", {
"message": _(u"This post does not exist."),
})
else:
self.render(self._template_name, self._render_values(post, slug))
def _get_post_from_form(self):
return self._model_cls.get_by_id(int(self.request.get("__master")))
def _post(self, slug=""):
raise Exception("Must be overridden.")