本文整理匯總了Python中flask.views.View方法的典型用法代碼示例。如果您正苦於以下問題:Python views.View方法的具體用法?Python views.View怎麽用?Python views.View使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類flask.views
的用法示例。
在下文中一共展示了views.View方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: route_as_view
# 需要導入模塊: from flask import views [as 別名]
# 或者: from flask.views import View [as 別名]
def route_as_view(cls, app, name, rules, *class_args, **class_kwargs):
""" Register the view with an URL route
:param app: Flask application
:type app: flask.Flask|flask.Blueprint
:param name: Unique view name
:type name: str
:param rules: List of route rules to use
:type rules: Iterable[str|werkzeug.routing.Rule]
:param class_args: Args to pass to object constructor
:param class_kwargs: KwArgs to pass to object constructor
:return: View callable
:rtype: Callable
"""
view = super(MethodView, cls).as_view(name, *class_args, **class_kwargs)
for rule in rules:
app.add_url_rule(rule, view_func=view)
return view
示例2: test_class_based_views
# 需要導入模塊: from flask import views [as 別名]
# 或者: from flask.views import View [as 別名]
def test_class_based_views(sentry_init, app, capture_events):
sentry_init(integrations=[flask_sentry.FlaskIntegration()])
events = capture_events()
@app.route("/")
class HelloClass(View):
def dispatch_request(self):
capture_message("hi")
return "ok"
app.add_url_rule("/hello-class/", view_func=HelloClass.as_view("hello_class"))
with app.test_client() as client:
response = client.get("/hello-class/")
assert response.status_code == 200
(event,) = events
assert event["message"] == "hi"
assert event["transaction"] == "hello_class"
示例3: test_requires_works_as_cbv_decorator
# 需要導入模塊: from flask import views [as 別名]
# 或者: from flask.views import View [as 別名]
def test_requires_works_as_cbv_decorator(app, ismember, guest):
class IsMemberView(View):
decorators = [requires(ismember)]
Allows(app=app, identity_loader=lambda: guest)
with pytest.raises(Forbidden):
with app.app_context():
IsMemberView.as_view("memberonly")()
示例4: handle_500
# 需要導入模塊: from flask import views [as 別名]
# 或者: from flask.views import View [as 別名]
def handle_500(e):
return '500', 500
# Registering a flask.views.View class as an endpoint
示例5: update_g
# 需要導入模塊: from flask import views [as 別名]
# 或者: from flask.views import View [as 別名]
def update_g(self):
# g lifetime: every single request
# Note that use inject_variable() in View class would cause memory leak, issue #14
g.IS_MOBILE = self.IS_MOBILE
g.url_jobs_list = [url_for('jobs', node=node, ui=self.UI)
for node in range(1, self.SCRAPYD_SERVERS_AMOUNT + 1)]
g.multinode = ('<label title="multinode">'
'<svg class="icon" aria-hidden="true"><use xlink:href="#icon-servers"></use></svg>'
'</label>')
# For base.html
if not self.USE_MOBILEUI:
g.url_daemonstatus = url_for('api', node=self.node, opt='daemonstatus')
g.url_menu_servers = url_for('servers', node=self.node)
g.url_menu_jobs = url_for('jobs', node=self.node)
g.url_menu_nodereports = url_for('nodereports', node=self.node)
g.url_menu_clusterreports = url_for('clusterreports', node=self.node)
g.url_menu_tasks = url_for('tasks', node=self.node)
g.url_menu_deploy = url_for('deploy', node=self.node)
g.url_menu_schedule = url_for('schedule', node=self.node)
g.url_menu_projects = url_for('projects', node=self.node)
g.url_menu_logs = url_for('logs', node=self.node)
g.url_menu_items = url_for('items', node=self.node)
g.url_menu_sendtext = url_for('sendtext', node=self.node)
g.url_menu_parse = url_for('parse.upload', node=self.node)
g.url_menu_settings = url_for('settings', node=self.node)
g.url_menu_mobileui = url_for('index', node=self.node, ui='mobile')
g.scheduler_state_paused = self.scheduler.state == STATE_PAUSED and self.any_running_apscheduler_jobs
g.scheduler_state_running = self.scheduler.state == STATE_RUNNING and self.any_running_apscheduler_jobs
# Issue#48 [PY2] UnicodeDecodeError raised when there are some files with illegal filenames in `SCRAPY_PROJECTS_DIR`
# https://stackoverflow.com/questions/21772271/unicodedecodeerror-when-performing-os-walk
# https://xuanwo.io/2018/04/01/python-os-walk/
# Tested in Ubuntu:
# touch $(echo -e "\x8b\x8bFile")
# mkdir $(echo -e "\x8b\x8bFolder")
示例6: test_pluggable_views
# 需要導入模塊: from flask import views [as 別名]
# 或者: from flask.views import View [as 別名]
def test_pluggable_views(extension_factory):
app, limiter = extension_factory(default_limits=["1/hour"])
class Va(View):
methods = ['GET', 'POST']
decorators = [limiter.limit("2/second")]
def dispatch_request(self):
return request.method.lower()
class Vb(View):
methods = ['GET']
decorators = [limiter.limit("1/second, 3/minute")]
def dispatch_request(self):
return request.method.lower()
class Vc(View):
methods = ['GET']
def dispatch_request(self):
return request.method.lower()
app.add_url_rule("/a", view_func=Va.as_view("a"))
app.add_url_rule("/b", view_func=Vb.as_view("b"))
app.add_url_rule("/c", view_func=Vc.as_view("c"))
with hiro.Timeline().freeze() as timeline:
with app.test_client() as cli:
assert 200 == cli.get("/a").status_code
assert 200 == cli.get("/a").status_code
assert 429 == cli.post("/a").status_code
assert 200 == cli.get("/b").status_code
timeline.forward(1)
assert 200 == cli.get("/b").status_code
timeline.forward(1)
assert 200 == cli.get("/b").status_code
timeline.forward(1)
assert 429 == cli.get("/b").status_code
assert 200 == cli.get("/c").status_code
assert 429 == cli.get("/c").status_code