# -*- coding: utf-8 -*-"""flask.views~~~~~~~~~~~This module provides class-based views inspired by the ones in Django.:copyright: © 2010 by the Pallets team.:license: BSD, see LICENSE for more details."""from .globals import requestfrom ._compat import with_metaclasshttp_method_funcs = frozenset(['get', 'post', 'head', 'options','delete', 'put', 'trace', 'patch'])class View(object):"""Alternative way to use view functions. A subclass has to implement:meth:`dispatch_request` which is called with the view arguments fromthe URL routing system. If :attr:`methods` is provided the methodsdo not have to be passed to the :meth:`~flask.Flask.add_url_rule`method explicitly::class MyView(View):methods = ['GET']def dispatch_request(self, name):return 'Hello %s!' % nameapp.add_url_rule('/hello/<name>', view_func=MyView.as_view('myview'))When you want to decorate a pluggable view you will have to either do thatwhen the view function is created (by wrapping the return value of:meth:`as_view`) or you can use the :attr:`decorators` attribute::class SecretView(View):methods = ['GET']decorators = [superuser_required]def dispatch_request(self):...The decorators stored in the decorators list are applied one after anotherwhen the view function is created. Note that you can *not* use the classbased decorators since those would decorate the view class and not thegenerated view function!"""#: A list of methods this view can handle.methods = None#: Setting this disables or force-enables the automatic options handling.provide_automatic_options = None#: The canonical way to decorate class-based views is to decorate the#: return value of as_view(). However since this moves parts of the#: logic from the class declaration to the place where it's hooked#: into the routing system.#:#: You can place one or more decorators in this list and whenever the#: view function is created the result is automatically decorated.#:#: .. versionadded:: 0.8decorators = ()def dispatch_request(self):"""Subclasses have to override this method to implement theactual view function code. This method is called with allthe arguments from the URL rule."""raise NotImplementedError()@classmethoddef as_view(cls, name, *class_args, **class_kwargs):"""Converts the class into an actual view function that can be usedwith the routing system. Internally this generates a function on thefly which will instantiate the :class:`View` on each request and callthe :meth:`dispatch_request` method on it.The arguments passed to :meth:`as_view` are forwarded to theconstructor of the class."""def view(*args, **kwargs):self = view.view_class(*class_args, **class_kwargs)return self.dispatch_request(*args, **kwargs)if cls.decorators:view.__name__ = nameview.__module__ = cls.__module__for decorator in cls.decorators:view = decorator(view)# We attach the view class to the view function for two reasons:# first of all it allows us to easily figure out what class-based# view this thing came from, secondly it's also used for instantiating# the view class so you can actually replace it with something else# for testing purposes and debugging.view.view_class = clsview.__name__ = nameview.__doc__ = cls.__doc__view.__module__ = cls.__module__view.methods = cls.methodsview.provide_automatic_options = cls.provide_automatic_optionsreturn viewclass MethodViewType(type):"""Metaclass for :class:`MethodView` that determines what methods the viewdefines."""def __init__(cls, name, bases, d):super(MethodViewType, cls).__init__(name, bases, d)if 'methods' not in d:methods = set()for key in http_method_funcs:if hasattr(cls, key):methods.add(key.upper())# If we have no method at all in there we don't want to add a# method list. This is for instance the case for the base class# or another subclass of a base method view that does not introduce# new methods.if methods:cls.methods = methodsclass MethodView(with_metaclass(MethodViewType, View)):"""A class-based view that dispatches request methods to the correspondingclass methods. For example, if you implement a ``get`` method, it will beused to handle ``GET`` requests. ::class CounterAPI(MethodView):def get(self):return session.get('counter', 0)def post(self):session['counter'] = session.get('counter', 0) + 1return 'OK'app.add_url_rule('/counter', view_func=CounterAPI.as_view('counter'))"""def dispatch_request(self, *args, **kwargs):meth = getattr(self, request.method.lower(), None)# If the request method is HEAD and we don't have a handler for it# retry with GET.if meth is None and request.method == 'HEAD':meth = getattr(self, 'get', None)assert meth is not None, 'Unimplemented method %r' % request.methodreturn meth(*args, **kwargs)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。