<s> from __future__ import unicode_literals <EOL> import logging <EOL> from django . contrib . auth . models import User <EOL> from django . core . cache import cache <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db import models <EOL> from django . db . models import Count , Q <EOL> from django . utils import six , timezone <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from djblets . cache . backend import make_cache_key <EOL> from djblets . db . fields import CounterField , ModificationTimestampField <EOL> from djblets . db . query import get_object_or_none <EOL> from reviewboard . attachments . models import ( FileAttachment , <EOL> FileAttachmentHistory ) <EOL> from reviewboard . changedescs . models import ChangeDescription <EOL> from reviewboard . diffviewer . models import DiffSet , DiffSetHistory <EOL> from reviewboard . reviews . errors import ( PermissionError , <EOL> PublishError ) <EOL> from reviewboard . reviews . fields import get_review_request_field <EOL> from reviewboard . reviews . managers import ReviewRequestManager <EOL> from reviewboard . reviews . models . base_comment import BaseComment <EOL> from reviewboard . reviews . models . base_review_request_details import BaseReviewRequestDetails <EOL> from reviewboard . reviews . models . group import Group <EOL> from reviewboard . reviews . models . screenshot import Screenshot <EOL> from reviewboard . reviews . signals import ( review_request_closed , <EOL> review_request_closing , <EOL> review_request_published , <EOL> review_request_publishing , <EOL> review_request_reopened , <EOL> review_request_reopening ) <EOL> from reviewboard . scmtools . models import Repository <EOL> from reviewboard . site . models import LocalSite <EOL> from reviewboard . site . urlresolvers import local_site_reverse <EOL> def fetch_issue_counts ( review_request , extra_query = None ) : <EOL> """<STR_LIT>""" <EOL> issue_counts = { <EOL> BaseComment . OPEN : <NUM_LIT:0> , <EOL> BaseComment . RESOLVED : <NUM_LIT:0> , <EOL> BaseComment . DROPPED : <NUM_LIT:0> <EOL> } <EOL> q = Q ( public = True ) & Q ( base_reply_to__isnull = True ) <EOL> if extra_query : <EOL> q = q & extra_query <EOL> issue_statuses = review_request . reviews . filter ( q ) . values ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if issue_statuses : <EOL> comment_fields = { <EOL> '<STR_LIT>' : set ( ) , <EOL> '<STR_LIT>' : set ( ) , <EOL> '<STR_LIT>' : set ( ) , <EOL> '<STR_LIT>' : set ( ) , <EOL> } <EOL> for issue_fields in issue_statuses : <EOL> for key , comments in six . iteritems ( comment_fields ) : <EOL> issue_opened = issue_fields [ key + '<STR_LIT>' ] <EOL> comment_pk = issue_fields [ key + '<STR_LIT>' ] <EOL> if issue_opened and comment_pk not in comments : <EOL> comments . add ( comment_pk ) <EOL> issue_status = issue_fields [ key + '<STR_LIT>' ] <EOL> if issue_status : <EOL> issue_counts [ issue_status ] += <NUM_LIT:1> <EOL> logging . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> review_request . pk , len ( issue_statuses ) , issue_counts , <EOL> issue_statuses , comment_fields ) <EOL> return issue_counts <EOL> def _initialize_issue_counts ( review_request ) : <EOL> """<STR_LIT>""" <EOL> if review_request . pk is None : <EOL> return <NUM_LIT:0> <EOL> issue_counts = fetch_issue_counts ( review_request ) <EOL> review_request . issue_open_count = issue_counts [ BaseComment . OPEN ] <EOL> review_request . issue_resolved_count = issue_counts [ BaseComment . RESOLVED ] <EOL> review_request . issue_dropped_count = issue_counts [ BaseComment . DROPPED ] <EOL> review_request . save ( update_fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> return None <EOL> class ReviewRequest ( BaseReviewRequestDetails ) : <EOL> """<STR_LIT>""" <EOL> PENDING_REVIEW = "<STR_LIT:P>" <EOL> SUBMITTED = "<STR_LIT:S>" <EOL> DISCARDED = "<STR_LIT:D>" <EOL> STATUSES = ( <EOL> ( PENDING_REVIEW , _ ( '<STR_LIT>' ) ) , <EOL> ( SUBMITTED , _ ( '<STR_LIT>' ) ) , <EOL> ( DISCARDED , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> ISSUE_COUNTER_FIELDS = { <EOL> BaseComment . OPEN : '<STR_LIT>' , <EOL> BaseComment . RESOLVED : '<STR_LIT>' , <EOL> BaseComment . DROPPED : '<STR_LIT>' , <EOL> } <EOL> summary = models . CharField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> max_length = BaseReviewRequestDetails . MAX_SUMMARY_LENGTH ) <EOL> submitter = models . ForeignKey ( User , verbose_name = _ ( "<STR_LIT>" ) , <EOL> related_name = "<STR_LIT>" ) <EOL> time_added = models . DateTimeField ( _ ( "<STR_LIT>" ) , default = timezone . now ) <EOL> last_updated = ModificationTimestampField ( _ ( "<STR_LIT>" ) ) <EOL> status = models . CharField ( _ ( "<STR_LIT:status>" ) , max_length = <NUM_LIT:1> , choices = STATUSES , <EOL> db_index = True ) <EOL> public = models . BooleanField ( _ ( "<STR_LIT>" ) , default = False ) <EOL> changenum = models . PositiveIntegerField ( _ ( "<STR_LIT>" ) , blank = True , <EOL> null = True , db_index = True ) <EOL> repository = models . ForeignKey ( Repository , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> null = True , <EOL> blank = True ) <EOL> email_message_id = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:255> , <EOL> blank = True , null = True ) <EOL> time_emailed = models . DateTimeField ( _ ( "<STR_LIT>" ) , null = True , <EOL> default = None , blank = True ) <EOL> diffset_history = models . ForeignKey ( DiffSetHistory , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> blank = True ) <EOL> target_groups = models . ManyToManyField ( <EOL> Group , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> blank = True ) <EOL> target_people = models . ManyToManyField ( <EOL> User , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> related_name = "<STR_LIT>" , <EOL> blank = True ) <EOL> screenshots = models . ManyToManyField ( <EOL> Screenshot , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> blank = True ) <EOL> inactive_screenshots = models . ManyToManyField ( <EOL> Screenshot , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> related_name = "<STR_LIT>" , <EOL> blank = True ) <EOL> file_attachments = models . ManyToManyField ( <EOL> FileAttachment , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> blank = True ) <EOL> inactive_file_attachments = models . ManyToManyField ( <EOL> FileAttachment , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> related_name = "<STR_LIT>" , <EOL> blank = True ) <EOL> file_attachment_histories = models . ManyToManyField ( <EOL> FileAttachmentHistory , <EOL> related_name = '<STR_LIT>' , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> blank = True ) <EOL> changedescs = models . ManyToManyField ( <EOL> ChangeDescription , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> related_name = "<STR_LIT>" , <EOL> blank = True ) <EOL> depends_on = models . ManyToManyField ( '<STR_LIT>' , <EOL> blank = True , null = True , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> related_name = '<STR_LIT>' ) <EOL> last_review_activity_timestamp = models . DateTimeField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> db_column = '<STR_LIT>' , <EOL> null = True , <EOL> default = None , <EOL> blank = True ) <EOL> shipit_count = CounterField ( _ ( "<STR_LIT>" ) , default = <NUM_LIT:0> ) <EOL> issue_open_count = CounterField ( <EOL> _ ( '<STR_LIT>' ) , <EOL> initializer = _initialize_issue_counts ) <EOL> issue_resolved_count = CounterField ( <EOL> _ ( '<STR_LIT>' ) , <EOL> initializer = _initialize_issue_counts ) <EOL> issue_dropped_count = CounterField ( <EOL> _ ( '<STR_LIT>' ) , <EOL> initializer = _initialize_issue_counts ) <EOL> local_site = models . ForeignKey ( LocalSite , blank = True , null = True , <EOL> related_name = '<STR_LIT>' ) <EOL> local_id = models . IntegerField ( '<STR_LIT>' , blank = True , null = True ) <EOL> objects = ReviewRequestManager ( ) <EOL> def get_commit ( self ) : <EOL> if self . commit_id is not None : <EOL> return self . commit_id <EOL> elif self . changenum is not None : <EOL> self . commit_id = six . text_type ( self . changenum ) <EOL> ReviewRequest . objects . filter ( pk = self . pk ) . update ( <EOL> commit_id = six . text_type ( self . changenum ) ) <EOL> return self . commit_id <EOL> return None <EOL> def set_commit ( self , commit_id ) : <EOL> try : <EOL> self . changenum = int ( commit_id ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> self . commit_id = commit_id <EOL> commit = property ( get_commit , set_commit ) <EOL> @ property <EOL> def approved ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _calculate_approval ( ) <EOL> return self . _approved <EOL> @ property <EOL> def approval_failure ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _calculate_approval ( ) <EOL> return self . _approval_failure <EOL> def get_participants ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ u for review in self . reviews . all ( ) <EOL> for u in review . participants ] <EOL> participants = property ( get_participants ) <EOL> def get_new_reviews ( self , user ) : <EOL> """<STR_LIT>""" <EOL> if user . is_authenticated ( ) : <EOL> if hasattr ( self , "<STR_LIT>" ) and self . new_review_count > <NUM_LIT:0> : <EOL> query = self . visits . filter ( user = user ) <EOL> try : <EOL> visit = query [ <NUM_LIT:0> ] <EOL> return self . reviews . filter ( <EOL> public = True , <EOL> timestamp__gt = visit . timestamp ) . exclude ( user = user ) <EOL> except IndexError : <EOL> pass <EOL> return self . reviews . get_empty_query_set ( ) <EOL> def get_display_id ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . local_site_id : <EOL> return self . local_id <EOL> else : <EOL> return self . id <EOL> display_id = property ( get_display_id ) <EOL> def get_public_reviews ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . reviews . filter ( public = True , base_reply_to__isnull = True ) <EOL> def is_accessible_by ( self , user , local_site = None , request = None , <EOL> silent = False ) : <EOL> """<STR_LIT>""" <EOL> if self . submitter == user : <EOL> return True <EOL> if not self . public and not self . is_mutable_by ( user ) : <EOL> if not silent : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . pk , self . display_id , user , <EOL> request = request ) <EOL> return False <EOL> if self . repository and not self . repository . is_accessible_by ( user ) : <EOL> if not silent : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . pk , self . display_id , user , <EOL> request = request ) <EOL> return False <EOL> if local_site and not local_site . is_accessible_by ( user ) : <EOL> if not silent : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . pk , self . display_id , user , <EOL> request = request ) <EOL> return False <EOL> if ( user . is_authenticated ( ) and <EOL> self . target_people . filter ( pk = user . pk ) . count ( ) > <NUM_LIT:0> ) : <EOL> return True <EOL> groups = list ( self . target_groups . all ( ) ) <EOL> if not groups : <EOL> return True <EOL> for group in groups : <EOL> if group . is_accessible_by ( user , silent = silent ) : <EOL> return True <EOL> if not silent : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . pk , self . display_id , user , request = request ) <EOL> return False <EOL> def is_mutable_by ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return ( self . submitter == user or <EOL> user . has_perm ( '<STR_LIT>' , <EOL> self . local_site ) ) <EOL> def is_status_mutable_by ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return ( self . submitter == user or <EOL> user . has_perm ( '<STR_LIT>' , self . local_site ) ) <EOL> def is_deletable_by ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return user . has_perm ( '<STR_LIT>' ) <EOL> def get_draft ( self , user = None ) : <EOL> """<STR_LIT>""" <EOL> if not user : <EOL> return get_object_or_none ( self . draft ) <EOL> elif user . is_authenticated ( ) : <EOL> return get_object_or_none ( self . draft , <EOL> review_request__submitter = user ) <EOL> return None <EOL> def get_pending_review ( self , user ) : <EOL> """<STR_LIT>""" <EOL> from reviewboard . reviews . models . review import Review <EOL> return Review . objects . get_pending_review ( self , user ) <EOL> def get_last_activity ( self , diffsets = None , reviews = None ) : <EOL> """<STR_LIT>""" <EOL> timestamp = self . last_updated <EOL> updated_object = self <EOL> if not diffsets and self . repository_id : <EOL> latest_diffset = self . get_latest_diffset ( ) <EOL> diffsets = [ ] <EOL> if latest_diffset : <EOL> diffsets . append ( latest_diffset ) <EOL> if diffsets : <EOL> for diffset in diffsets : <EOL> if diffset . timestamp >= timestamp : <EOL> timestamp = diffset . timestamp <EOL> updated_object = diffset <EOL> if not reviews : <EOL> try : <EOL> reviews = [ self . reviews . filter ( public = True ) . latest ( ) ] <EOL> except ObjectDoesNotExist : <EOL> reviews = [ ] <EOL> for review in reviews : <EOL> if review . public and review . timestamp >= timestamp : <EOL> timestamp = review . timestamp <EOL> updated_object = review <EOL> return timestamp , updated_object <EOL> def changeset_is_pending ( self , commit_id ) : <EOL> """<STR_LIT>""" <EOL> cache_key = make_cache_key ( <EOL> '<STR_LIT>' % ( self . pk , commit_id ) ) <EOL> cached_values = cache . get ( cache_key ) <EOL> if cached_values : <EOL> return cached_values <EOL> is_pending = False <EOL> scmtool = self . repository . get_scmtool ( ) <EOL> if ( scmtool . supports_pending_changesets and <EOL> commit_id is not None ) : <EOL> changeset = scmtool . get_changeset ( commit_id , allow_empty = True ) <EOL> if changeset : <EOL> is_pending = changeset . pending <EOL> new_commit_id = six . text_type ( changeset . changenum ) <EOL> if commit_id != new_commit_id : <EOL> self . commit_id = new_commit_id <EOL> self . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> commit_id = new_commit_id <EOL> draft = self . get_draft ( ) <EOL> if draft : <EOL> draft . commit_id = new_commit_id <EOL> draft . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> if is_pending : <EOL> cache . set ( cache_key , ( is_pending , commit_id ) , <NUM_LIT> ) <EOL> else : <EOL> cache . set ( cache_key , ( is_pending , commit_id ) ) <EOL> return is_pending , commit_id <EOL> def get_absolute_url ( self ) : <EOL> if self . local_site : <EOL> local_site_name = self . local_site . name <EOL> else : <EOL> local_site_name = None <EOL> return local_site_reverse ( <EOL> '<STR_LIT>' , <EOL> local_site_name = local_site_name , <EOL> kwargs = { '<STR_LIT>' : self . display_id } ) <EOL> def get_diffsets ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . repository_id : <EOL> return [ ] <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _diffsets = list ( <EOL> DiffSet . objects <EOL> . filter ( history__pk = self . diffset_history_id ) <EOL> . annotate ( file_count = Count ( '<STR_LIT>' ) ) <EOL> . prefetch_related ( '<STR_LIT>' ) ) <EOL> return self . _diffsets <EOL> def get_latest_diffset ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return DiffSet . objects . filter ( <EOL> history = self . diffset_history_id ) . latest ( ) <EOL> except DiffSet . DoesNotExist : <EOL> return None <EOL> def get_close_description ( self ) : <EOL> """<STR_LIT>""" <EOL> changedescs = list ( self . changedescs . all ( ) ) <EOL> latest_changedesc = None <EOL> for changedesc in changedescs : <EOL> if changedesc . public : <EOL> latest_changedesc = changedesc <EOL> break <EOL> close_description = '<STR_LIT>' <EOL> is_rich_text = False <EOL> if latest_changedesc and '<STR_LIT:status>' in latest_changedesc . fields_changed : <EOL> status = latest_changedesc . fields_changed [ '<STR_LIT:status>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if status in ( ReviewRequest . DISCARDED , ReviewRequest . SUBMITTED ) : <EOL> close_description = latest_changedesc . text <EOL> is_rich_text = latest_changedesc . rich_text <EOL> return ( close_description , is_rich_text ) <EOL> def get_blocks ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _blocks = list ( self . blocks . all ( ) ) <EOL> return self . _blocks <EOL> def save ( self , update_counts = False , ** kwargs ) : <EOL> if update_counts or self . id is None : <EOL> self . _update_counts ( ) <EOL> if self . status != self . PENDING_REVIEW : <EOL> self . visits . all ( ) . delete ( ) <EOL> super ( ReviewRequest , self ) . save ( ** kwargs ) <EOL> def delete ( self , ** kwargs ) : <EOL> from reviewboard . accounts . models import Profile , LocalSiteProfile <EOL> profile , profile_is_new = Profile . objects . get_or_create ( user = self . submitter ) <EOL> if profile_is_new : <EOL> profile . save ( ) <EOL> local_site = self . local_site <EOL> site_profile , site_profile_is_new = LocalSiteProfile . objects . get_or_create ( user = self . submitter , <EOL> profile = profile , <EOL> local_site = local_site ) <EOL> site_profile . decrement_total_outgoing_request_count ( ) <EOL> if self . status == self . PENDING_REVIEW : <EOL> site_profile . decrement_pending_outgoing_request_count ( ) <EOL> if self . public : <EOL> self . _decrement_reviewer_counts ( ) <EOL> super ( ReviewRequest , self ) . delete ( ** kwargs ) <EOL> def can_publish ( self ) : <EOL> return not self . public or get_object_or_none ( self . draft ) is not None <EOL> def close ( self , type , user = None , description = None , rich_text = False ) : <EOL> """<STR_LIT>""" <EOL> if ( user and not self . is_mutable_by ( user ) and <EOL> not user . has_perm ( "<STR_LIT>" , self . local_site ) ) : <EOL> raise PermissionError <EOL> if type not in [ self . SUBMITTED , self . DISCARDED ] : <EOL> raise AttributeError ( "<STR_LIT>" % type ) <EOL> review_request_closing . send ( sender = self . __class__ , <EOL> user = user , <EOL> review_request = self , <EOL> type = type , <EOL> description = description , <EOL> rich_text = rich_text ) <EOL> draft = get_object_or_none ( self . draft ) <EOL> if self . status != type : <EOL> if ( draft is not None and <EOL> not self . public and type == self . DISCARDED ) : <EOL> draft . copy_fields_to_request ( self ) <EOL> changedesc = ChangeDescription ( public = True , <EOL> text = description or "<STR_LIT>" , <EOL> rich_text = rich_text or False , <EOL> user = user or self . submitter ) <EOL> status_field = get_review_request_field ( '<STR_LIT:status>' ) ( self ) <EOL> status_field . record_change_entry ( changedesc , self . status , type ) <EOL> changedesc . save ( ) <EOL> self . changedescs . add ( changedesc ) <EOL> if type == self . SUBMITTED : <EOL> if not self . public : <EOL> raise PublishError ( "<STR_LIT>" ) <EOL> else : <EOL> self . commit_id = None <EOL> self . status = type <EOL> self . save ( update_counts = True ) <EOL> review_request_closed . send ( sender = self . __class__ , user = user , <EOL> review_request = self , <EOL> type = type , <EOL> description = description , <EOL> rich_text = rich_text ) <EOL> else : <EOL> changedesc = self . changedescs . filter ( public = True ) . latest ( ) <EOL> changedesc . timestamp = timezone . now ( ) <EOL> changedesc . text = description or "<STR_LIT>" <EOL> changedesc . rich_text = rich_text <EOL> changedesc . save ( ) <EOL> self . save ( ) <EOL> if draft is not None : <EOL> draft . delete ( ) <EOL> def reopen ( self , user = None ) : <EOL> """<STR_LIT>""" <EOL> from reviewboard . reviews . models . review_request_draft import ReviewRequestDraft <EOL> if ( user and not self . is_mutable_by ( user ) and <EOL> not user . has_perm ( "<STR_LIT>" , self . local_site ) ) : <EOL> raise PermissionError <EOL> if self . status != self . PENDING_REVIEW : <EOL> review_request_reopening . send ( sender = self . __class__ , <EOL> user = user , <EOL> review_request = self ) <EOL> changedesc = ChangeDescription ( user = user or self . submitter ) <EOL> status_field = get_review_request_field ( '<STR_LIT:status>' ) ( self ) <EOL> status_field . record_change_entry ( changedesc , self . status , <EOL> self . PENDING_REVIEW ) <EOL> if self . status == self . DISCARDED : <EOL> self . public = False <EOL> changedesc . save ( ) <EOL> draft = ReviewRequestDraft . create ( self ) <EOL> draft . changedesc = changedesc <EOL> draft . save ( ) <EOL> else : <EOL> changedesc . public = True <EOL> changedesc . save ( ) <EOL> self . changedescs . add ( changedesc ) <EOL> self . status = self . PENDING_REVIEW <EOL> self . save ( update_counts = True ) <EOL> review_request_reopened . send ( sender = self . __class__ , user = user , <EOL> review_request = self ) <EOL> def publish ( self , user , trivial = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_mutable_by ( user ) : <EOL> raise PermissionError <EOL> draft = get_object_or_none ( self . draft ) <EOL> review_request_publishing . send ( sender = self . __class__ , user = user , <EOL> review_request_draft = draft ) <EOL> if self . public : <EOL> self . _decrement_reviewer_counts ( ) <EOL> if draft is not None : <EOL> try : <EOL> changes = draft . publish ( self , send_notification = False , <EOL> user = user ) <EOL> except Exception : <EOL> if self . public : <EOL> self . _increment_reviewer_counts ( ) <EOL> raise <EOL> draft . delete ( ) <EOL> else : <EOL> changes = None <EOL> if not self . public and self . changedescs . count ( ) == <NUM_LIT:0> : <EOL> self . time_added = timezone . now ( ) <EOL> self . public = True <EOL> self . save ( update_counts = True ) <EOL> review_request_published . send ( sender = self . __class__ , user = user , <EOL> review_request = self , trivial = trivial , <EOL> changedesc = changes ) <EOL> def determine_user_for_changedesc ( self , changedesc ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in changedesc . fields_changed : <EOL> entry = changedesc . fields_changed [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> return User . objects . get ( pk = entry [ <NUM_LIT:2> ] ) <EOL> user_pk = None <EOL> changes = ( <EOL> self . changedescs <EOL> . filter ( pk__lt = changedesc . pk ) <EOL> . order_by ( '<STR_LIT>' ) <EOL> ) <EOL> for changedesc in changes : <EOL> if '<STR_LIT>' in changedesc . fields_changed : <EOL> user_pk = changedesc . fields_changed [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> break <EOL> if user_pk : <EOL> return User . objects . get ( pk = user_pk ) <EOL> return self . submitter <EOL> def _update_counts ( self ) : <EOL> from reviewboard . accounts . models import Profile , LocalSiteProfile <EOL> profile , profile_is_new = Profile . objects . get_or_create ( user = self . submitter ) <EOL> if profile_is_new : <EOL> profile . save ( ) <EOL> local_site = self . local_site <EOL> site_profile , site_profile_is_new = LocalSiteProfile . objects . get_or_create ( <EOL> user = self . submitter , <EOL> profile = profile , <EOL> local_site = local_site ) <EOL> if site_profile_is_new : <EOL> site_profile . save ( ) <EOL> if self . id is None : <EOL> site_profile . increment_total_outgoing_request_count ( ) <EOL> old_status = None <EOL> old_public = False <EOL> else : <EOL> r = ReviewRequest . objects . get ( pk = self . id ) <EOL> old_status = r . status <EOL> old_public = r . public <EOL> if self . status == self . PENDING_REVIEW : <EOL> if old_status != self . status : <EOL> site_profile . increment_pending_outgoing_request_count ( ) <EOL> if self . public and self . id is not None : <EOL> self . _increment_reviewer_counts ( ) <EOL> elif old_status == self . PENDING_REVIEW : <EOL> if old_status != self . status : <EOL> site_profile . decrement_pending_outgoing_request_count ( ) <EOL> if old_public : <EOL> self . _decrement_reviewer_counts ( ) <EOL> def _increment_reviewer_counts ( self ) : <EOL> from reviewboard . accounts . models import LocalSiteProfile <EOL> groups = self . target_groups . all ( ) <EOL> people = self . target_people . all ( ) <EOL> Group . incoming_request_count . increment ( groups ) <EOL> LocalSiteProfile . direct_incoming_request_count . increment ( <EOL> LocalSiteProfile . objects . filter ( user__in = people , <EOL> local_site = self . local_site ) ) <EOL> LocalSiteProfile . total_incoming_request_count . increment ( <EOL> LocalSiteProfile . objects . filter ( <EOL> Q ( local_site = self . local_site ) & <EOL> Q ( Q ( user__review_groups__in = groups ) | <EOL> Q ( user__in = people ) ) ) ) <EOL> LocalSiteProfile . starred_public_request_count . increment ( <EOL> LocalSiteProfile . objects . filter ( <EOL> profile__starred_review_requests = self , <EOL> local_site = self . local_site ) ) <EOL> def _decrement_reviewer_counts ( self ) : <EOL> from reviewboard . accounts . models import LocalSiteProfile <EOL> groups = self . target_groups . all ( ) <EOL> people = self . target_people . all ( ) <EOL> Group . incoming_request_count . decrement ( groups ) <EOL> LocalSiteProfile . direct_incoming_request_count . decrement ( <EOL> LocalSiteProfile . objects . filter ( <EOL> user__in = people , <EOL> local_site = self . local_site ) ) <EOL> LocalSiteProfile . total_incoming_request_count . decrement ( <EOL> LocalSiteProfile . objects . filter ( <EOL> Q ( local_site = self . local_site ) & <EOL> Q ( Q ( user__review_groups__in = groups ) | <EOL> Q ( user__in = people ) ) ) ) <EOL> LocalSiteProfile . starred_public_request_count . decrement ( <EOL> LocalSiteProfile . objects . filter ( <EOL> profile__starred_review_requests = self , <EOL> local_site = self . local_site ) ) <EOL> def _calculate_approval ( self ) : <EOL> """<STR_LIT>""" <EOL> from reviewboard . extensions . hooks import ReviewRequestApprovalHook <EOL> approved = True <EOL> failure = None <EOL> if self . shipit_count == <NUM_LIT:0> : <EOL> approved = False <EOL> failure = '<STR_LIT>' <EOL> elif self . issue_open_count > <NUM_LIT:0> : <EOL> approved = False <EOL> failure = '<STR_LIT>' <EOL> for hook in ReviewRequestApprovalHook . hooks : <EOL> try : <EOL> result = hook . is_approved ( self , approved , failure ) <EOL> if isinstance ( result , tuple ) : <EOL> approved , failure = result <EOL> elif isinstance ( result , bool ) : <EOL> approved = result <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( hook , result ) ) <EOL> if approved : <EOL> failure = None <EOL> except Exception as e : <EOL> extension = hook . extension <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> extension . id , e , exc_info = <NUM_LIT:1> ) <EOL> self . _approval_failure = failure <EOL> self . _approved = approved <EOL> def get_review_request ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> ordering = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> unique_together = ( ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> permissions = ( <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ) </s>
