<s> from __future__ import unicode_literals <EOL> import copy <EOL> from django . contrib . auth . models import User <EOL> from django . db import models <EOL> from django . db . models import F <EOL> from django . utils import timezone <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from djblets . db . fields import ModificationTimestampField <EOL> from djblets . db . managers import ConcurrencyManager <EOL> from reviewboard . attachments . models import FileAttachment <EOL> from reviewboard . changedescs . models import ChangeDescription <EOL> from reviewboard . diffviewer . models import DiffSet <EOL> from reviewboard . reviews . errors import NotModifiedError <EOL> from reviewboard . reviews . models . group import Group <EOL> from reviewboard . reviews . models . base_review_request_details import BaseReviewRequestDetails <EOL> from reviewboard . reviews . models . review_request import ReviewRequest <EOL> from reviewboard . reviews . models . screenshot import Screenshot <EOL> from reviewboard . reviews . fields import get_review_request_fields <EOL> from reviewboard . reviews . signals import review_request_published <EOL> class ReviewRequestDraft ( BaseReviewRequestDetails ) : <EOL> """<STR_LIT>""" <EOL> summary = models . CharField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> max_length = BaseReviewRequestDetails . MAX_SUMMARY_LENGTH ) <EOL> owner = models . ForeignKey ( <EOL> User , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> null = True , <EOL> related_name = '<STR_LIT>' ) <EOL> review_request = models . ForeignKey ( <EOL> ReviewRequest , <EOL> related_name = "<STR_LIT>" , <EOL> verbose_name = _ ( "<STR_LIT>" ) , <EOL> unique = True ) <EOL> last_updated = ModificationTimestampField ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> diffset = models . ForeignKey ( <EOL> DiffSet , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> blank = True , <EOL> null = True , <EOL> related_name = '<STR_LIT>' ) <EOL> changedesc = models . ForeignKey ( <EOL> ChangeDescription , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> blank = True , <EOL> null = 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> 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> related_name = "<STR_LIT>" , <EOL> blank = True ) <EOL> submitter = property ( lambda self : self . owner or <EOL> self . review_request . submitter ) <EOL> repository = property ( lambda self : self . review_request . repository ) <EOL> local_site = property ( lambda self : self . review_request . local_site ) <EOL> depends_on = models . ManyToManyField ( '<STR_LIT>' , <EOL> blank = True , null = True , <EOL> verbose_name = _ ( '<STR_LIT>' ) , <EOL> related_name = '<STR_LIT>' ) <EOL> objects = ConcurrencyManager ( ) <EOL> commit = property ( lambda self : self . commit_id , <EOL> lambda self , value : setattr ( self , '<STR_LIT>' , value ) ) <EOL> def get_latest_diffset ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . diffset <EOL> def is_accessible_by ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return self . is_mutable_by ( user ) <EOL> def is_mutable_by ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return self . review_request . is_mutable_by ( user ) <EOL> @ staticmethod <EOL> def create ( review_request ) : <EOL> """<STR_LIT>""" <EOL> draft , draft_is_new = ReviewRequestDraft . objects . get_or_create ( <EOL> review_request = review_request , <EOL> defaults = { <EOL> '<STR_LIT>' : review_request . summary , <EOL> '<STR_LIT:description>' : review_request . description , <EOL> '<STR_LIT>' : review_request . testing_done , <EOL> '<STR_LIT>' : review_request . bugs_closed , <EOL> '<STR_LIT>' : review_request . branch , <EOL> '<STR_LIT>' : <EOL> review_request . description_rich_text , <EOL> '<STR_LIT>' : <EOL> review_request . testing_done_rich_text , <EOL> '<STR_LIT>' : review_request . rich_text , <EOL> '<STR_LIT>' : review_request . commit_id , <EOL> } ) <EOL> if draft . changedesc is None and review_request . public : <EOL> draft . changedesc = ChangeDescription . objects . create ( ) <EOL> if draft_is_new : <EOL> draft . target_groups = review_request . target_groups . all ( ) <EOL> draft . target_people = review_request . target_people . all ( ) <EOL> draft . depends_on = review_request . depends_on . all ( ) <EOL> draft . extra_data = copy . deepcopy ( review_request . extra_data ) <EOL> draft . save ( ) <EOL> review_request . screenshots . update ( draft_caption = F ( '<STR_LIT>' ) ) <EOL> draft . screenshots = review_request . screenshots . all ( ) <EOL> review_request . inactive_screenshots . update ( <EOL> draft_caption = F ( '<STR_LIT>' ) ) <EOL> draft . inactive_screenshots = review_request . inactive_screenshots . all ( ) <EOL> review_request . file_attachments . update ( draft_caption = F ( '<STR_LIT>' ) ) <EOL> draft . file_attachments = review_request . file_attachments . all ( ) <EOL> review_request . inactive_file_attachments . update ( <EOL> draft_caption = F ( '<STR_LIT>' ) ) <EOL> draft . inactive_file_attachments = review_request . inactive_file_attachments . all ( ) <EOL> return draft <EOL> def publish ( self , review_request = None , user = None , trivial = False , <EOL> send_notification = True ) : <EOL> """<STR_LIT>""" <EOL> if not review_request : <EOL> review_request = self . review_request <EOL> if not self . changedesc and review_request . public : <EOL> self . changedesc = ChangeDescription ( ) <EOL> if not user : <EOL> if self . changedesc : <EOL> user = self . changedesc . get_user ( self ) <EOL> else : <EOL> user = review_request . submitter <EOL> self . copy_fields_to_request ( review_request ) <EOL> if self . diffset : <EOL> self . diffset . history = review_request . diffset_history <EOL> self . diffset . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> if self . changedesc and not self . changedesc . has_modified_fields ( ) : <EOL> raise NotModifiedError ( ) <EOL> if self . changedesc : <EOL> self . changedesc . user = user <EOL> self . changedesc . timestamp = timezone . now ( ) <EOL> self . changedesc . public = True <EOL> self . changedesc . save ( ) <EOL> review_request . changedescs . add ( self . changedesc ) <EOL> review_request . description_rich_text = self . description_rich_text <EOL> review_request . testing_done_rich_text = self . testing_done_rich_text <EOL> review_request . rich_text = self . rich_text <EOL> review_request . save ( ) <EOL> if send_notification : <EOL> review_request_published . send ( sender = review_request . __class__ , <EOL> user = user , <EOL> review_request = review_request , <EOL> trivial = trivial , <EOL> changedesc = self . changedesc ) <EOL> return self . changedesc <EOL> def copy_fields_to_request ( self , review_request ) : <EOL> """<STR_LIT>""" <EOL> def update_list ( a , b , name , record_changes = True , name_field = None ) : <EOL> aset = set ( [ x . id for x in a . all ( ) ] ) <EOL> bset = set ( [ x . id for x in b . all ( ) ] ) <EOL> if aset . symmetric_difference ( bset ) : <EOL> if record_changes and self . changedesc : <EOL> self . changedesc . record_field_change ( name , a . all ( ) , b . all ( ) , <EOL> name_field ) <EOL> a . clear ( ) <EOL> for item in b . all ( ) : <EOL> a . add ( item ) <EOL> for field_cls in get_review_request_fields ( ) : <EOL> field = field_cls ( review_request ) <EOL> if field . can_record_change_entry : <EOL> old_value = field . load_value ( review_request ) <EOL> new_value = field . load_value ( self ) <EOL> if field . has_value_changed ( old_value , new_value ) : <EOL> field . save_value ( new_value ) <EOL> if self . changedesc : <EOL> field . record_change_entry ( self . changedesc , <EOL> old_value , new_value ) <EOL> screenshots = list ( self . screenshots . all ( ) ) <EOL> caption_changes = { } <EOL> for s in review_request . screenshots . all ( ) : <EOL> if s in screenshots and s . caption != s . draft_caption : <EOL> caption_changes [ s . id ] = { <EOL> '<STR_LIT>' : ( s . caption , ) , <EOL> '<STR_LIT>' : ( s . draft_caption , ) , <EOL> } <EOL> s . caption = s . draft_caption <EOL> s . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> for s in screenshots : <EOL> if s . caption != s . draft_caption : <EOL> s . caption = s . draft_caption <EOL> s . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> if caption_changes and self . changedesc : <EOL> self . changedesc . fields_changed [ '<STR_LIT>' ] = caption_changes <EOL> update_list ( review_request . screenshots , self . screenshots , <EOL> '<STR_LIT>' , name_field = "<STR_LIT>" ) <EOL> review_request . inactive_screenshots = self . inactive_screenshots . all ( ) <EOL> files = list ( self . file_attachments . all ( ) ) <EOL> caption_changes = { } <EOL> for f in review_request . file_attachments . all ( ) : <EOL> if f in files and f . caption != f . draft_caption : <EOL> caption_changes [ f . id ] = { <EOL> '<STR_LIT>' : ( f . caption , ) , <EOL> '<STR_LIT>' : ( f . draft_caption , ) , <EOL> } <EOL> f . caption = f . draft_caption <EOL> f . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> for f in files : <EOL> if f . caption != f . draft_caption : <EOL> f . caption = f . draft_caption <EOL> f . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> if caption_changes and self . changedesc : <EOL> self . changedesc . fields_changed [ '<STR_LIT>' ] = caption_changes <EOL> update_list ( review_request . file_attachments , self . file_attachments , <EOL> '<STR_LIT>' , name_field = "<STR_LIT>" ) <EOL> review_request . inactive_file_attachments = self . inactive_file_attachments . all ( ) <EOL> def get_review_request ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . review_request <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> ordering = [ '<STR_LIT>' ] </s>
