def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
python类Markup()的实例源码
def main():
now = datetime.datetime.now()
timeString = now.strftime("%Y-%m-%d %I:%M %p")
passer = ''
for i in range(len(roomName)):
passer = passer + "<p class='roomtitle'>%s</p>" % (roomName[i])
for j in range(len(accName[i])):
buttonHtmlName = accName[i][j].replace(" ", "<br>")
passer = passer + "<span id='button%d%d'><button class='%s' onclick='toggle(%d,%d)'>%s</button></span>" % (i, j, accState(i,j), i, j, buttonHtmlName)
buttonGrid = Markup(passer)
templateData = {
'title' : 'WebGPIO',
'time': timeString,
'buttons' : buttonGrid
}
return render_template('main.html', **templateData)
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def test():
mysql_result = False
# TODO REMOVE FOLLOWING LINE AFTER TESTING COMPLETE.
db.session.query("1").from_statement("SELECT 1").all()
try:
if db.session.query("1").from_statement("SELECT 1").all():
mysql_result = True
except:
pass
if mysql_result:
result = Markup('<span style="color: green;">PASS</span>')
else:
result = Markup('<span style="color: red;">FAIL</span>')
# Return the page with the result.
return render_template('index.html', result=result)
def search(page=1):
term = request.args.get('term')
if term:
new_term = TermRepository()
new_term.create_or_update(term)
episode = EpisodeRepository()
episodes = episode.result_search_paginate(term, page, 20)
if episodes.total:
flash('{} resultados para {}'.format(episodes.total, term))
else:
message = Markup('<span>Nenhum resultado encontrado.</span> <a class="link-add-suggestion" href="/add_topic_suggestion">Gostaria de sugerir o tema?</a>')
flash(message)
return render_template('search.html', episodes=episodes, page="search")
else:
return render_template('search.html', page="search")
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def validate(self):
if not Form.validate(self):
#print "not form validate"
if "<" in self.respondent.data or \
"<" in self.petitioner.data:
info = Markup("<img src='/static/hackers.jpg'>")
flash(info)
return False
elif self.respondent or \
self.petitioner.data or \
self.respondent.data:
return True
#print "Should return False"
info = Markup('<h2 style="color:red"> Must complete all fields </h2>')
flash(info)
return False
return True
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def render_map():
css_js = ''
if conf.LOAD_CUSTOM_CSS_FILE:
css_js = '<link rel="stylesheet" href="static/css/custom.css">'
if conf.LOAD_CUSTOM_JS_FILE:
css_js += '<script type="text/javascript" src="static/js/custom.js"></script>'
js_vars = Markup(
"_defaultSettings['FIXED_OPACITY'] = '{:d}'; "
"_defaultSettings['SHOW_TIMER'] = '{:d}'; "
"_defaultSettings['TRASH_IDS'] = [{}]; ".format(conf.FIXED_OPACITY, conf.SHOW_TIMER, ', '.join(str(p_id) for p_id in conf.TRASH_IDS)))
template = app.jinja_env.get_template('custom.html' if conf.LOAD_CUSTOM_HTML_FILE else 'newmap.html')
return template.render(
area_name=conf.AREA_NAME,
map_center=center,
map_provider_url=conf.MAP_PROVIDER_URL,
map_provider_attribution=conf.MAP_PROVIDER_ATTRIBUTION,
social_links=social_links(),
init_js_vars=js_vars,
extra_css_js=Markup(css_js)
)
def __init__(self, initial=''):
"""Create a Markdown widget.
Parameters
----------
initial : str, optional
Default markdown for the widget.
"""
super(Markdown, self).__init__()
self._comp = self._tag.format(
initial=Markup(markdown(initial).replace('\n', '\\n'))
)
# pylint: disable=no-self-use
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def ctx_processor():
ctx_object = CTX()
def ctx_protect(secret, origin=None, alphabet=None):
protected_secret = ctx_object.protect(secret, origin, alphabet)
return Markup(
"<div data-ctx-origin='{origin_id}'>{permuted}</div>".format(
origin_id=protected_secret['origin_id'],
permuted=quote(protected_secret['permuted'])
)
)
def ctx_permutations():
return Markup(
"<script type='application/json' id='ctx-permutations'>{permutations}</script>".format(
permutations=dumps(
ctx_object.get_permutations()
)
)
)
return {
'ctx_protect': ctx_protect,
'ctx_permutations': ctx_permutations
}
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def build_zone_base_template(zone):
"""
Load template files from the zone file directory and create the base NS and static records
"""
template_data = []
zone_dir = current_app.config['zone_dir']
base_template = read_if_exists(os.path.join(zone_dir, 'base_zone.j2'))
if base_template:
template_data.append(base_template)
zone_template = read_if_exists(os.path.join(zone_dir, '{}.zone.j2'.format(zone)))
if zone_template:
template_data.append(zone_template)
template = '\n'.join(template_data)
# Use Markup to mark the string as safe, we're not generating HTML
return render_template_string(Markup(template), origin=zone)
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def search_for(self, query, include_draft=False):
"""
Search for a query text.
:param query: keyword to query
:param include_draft: return draft posts/pages or not
:return: an iterable object of posts and pages (if allowed).
"""
query = query.lower()
if not query:
return []
def contains_query_keyword(post_or_page):
contains = query in post_or_page.title.lower() \
or query in Markup(
get_parser(post_or_page.format).parse_whole(post_or_page.raw_content)
).striptags().lower()
return contains
return filter(contains_query_keyword, chain(self.get_posts(include_draft=include_draft),
self.get_pages(include_draft=include_draft)
if current_app.config['ALLOW_SEARCH_PAGES'] else []))
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
templating.py 文件源码
项目:My-Web-Server-Framework-With-Python2.7
作者: syjsu
项目源码
文件源码
阅读 38
收藏 0
点赞 0
评论 0
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def display():
with app.open_resource('static/corpus000.txt') as f:
content = f.read()
cap = caption.Caption(content)
cap.make()
cartoon = cg.Cartoon(cap)
svg_cartoon = cartoon.assemble()
this_cartoon = Cartoon(svg_cartoon)
db.session.add(this_cartoon)
db.session.commit()
drawing_markup = Markup(svg_cartoon)
return render_template('display.html',
drawn=True,
svgwrite=drawing_markup)
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def pubmed(id_):
from so2html import standoff_to_html
from operator import itemgetter
from itertools import groupby
try:
text = '\n'.join(pubmed_text_store.get_tiab(id_))
except KeyError:
abort(404)
try:
ann = pubmed_ann_store.get_spans(id_, minimize=True)
except KeyError:
# Assume no annotations.
ann = []
html = standoff_to_html(text, ann, legend=True, tooltips=True,
links=False, content_only=True)
template_args = { 'documents': { id_: { 'html': Markup(html), } } }
return render_template_with_globals('pubmed.html', template_args)
def links(self):
"""Get all the pagination links."""
if self.total_pages <= 1:
if self.show_single_page:
return self._get_single_page_link()
return ''
s = [self.link_css_fmt.format(self.link_size, self.alignment)]
s.append(self.prev_page)
for page in self.pages:
s.append(self.single_page(page) if page else self.gap_marker_fmt)
s.append(self.next_page)
s.append(self.css_end_fmt)
if self.css_framework == 'foundation' and self.alignment:
s.insert(0, F_ALIGNMENT.format(self.alignment))
s.append('</div>')
return Markup(''.join(s))
def test_escaping(self):
text = '<p>Hello World!'
app = flask.Flask(__name__)
@app.route('/')
def index():
return flask.render_template('escaping_template.html', text=text,
html=flask.Markup(text))
lines = app.test_client().get('/').data.splitlines()
self.assert_equal(lines, [
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!',
b'<p>Hello World!'
])
def match_rcon(matchid):
match = Match.query.get_or_404(matchid)
admintools_check(g.user, match)
command = request.values.get('command')
server = GameServer.query.get_or_404(match.server_id)
if command:
try:
rcon_response = server.send_rcon_command(
command, raise_errors=True)
if rcon_response:
rcon_response = Markup(rcon_response.replace('\n', '<br>'))
else:
rcon_response = 'No output'
flash(rcon_response)
except util.RconError as e:
print(e)
flash('Failed to send command: ' + str(e))
return redirect('/match/{}'.format(matchid))