<s> from __future__ import unicode_literals <EOL> import logging <EOL> from django . db . models import Q <EOL> from django . utils import six <EOL> from djblets . webapi . decorators import ( webapi_login_required , <EOL> webapi_response_errors , <EOL> webapi_request_fields ) <EOL> from djblets . webapi . errors import ( DOES_NOT_EXIST , INVALID_FORM_DATA , <EOL> NOT_LOGGED_IN , PERMISSION_DENIED ) <EOL> from reviewboard . diffviewer . errors import ( DiffParserError , <EOL> DiffTooBigError , <EOL> EmptyDiffError ) <EOL> from reviewboard . diffviewer . models import DiffSet <EOL> from reviewboard . scmtools . models import Repository <EOL> from reviewboard . scmtools . errors import ( FileNotFoundError , <EOL> SCMError ) <EOL> from reviewboard . scmtools . git import ShortSHA1Error <EOL> from reviewboard . webapi . decorators import ( webapi_check_login_required , <EOL> webapi_check_local_site ) <EOL> from reviewboard . webapi . errors import ( DIFF_EMPTY , <EOL> DIFF_PARSE_ERROR , <EOL> DIFF_TOO_BIG , <EOL> INVALID_REPOSITORY , <EOL> REPO_FILE_NOT_FOUND ) <EOL> from reviewboard . webapi . resources . diff import DiffResource <EOL> class ValidateDiffResource ( DiffResource ) : <EOL> """<STR_LIT>""" <EOL> added_in = '<STR_LIT>' <EOL> singleton = True <EOL> name = '<STR_LIT>' <EOL> uri_name = '<STR_LIT>' <EOL> uri_object_key = None <EOL> allowed_methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , ) <EOL> item_child_resources = [ ] <EOL> list_child_resources = [ ] <EOL> @ webapi_check_local_site <EOL> @ webapi_check_login_required <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return <NUM_LIT:200> , { <EOL> '<STR_LIT>' : self . get_links ( request = request , * args , ** kwargs ) , <EOL> } <EOL> @ webapi_check_local_site <EOL> @ webapi_login_required <EOL> @ webapi_response_errors ( DOES_NOT_EXIST , NOT_LOGGED_IN , PERMISSION_DENIED , <EOL> REPO_FILE_NOT_FOUND , INVALID_FORM_DATA , <EOL> INVALID_REPOSITORY , DIFF_EMPTY , DIFF_TOO_BIG , <EOL> DIFF_PARSE_ERROR ) <EOL> @ webapi_request_fields ( <EOL> required = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:path>' : { <EOL> '<STR_LIT:type>' : file , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } , <EOL> optional = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : file , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> } <EOL> ) <EOL> def create ( self , request , repository , basedir = None , local_site_name = None , <EOL> base_commit_id = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> local_site = self . _get_local_site ( local_site_name ) <EOL> path = request . FILES . get ( '<STR_LIT:path>' ) <EOL> parent_diff_path = request . FILES . get ( '<STR_LIT>' ) <EOL> try : <EOL> query = Q ( pk = int ( repository ) , local_site = local_site ) <EOL> except ValueError : <EOL> query = ( Q ( local_site = local_site ) <EOL> & ( Q ( path = repository ) <EOL> | Q ( mirror_path = repository ) <EOL> | Q ( name = repository ) ) ) <EOL> try : <EOL> repository = Repository . objects . get ( query ) <EOL> except Repository . DoesNotExist : <EOL> return INVALID_REPOSITORY , { <EOL> '<STR_LIT>' : repository <EOL> } <EOL> except Repository . MultipleObjectsReturned : <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % repository ) <EOL> return INVALID_REPOSITORY . with_message ( msg ) , { <EOL> '<STR_LIT>' : repository , <EOL> } <EOL> if ( not repository . get_scmtool ( ) . get_diffs_use_absolute_paths ( ) and <EOL> basedir is None ) : <EOL> return INVALID_FORM_DATA , { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> } , <EOL> } <EOL> if basedir is None : <EOL> basedir = '<STR_LIT>' <EOL> try : <EOL> DiffSet . objects . create_from_upload ( <EOL> repository , path , parent_diff_path , None , basedir , request , <EOL> base_commit_id = base_commit_id , <EOL> save = False ) <EOL> except FileNotFoundError as e : <EOL> return REPO_FILE_NOT_FOUND , { <EOL> '<STR_LIT:file>' : e . path , <EOL> '<STR_LIT>' : six . text_type ( e . revision ) , <EOL> } <EOL> except EmptyDiffError : <EOL> return DIFF_EMPTY <EOL> except DiffTooBigError as e : <EOL> return DIFF_TOO_BIG , { <EOL> '<STR_LIT>' : six . text_type ( e ) , <EOL> '<STR_LIT>' : e . max_diff_size , <EOL> } <EOL> except DiffParserError as e : <EOL> return DIFF_PARSE_ERROR , { <EOL> '<STR_LIT>' : six . text_type ( e ) , <EOL> '<STR_LIT>' : e . linenum , <EOL> } <EOL> except ShortSHA1Error as e : <EOL> return REPO_FILE_NOT_FOUND , { <EOL> '<STR_LIT>' : six . text_type ( e ) , <EOL> '<STR_LIT:file>' : e . path , <EOL> '<STR_LIT>' : six . text_type ( e . revision ) , <EOL> } <EOL> except SCMError as e : <EOL> return DIFF_PARSE_ERROR . with_message ( six . text_type ( e ) ) <EOL> except Exception as e : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> return DIFF_PARSE_ERROR . with_message ( <EOL> '<STR_LIT>' % e ) <EOL> return <NUM_LIT:200> , { } <EOL> def _build_named_url ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> validate_diff_resource = ValidateDiffResource ( ) </s>
