<s> from __future__ import unicode_literals <EOL> import logging <EOL> from django . http import HttpResponse <EOL> from django . utils . six . moves . urllib . parse import quote as urllib_quote <EOL> from djblets . util . http import set_last_modified <EOL> from djblets . webapi . errors import DOES_NOT_EXIST , WebAPIError <EOL> from reviewboard . diffviewer . models import FileDiff <EOL> from reviewboard . diffviewer . diffutils import ( get_original_file , <EOL> get_patched_file ) <EOL> from reviewboard . webapi . base import WebAPIResource <EOL> from reviewboard . webapi . decorators import ( webapi_check_local_site , <EOL> webapi_check_login_required ) <EOL> from reviewboard . webapi . errors import FILE_RETRIEVAL_ERROR <EOL> class BasePatchedFileResource ( WebAPIResource ) : <EOL> """<STR_LIT>""" <EOL> added_in = '<STR_LIT>' <EOL> uri_name = '<STR_LIT>' <EOL> link_name = '<STR_LIT>' <EOL> singleton = True <EOL> allowed_mimetypes = [ <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] <EOL> def get_filediff ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ webapi_check_login_required <EOL> @ webapi_check_local_site <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> result = self . get_filediff ( request , * args , ** kwargs ) <EOL> if isinstance ( result , FileDiff ) : <EOL> filediff = result <EOL> elif isinstance ( result , WebAPIError ) : <EOL> return result <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if filediff . deleted : <EOL> return DOES_NOT_EXIST <EOL> try : <EOL> orig_file = get_original_file ( <EOL> filediff , request , <EOL> filediff . diffset . repository . get_encoding_list ( ) ) <EOL> except Exception as e : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . __class__ . __name__ , filediff . pk , e , exc_info = <NUM_LIT:1> , <EOL> request = request ) <EOL> return FILE_RETRIEVAL_ERROR <EOL> try : <EOL> patched_file = get_patched_file ( orig_file , filediff , request ) <EOL> except Exception as e : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . __class__ . __name__ , filediff . pk , e , exc_info = <NUM_LIT:1> , <EOL> request = request ) <EOL> return FILE_RETRIEVAL_ERROR <EOL> resp = HttpResponse ( patched_file , content_type = '<STR_LIT>' ) <EOL> filename = urllib_quote ( filediff . dest_file ) <EOL> resp [ '<STR_LIT>' ] = '<STR_LIT>' % filename <EOL> set_last_modified ( resp , filediff . diffset . timestamp ) <EOL> return resp </s>
