<s> from __future__ import absolute_import <EOL> from django . views . decorators . csrf import csrf_exempt , csrf_protect <EOL> from zerver . decorator import authenticated_json_view , authenticated_rest_api_view , process_as_post , JsonableError <EOL> from zerver . lib . response import json_method_not_allowed , json_unauthorized , json_unhandled_exception <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . conf import settings <EOL> import logging <EOL> METHODS = ( '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ csrf_exempt <EOL> def rest_dispatch ( request , globals_list , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> supported_methods = { } <EOL> for arg in list ( kwargs ) : <EOL> if arg in METHODS : <EOL> supported_methods [ arg ] = kwargs [ arg ] <EOL> del kwargs [ arg ] <EOL> if request . method == '<STR_LIT>' : <EOL> response = HttpResponse ( status = <NUM_LIT> ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( supported_methods . keys ( ) ) <EOL> response [ '<STR_LIT>' ] = "<STR_LIT:0>" <EOL> return response <EOL> method_to_use = request . method <EOL> if request . POST and '<STR_LIT>' in request . POST : <EOL> method_to_use = request . POST [ '<STR_LIT>' ] <EOL> if method_to_use == "<STR_LIT>" and "<STR_LIT>" in request . META : <EOL> method_to_use = request . META [ "<STR_LIT>" ] <EOL> if method_to_use in supported_methods : <EOL> target_function = globals_list [ supported_methods [ method_to_use ] ] <EOL> request . _query = target_function . __name__ <EOL> if not request . path . startswith ( "<STR_LIT>" ) and request . user . is_authenticated ( ) : <EOL> target_function = csrf_protect ( authenticated_json_view ( target_function ) ) <EOL> elif request . META . get ( '<STR_LIT>' , None ) : <EOL> target_function = authenticated_rest_api_view ( target_function ) <EOL> else : <EOL> if '<STR_LIT>' in request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return HttpResponseRedirect ( '<STR_LIT>' % ( settings . HOME_NOT_LOGGED_IN , request . path ) ) <EOL> else : <EOL> return json_unauthorized ( "<STR_LIT>" ) <EOL> if request . method not in [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] : <EOL> target_function = process_as_post ( target_function ) <EOL> return target_function ( request , ** kwargs ) <EOL> return json_method_not_allowed ( list ( supported_methods . keys ( ) ) ) </s>
