def submit_post(obj_response, files, form_values):
form = forms.PostForm(**form_values)
if form.validate():
parent_id = None
if form.parent_id.data:
parent_id = form.parent_id.data
post = Post.submit(form.content.data, current_user, parent_id)
if parent_id:
render_comment = get_template_attribute('macros.html', 'render_comment')
obj_response.html_prepend(''.join(['#post-', parent_id, '-comments']),
render_comment(post, current_user).unescape())
# update parent comments counter
obj_response.script(''.join(['$("#load_comment_button_', parent_id, '").children(".badge").html(',
str(Post.query.filter_by(parent_id=parent_id).count()), ')']))
else:
render_post = get_template_attribute('macros.html', 'render_post')
obj_response.html_prepend('#post-container', render_post(post, current_user).unescape())
obj_response.script("$('#collapsable_post_form').collapse('hide');")
form.reset()
render_post_form = get_template_attribute('macros.html', 'render_post_form')
obj_response.html('#collapsable_post_form', render_post_form(form, current_user).unescape())
# register again the sijax upload plugin
obj_response.script('sjxUpload.registerForm({"callback": "post_form_upload", "formId": "post_form"});')
python类get_template_attribute()的实例源码
def load_more_laws(obj_response, group_name, status_name, order, last):
laws = Law.get_more(group_name=group_name, status_name=status_name, order=order, last=last)
render_law = get_template_attribute('macros.html', 'render_law')
more_laws_panel = get_template_attribute('macros.html', 'more_laws_panel')
if laws:
for law in laws:
obj_response.html_append('#laws-container', render_law(law, current_user, actions_footer=True).unescape())
if order == 'id':
panel = more_laws_panel(group_name, status_name, order, laws[-1].id).unescape()
elif order == 'date':
panel = more_laws_panel(group_name, status_name, order, laws[-1].date).unescape()
else:
return
obj_response.html('#load_more_container', panel)
# refresh masonry to load the new laws correctly
obj_response.script('$(".masonry-grid").masonry( "reloadItems" )')
obj_response.script('$(".masonry-grid").masonry()')
# refresh and re-enable waypoint to achieve continuous loading
obj_response.script('Waypoint.refreshAll()')
obj_response.script('Waypoint.enableAll()')
else:
obj_response.html('#load_more_container', more_laws_panel().unescape())
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def load_more_posts(obj_response, group, name, older_than):
posts = Post.get_more(group=group, name=name, older_than=older_than)
render_post = get_template_attribute('macros.html', 'render_post')
more_posts_panel = get_template_attribute('macros.html', 'more_posts_panel')
if posts:
for post in posts:
obj_response.html_append('#post-container', render_post(post, current_user).unescape())
obj_response.html('#load_more_container', more_posts_panel(group, name, posts[-1].date).unescape())
# refresh and re-enable waypoint to achieve continuous loading
obj_response.script('Waypoint.refreshAll()')
obj_response.script('Waypoint.enableAll()')
else:
obj_response.html('#load_more_container', more_posts_panel(group, name, None).unescape())
def load_comments(obj_response, post_id, depth):
comments = Post.query.filter_by(parent_id=post_id).order_by(Post.date)
if comments:
render_comment = get_template_attribute('macros.html', 'render_comment')
# clear the comments displayed (to avoid double loading of inserted comments)
obj_response.html(''.join(['#post-', str(post_id), '-comments']), '')
for comment in comments:
obj_response.html_append(''.join(['#post-', str(post_id), '-comments']),
render_comment(comment, current_user, depth).unescape())
# change the button to hide the comments if pressed again
obj_response.script(''.join(['$("#load_comment_button_', str(post_id), '").attr("onclick", "hide_comments(',
str(post_id), ',', str(depth), ')")']))
def load_more_proposals(obj_response, open, pending, older_than):
proposals = Proposal.get_more(open=open, pending=pending, older_than=older_than)
render_proposal = get_template_attribute('macros.html', 'render_proposal')
more_proposals_panel = get_template_attribute('macros.html', 'more_proposals_panel')
if proposals:
for proposal in proposals:
obj_response.html_append('#proposals-container', render_proposal(proposal, current_user).unescape())
obj_response.html('#load_more_container',
more_proposals_panel(proposals[-1].date, open=open, pending=pending).unescape())
# refresh and re-enable waypoint to achieve continuous loading
obj_response.script('Waypoint.refreshAll()')
obj_response.script('Waypoint.enableAll()')
else:
obj_response.html('#load_more_container', more_proposals_panel(None).unescape())
def update_notifications(obj_response, newer_than):
if current_user.has_new_notifications(newer_than):
# make the notifications bell green
obj_response.script('$("#notifications_bell").attr("class", "glyphicon glyphicon-bell notif-unseen")')
# play an unpleasant sound
obj_response.script('document.getElementById("bleep_sound").play()')
# update the notifications dropdown
render_dropdown = get_template_attribute('macros.html', 'render_notifications_dropdown')
obj_response.html('#notifications_dropdown', render_dropdown(current_user).unescape())
def set_all_notifications_seen(obj_response):
# set all as seen in the database:
current_user.set_all_notifications_seen()
# make the notifications bell white
obj_response.script('$("#notifications_bell").attr("class", "glyphicon glyphicon-bell")')
# update the notifications dropdown
render_dropdown = get_template_attribute('macros.html', 'render_notifications_dropdown')
obj_response.html('#notifications_dropdown', render_dropdown(current_user).unescape())
def toggle_subscription(obj_response, item_type, item_id):
render_subscription = get_template_attribute('macros.html', 'render_subscription')
if item_type == 'proposal':
query = Proposal.query
elif item_type == 'law':
query = Law.query
elif item_type == 'post':
query = Post.query
else:
return
item = query.filter_by(id=item_id).first()
if item:
item.toggle_subscription(current_user)
obj_response.html('#' + item_type + '-' + str(item_id) + '-subscription',
render_subscription(current_user, item, item_type).unescape())
def autolink(self, link, is_email):
if is_email:
return '<a href="mailto:{0}">{0}</a>'.format(link)
# analyze url
parsed = urlparse(link)
if 'youtube.com' in parsed.netloc:
vid_id = dict(item.split('=') for item in parsed.query.split('&'))['v']
return youtube_embed(vid_id)
if 'youtu.be' in parsed.netloc:
vid_id = parsed.path
return youtube_embed(vid_id)
if parsed.path.lower().endswith(('.jpg', '.jpeg', '.jpe', '.jif', '.jfif', '.jfi', '.gif', '.png')):
return '<a href="{0}"><img src="{0}" class="img-responsive img-thumbnail" /></a>'.format(link)
# standard case autolink
res = requests.get(link)
soup = BeautifulSoup(res.text, 'html.parser')
title = soup.title.string
if len(title) > 50:
title = title[:47] + '...'
icon = soup.find("link", rel="shortcut icon")
if icon:
icon = icon['href']
# might be not absolute
if not icon.startswith('http'):
# url might have changed
parsed = urlparse(res.url)
if not icon.startswith('/'):
icon = '/' + icon
icon = parsed.scheme + '://' + parsed.netloc + icon
render_autolink = get_template_attribute('macros.html', 'render_autolink')
return render_autolink(link, title, icon).unescape()
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
templating.py 文件源码
项目:My-Web-Server-Framework-With-Python2.7
作者: syjsu
项目源码
文件源码
阅读 35
收藏 0
点赞 0
评论 0
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')
def test_macros(self):
app = flask.Flask(__name__)
with app.test_request_context():
macro = flask.get_template_attribute('_macro.html', 'hello')
self.assert_equal(macro('World'), 'Hello World!')