<s> from __future__ import unicode_literals <EOL> import logging <EOL> from functools import update_wrapper <EOL> from django import http <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import reverse , NoReverseMatch <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . decorators import classonlymethod <EOL> from django . utils import six <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class ContextMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_context_data ( self , ** kwargs ) : <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = self <EOL> return kwargs <EOL> class View ( object ) : <EOL> """<STR_LIT>""" <EOL> http_method_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for key , value in six . iteritems ( kwargs ) : <EOL> setattr ( self , key , value ) <EOL> @ classonlymethod <EOL> def as_view ( cls , ** initkwargs ) : <EOL> """<STR_LIT>""" <EOL> for key in initkwargs : <EOL> if key in cls . http_method_names : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( key , cls . __name__ ) ) <EOL> if not hasattr ( cls , key ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , key ) ) <EOL> def view ( request , * args , ** kwargs ) : <EOL> self = cls ( ** initkwargs ) <EOL> if hasattr ( self , '<STR_LIT>' ) and not hasattr ( self , '<STR_LIT>' ) : <EOL> self . head = self . get <EOL> self . request = request <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> return self . dispatch ( request , * args , ** kwargs ) <EOL> update_wrapper ( view , cls , updated = ( ) ) <EOL> update_wrapper ( view , cls . dispatch , assigned = ( ) ) <EOL> return view <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> if request . method . lower ( ) in self . http_method_names : <EOL> handler = getattr ( self , request . method . lower ( ) , self . http_method_not_allowed ) <EOL> else : <EOL> handler = self . http_method_not_allowed <EOL> return handler ( request , * args , ** kwargs ) <EOL> def http_method_not_allowed ( self , request , * args , ** kwargs ) : <EOL> logger . warning ( '<STR_LIT>' , request . method , request . path , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : self . request <EOL> } <EOL> ) <EOL> return http . HttpResponseNotAllowed ( self . _allowed_methods ( ) ) <EOL> def options ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> response = http . HttpResponse ( ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( self . _allowed_methods ( ) ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT:0>' <EOL> return response <EOL> def _allowed_methods ( self ) : <EOL> return [ m . upper ( ) for m in self . http_method_names if hasattr ( self , m ) ] <EOL> class TemplateResponseMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> template_name = None <EOL> response_class = TemplateResponse <EOL> content_type = None <EOL> def render_to_response ( self , context , ** response_kwargs ) : <EOL> """<STR_LIT>""" <EOL> response_kwargs . setdefault ( '<STR_LIT>' , self . content_type ) <EOL> return self . response_class ( <EOL> request = self . request , <EOL> template = self . get_template_names ( ) , <EOL> context = context , <EOL> ** response_kwargs <EOL> ) <EOL> def get_template_names ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . template_name is None : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> return [ self . template_name ] <EOL> class TemplateView ( TemplateResponseMixin , ContextMixin , View ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> context = self . get_context_data ( ** kwargs ) <EOL> return self . render_to_response ( context ) <EOL> class RedirectView ( View ) : <EOL> """<STR_LIT>""" <EOL> permanent = True <EOL> url = None <EOL> pattern_name = None <EOL> query_string = False <EOL> def get_redirect_url ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . url : <EOL> url = self . url % kwargs <EOL> elif self . pattern_name : <EOL> try : <EOL> url = reverse ( self . pattern_name , args = args , kwargs = kwargs ) <EOL> except NoReverseMatch : <EOL> return None <EOL> else : <EOL> return None <EOL> args = self . request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if args and self . query_string : <EOL> url = "<STR_LIT>" % ( url , args ) <EOL> return url <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> url = self . get_redirect_url ( * args , ** kwargs ) <EOL> if url : <EOL> if self . permanent : <EOL> return http . HttpResponsePermanentRedirect ( url ) <EOL> else : <EOL> return http . HttpResponseRedirect ( url ) <EOL> else : <EOL> logger . warning ( '<STR_LIT>' , self . request . path , <EOL> extra = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : self . request <EOL> } ) <EOL> return http . HttpResponseGone ( ) <EOL> def head ( self , request , * args , ** kwargs ) : <EOL> return self . get ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> return self . get ( request , * args , ** kwargs ) <EOL> def options ( self , request , * args , ** kwargs ) : <EOL> return self . get ( request , * args , ** kwargs ) <EOL> def delete ( self , request , * args , ** kwargs ) : <EOL> return self . get ( request , * args , ** kwargs ) <EOL> def put ( self , request , * args , ** kwargs ) : <EOL> return self . get ( request , * args , ** kwargs ) <EOL> def patch ( self , request , * args , ** kwargs ) : <EOL> return self . get ( request , * args , ** kwargs ) </s>
