
¾«‘_ª  ã            	   @   s™   d  Z  d d l m Z d d l m Z e d d d d d d	 d
 d g ƒ Z Gd d „  d e ƒ Z Gd d „  d e	 ƒ Z
 Gd d „  d e e
 e ƒ ƒ Z d S)z¦
    flask.views
    ~~~~~~~~~~~

    This module provides class-based views inspired by the ones in Django.

    :copyright: 2010 Pallets
    :license: BSD-3-Clause
é   )Úwith_metaclass)ÚrequestÚgetÚpostÚheadÚoptionsÚdeleteÚputÚtraceÚpatchc               @   sF   e  Z d  Z d Z d Z d Z f  Z d d „  Z e d d „  ƒ Z	 d S)ÚViewa©  Alternative way to use view functions.  A subclass has to implement
    :meth:`dispatch_request` which is called with the view arguments from
    the URL routing system.  If :attr:`methods` is provided the methods
    do 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!' % name

        app.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 that
    when 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 another
    when the view function is created.  Note that you can *not* use the class
    based decorators since those would decorate the view class and not the
    generated view function!
    Nc             C   s   t  ƒ  ‚ d S)z­Subclasses have to override this method to implement the
        actual view function code.  This method is called with all
        the arguments from the URL rule.
        N)ÚNotImplementedError)Úself© r   ú,/tmp/pip-build-5gj8f0j9/flask/flask/views.pyÚdispatch_requestE   s    zView.dispatch_requestc                sœ   ‡  ‡ ‡ f d d †  ‰ |  j  rV | ˆ _ |  j ˆ _ x |  j  D] } | ˆ ƒ ‰ q@ W|  ˆ _ | ˆ _ |  j ˆ _ |  j ˆ _ |  j ˆ _ |  j ˆ _ ˆ S)a€  Converts the class into an actual view function that can be used
        with the routing system.  Internally this generates a function on the
        fly which will instantiate the :class:`View` on each request and call
        the :meth:`dispatch_request` method on it.

        The arguments passed to :meth:`as_view` are forwarded to the
        constructor of the class.
        c                 s"   ˆ j  ˆ  ˆ Ž  } | j |  | Ž  S)N)Ú
view_classr   )ÚargsÚkwargsr   )Ú
class_argsÚclass_kwargsÚviewr   r   r   W   s    zView.as_view.<locals>.view)Ú
decoratorsÚ__name__Ú
__module__r   Ú__doc__ÚmethodsÚprovide_automatic_options)ÚclsÚnamer   r   Z	decoratorr   )r   r   r   r   Úas_viewL   s    				zView.as_view)
r   r   Ú__qualname__r   r   r   r   r   Úclassmethodr    r   r   r   r   r      s   r   c                   s(   e  Z d  Z d Z ‡  f d d †  Z ‡  S)ÚMethodViewTypezYMetaclass for :class:`MethodView` that determines what methods the view
    defines.
    c                sª   t  t |  ƒ j | | | ƒ d | k r¦ t ƒ  } x0 | D]( } t | d d  ƒ r8 | j | j ƒ q8 Wx0 t D]( } t |  | ƒ rk | j	 | j
 ƒ  ƒ qk W| r¦ | |  _ d  S)Nr   )Úsuperr#   Ú__init__ÚsetÚgetattrÚupdater   Úhttp_method_funcsÚhasattrÚaddÚupper)r   r   ÚbasesÚdr   ÚbaseÚkey)Ú	__class__r   r   r%   t   s    	zMethodViewType.__init__)r   r   r!   r   r%   r   r   )r1   r   r#   o   s   r#   c               @   s"   e  Z d  Z d Z d d „  Z d S)Ú
MethodViewa   A class-based view that dispatches request methods to the corresponding
    class methods. For example, if you implement a ``get`` method, it will be
    used to handle ``GET`` requests. ::

        class CounterAPI(MethodView):
            def get(self):
                return session.get('counter', 0)

            def post(self):
                session['counter'] = session.get('counter', 0) + 1
                return 'OK'

        app.add_url_rule('/counter', view_func=CounterAPI.as_view('counter'))
    c             O   st   t  |  t j j ƒ  d  ƒ } | d  k rH t j d k rH t  |  d d  ƒ } | d  k	 sg t d t j ƒ ‚ | | | Ž  S)NÚHEADr   zUnimplemented method %r)r'   r   ÚmethodÚlowerÚAssertionError)r   r   r   Úmethr   r   r   r   š   s
    zMethodView.dispatch_requestN)r   r   r!   r   r   r   r   r   r   r2   Š   s   r2   N)r   Ú_compatr   Úglobalsr   Ú	frozensetr)   Úobjectr   Útyper#   r2   r   r   r   r   Ú<module>
   s   ![