<s> from __future__ import unicode_literals <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db . models import Q <EOL> from django . utils import six <EOL> from djblets . util . decorators import augment_method_from <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 , NOT_LOGGED_IN , <EOL> PERMISSION_DENIED ) <EOL> from reviewboard . reviews . errors import PublishError <EOL> from reviewboard . reviews . models import Review <EOL> from reviewboard . webapi . base import WebAPIResource <EOL> from reviewboard . webapi . decorators import webapi_check_local_site <EOL> from reviewboard . webapi . errors import PUBLISH_ERROR <EOL> from reviewboard . webapi . mixins import MarkdownFieldsMixin <EOL> from reviewboard . webapi . resources import resources <EOL> from reviewboard . webapi . resources . user import UserResource <EOL> class BaseReviewResource ( MarkdownFieldsMixin , WebAPIResource ) : <EOL> """<STR_LIT>""" <EOL> model = Review <EOL> fields = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : MarkdownFieldsMixin . TEXT_TYPES , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : MarkdownFieldsMixin . TEXT_TYPES , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : dict , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:id>' : { <EOL> '<STR_LIT:type>' : int , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : bool , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : bool , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : MarkdownFieldsMixin . TEXT_TYPES , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:user>' : { <EOL> '<STR_LIT:type>' : UserResource , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> allowed_methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> CREATE_UPDATE_OPTIONAL_FIELDS = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : bool , <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>' : True , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : MarkdownFieldsMixin . SAVEABLE_TEXT_TYPES , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : six . text_type , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : MarkdownFieldsMixin . SAVEABLE_TEXT_TYPES , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : MarkdownFieldsMixin . TEXT_TYPES , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : bool , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : MarkdownFieldsMixin . SAVEABLE_TEXT_TYPES , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT:\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:type>' : bool , <EOL> '<STR_LIT:description>' : '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> def get_queryset ( self , request , is_list = False , * args , ** kwargs ) : <EOL> review_request = resources . review_request . get_object ( <EOL> request , * args , ** kwargs ) <EOL> q = Q ( review_request = review_request ) & Q ( ** self . get_base_reply_to_field ( * args , ** kwargs ) ) <EOL> if is_list : <EOL> q = q & Q ( public = True ) <EOL> return self . model . objects . filter ( q ) <EOL> def get_base_reply_to_field ( self ) : <EOL> raise NotImplementedError <EOL> def has_access_permissions ( self , request , review , * args , ** kwargs ) : <EOL> return review . is_accessible_by ( request . user ) <EOL> def has_modify_permissions ( self , request , review , * args , ** kwargs ) : <EOL> return review . is_mutable_by ( request . user ) <EOL> def has_delete_permissions ( self , request , review , * args , ** kwargs ) : <EOL> return review . is_mutable_by ( request . user ) <EOL> def serialize_absolute_url_field ( self , obj , request , ** kwargs ) : <EOL> return request . build_absolute_uri ( obj . get_absolute_url ( ) ) <EOL> def serialize_body_top_text_type_field ( self , obj , ** kwargs ) : <EOL> return None <EOL> def serialize_body_bottom_text_type_field ( self , obj , ** kwargs ) : <EOL> return None <EOL> @ webapi_check_local_site <EOL> @ webapi_login_required <EOL> @ webapi_response_errors ( DOES_NOT_EXIST , NOT_LOGGED_IN , PERMISSION_DENIED ) <EOL> @ webapi_request_fields ( <EOL> optional = CREATE_UPDATE_OPTIONAL_FIELDS , <EOL> allow_unknown = True <EOL> ) <EOL> def create ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> review_request = resources . review_request . get_object ( request , * args , ** kwargs ) <EOL> except ObjectDoesNotExist : <EOL> return DOES_NOT_EXIST <EOL> review , is_new = Review . objects . get_or_create ( <EOL> review_request = review_request , <EOL> user = request . user , <EOL> public = False , <EOL> ** self . get_base_reply_to_field ( * args , ** kwargs ) ) <EOL> if is_new : <EOL> status_code = <NUM_LIT> <EOL> else : <EOL> status_code = <NUM_LIT> <EOL> result = self . _update_review ( request , review , * args , ** kwargs ) <EOL> if not isinstance ( result , tuple ) or result [ <NUM_LIT:0> ] != <NUM_LIT:200> : <EOL> return result <EOL> else : <EOL> return status_code , result [ <NUM_LIT:1> ] , { <EOL> '<STR_LIT>' : self . get_href ( review , 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> @ webapi_request_fields ( <EOL> optional = CREATE_UPDATE_OPTIONAL_FIELDS , <EOL> allow_unknown = True <EOL> ) <EOL> def update ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> resources . review_request . get_object ( request , * args , ** kwargs ) <EOL> review = resources . review . get_object ( request , * args , ** kwargs ) <EOL> except ObjectDoesNotExist : <EOL> return DOES_NOT_EXIST <EOL> return self . _update_review ( request , review , * args , ** kwargs ) <EOL> @ webapi_check_local_site <EOL> @ augment_method_from ( WebAPIResource ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ webapi_check_local_site <EOL> @ augment_method_from ( WebAPIResource ) <EOL> def get ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _update_review ( self , request , review , public = None , <EOL> publish_to_submitter_only = False , extra_fields = { } , <EOL> * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . has_modify_permissions ( request , review ) : <EOL> return self . get_no_access_error ( request ) <EOL> if '<STR_LIT>' in kwargs : <EOL> review . ship_it = kwargs [ '<STR_LIT>' ] <EOL> self . set_text_fields ( review , '<STR_LIT>' , ** kwargs ) <EOL> self . set_text_fields ( review , '<STR_LIT>' , ** kwargs ) <EOL> self . import_extra_data ( review , review . extra_data , extra_fields ) <EOL> review . save ( ) <EOL> if public : <EOL> try : <EOL> review . publish ( user = request . user , <EOL> to_submitter_only = publish_to_submitter_only ) <EOL> except PublishError as e : <EOL> return PUBLISH_ERROR . with_message ( six . text_type ( e ) ) <EOL> return <NUM_LIT:200> , { <EOL> self . item_result_key : review , <EOL> } </s>
