def repository_reviews_by_user(self, trans, **kwd): params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') if 'operation' in kwd: operation = kwd['operation'].lower() # The value of the received id is the encoded review id. review = suc.get_review(trans, kwd['id']) repository = review.repository kwd['id'] = trans.security.encode_id(repository.id) if operation == "inspect repository revisions": return trans.response.send_redirect( web.url_for(controller='repository_review', action='manage_repository_reviews', **kwd)) if operation == "view_or_manage_repository": kwd['changeset_revision'] = review.changeset_revision return trans.response.send_redirect( web.url_for(controller='repository_review', action='view_or_manage_repository', **kwd)) # The user may not be the current user. The value of the received id is the encoded user id. user = suc.get_user(trans, kwd['id']) self.repository_reviews_by_user_grid.title = "All repository revision reviews for user '%s'" % user.username return self.repository_reviews_by_user_grid(trans, **kwd)
def browse_review( self, trans, **kwd ): params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) review = suc.get_review( trans, kwd[ 'id' ] ) repository = review.repository repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) ) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, review.changeset_revision ) return trans.fill_template( '/webapps/community/repository_review/browse_review.mako', repository=repository, changeset_revision_label=changeset_revision_label, review=review, message=message, status=status )
def browse_review(self, trans, **kwd): params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') review = suc.get_review(trans, kwd['id']) repository = review.repository repo = hg.repository(suc.get_configured_ui(), repository.repo_path(trans.app)) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, review.changeset_revision) return trans.fill_template( '/webapps/community/repository_review/browse_review.mako', repository=repository, changeset_revision_label=changeset_revision_label, review=review, message=message, status=status)
def approve_repository_review( self, trans, **kwd ): # The value of the received id is the encoded review id. params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) encoded_review_id = kwd[ 'id' ] review = suc.get_review( trans, encoded_review_id ) if kwd.get( 'approve_repository_review_button', False ): approved_select_field_name = '%s%sapproved' % ( encoded_review_id, STRSEP ) approved_select_field_value = str( kwd[ approved_select_field_name ] ) review.approved = approved_select_field_value trans.sa_session.add( review ) trans.sa_session.flush() message = 'Approved value <b>%s</b> saved for this revision.' % approved_select_field_value repository_id = trans.security.encode_id( review.repository_id ) changeset_revision = review.changeset_revision return trans.response.send_redirect( web.url_for( controller='repository_review', action='manage_repository_reviews_of_revision', id=repository_id, changeset_revision=changeset_revision, message=message, status=status ) )
def repository_reviews_by_user( self, trans, **kwd ): params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) if 'operation' in kwd: operation = kwd['operation'].lower() # The value of the received id is the encoded review id. review = suc.get_review( trans, kwd[ 'id' ] ) repository = review.repository kwd[ 'id' ] = trans.security.encode_id( repository.id ) if operation == "inspect repository revisions": return trans.response.send_redirect( web.url_for( controller='repository_review', action='manage_repository_reviews', **kwd ) ) if operation == "view_or_manage_repository": kwd[ 'changeset_revision' ] = review.changeset_revision return trans.response.send_redirect( web.url_for( controller='repository_review', action='view_or_manage_repository', **kwd ) ) # The user may not be the current user. The value of the received id is the encoded user id. user = suc.get_user( trans, kwd[ 'id' ] ) self.repository_reviews_by_user_grid.title = "All repository revision reviews for user '%s'" % user.username return self.repository_reviews_by_user_grid( trans, **kwd )
def approve_repository_review(self, trans, **kwd): # The value of the received id is the encoded review id. params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') encoded_review_id = kwd['id'] review = suc.get_review(trans, encoded_review_id) if kwd.get('approve_repository_review_button', False): approved_select_field_name = '%s%sapproved' % (encoded_review_id, STRSEP) approved_select_field_value = str(kwd[approved_select_field_name]) review.approved = approved_select_field_value trans.sa_session.add(review) trans.sa_session.flush() message = 'Approved value <b>%s</b> saved for this revision.' % approved_select_field_value repository_id = trans.security.encode_id(review.repository_id) changeset_revision = review.changeset_revision return trans.response.send_redirect( web.url_for(controller='repository_review', action='manage_repository_reviews_of_revision', id=repository_id, changeset_revision=changeset_revision, message=message, status=status))
def edit_review( self, trans, **kwd ): # The value of the received id is the encoded review id. params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) review_id = kwd.get( 'id', None ) review = suc.get_review( trans, review_id ) components_dict = odict() for component in suc.get_components( trans ): components_dict[ component.name ] = dict( component=component, component_review=None ) repository = review.repository repo = hg.repository( suc.get_configured_ui(), repository.repo_path( trans.app ) ) for component_review in review.component_reviews: if component_review and component_review.component: component_name = component_review.component.name if component_name in components_dict: component_review_dict = components_dict[ component_name ] component_review_dict[ 'component_review' ] = component_review components_dict[ component_name ] = component_review_dict # Handle a Save button click. save_button_clicked = False save_buttons = [ '%s%sreview_button' % ( component_name, STRSEP ) for component_name in components_dict.keys() ] save_buttons.append( 'revision_approved_button' ) for save_button in save_buttons: if save_button in kwd: save_button_clicked = True break if save_button_clicked: # Handle the revision_approved_select_field value. revision_approved = kwd.get( 'revision_approved', None ) revision_approved_setting_changed = False if revision_approved: revision_approved = str( revision_approved ) if review.approved != revision_approved: revision_approved_setting_changed = True review.approved = revision_approved trans.sa_session.add( review ) trans.sa_session.flush() saved_component_names = [] for component_name in components_dict.keys(): flushed = False # Retrieve the review information from the form. # The star rating form field is a radio button list, so it will not be received if it was not clicked in the form. # Due to this behavior, default the value to 0. rating = 0 for k, v in kwd.items(): if k.startswith( '%s%s' % ( component_name, STRSEP ) ): component_review_attr = k.replace( '%s%s' % ( component_name, STRSEP ), '' ) if component_review_attr == 'component_id': component_id = str( v ) elif component_review_attr == 'comment': comment = str( v ) elif component_review_attr == 'private': private = CheckboxField.is_checked( v ) elif component_review_attr == 'approved': approved = str( v ) elif component_review_attr == 'rating': rating = int( str( v ) ) component = suc.get_component( trans, component_id ) component_review = suc.get_component_review_by_repository_review_id_component_id( trans, review_id, component_id ) if component_review: # See if the existing component review should be updated. if component_review.comment != comment or \ component_review.private != private or \ component_review.approved != approved or \ component_review.rating != rating: component_review.comment = comment component_review.private = private component_review.approved = approved component_review.rating = rating trans.sa_session.add( component_review ) trans.sa_session.flush() flushed = True saved_component_names.append( component_name ) else: # See if a new component_review should be created. if comment or private or approved != trans.model.ComponentReview.approved_states.NO or rating: component_review = trans.model.ComponentReview( repository_review_id=review.id, component_id=component.id, comment=comment, approved=approved, rating=rating ) trans.sa_session.add( component_review ) trans.sa_session.flush() flushed = True saved_component_names.append( component_name ) if flushed: # Update the repository rating value to be the average of all component review ratings. average_rating = trans.sa_session.query( func.avg( trans.model.ComponentReview.table.c.rating ) ) \ .filter( trans.model.ComponentReview.table.c.repository_review_id == review.id ) \ .scalar() review.rating = int( average_rating ) trans.sa_session.add( review ) trans.sa_session.flush() # Update the information in components_dict. if component_name in components_dict: component_review_dict = components_dict[ component_name ] component_review_dict[ 'component_review' ] = component_review components_dict[ component_name ] = component_review_dict if revision_approved_setting_changed: message += 'Approved value <b>%s</b> saved for this revision.<br/>' % review.approved if saved_component_names: message += 'Reviews were saved for components: %s' % ', '.join( saved_component_names ) if not revision_approved_setting_changed and not saved_component_names: message += 'No changes were made to this review, so nothing was saved.' if review and review.approved: selected_value = review.approved else: selected_value = trans.model.ComponentReview.approved_states.NO revision_approved_select_field = build_approved_select_field( trans, name='revision_approved', selected_value=selected_value, for_component=False ) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, review.changeset_revision ) return trans.fill_template( '/webapps/community/repository_review/edit_review.mako', repository=repository, review=review, changeset_revision_label=changeset_revision_label, revision_approved_select_field=revision_approved_select_field, components_dict=components_dict, message=message, status=status )
def create_review( self, trans, **kwd ): # The value of the received id is the encoded repository id. params = util.Params( kwd ) message = util.restore_text( params.get( 'message', '' ) ) status = params.get( 'status', 'done' ) repository_id = kwd.get( 'id', None ) changeset_revision = kwd.get( 'changeset_revision', None ) previous_review_id = kwd.get( 'previous_review_id', None ) create_without_copying = 'create_without_copying' in kwd if repository_id: if changeset_revision: # Make sure there is not already a review of the revision by the user. repository = suc.get_repository_in_tool_shed( trans, repository_id ) if suc.get_review_by_repository_id_changeset_revision_user_id( trans=trans, repository_id=repository_id, changeset_revision=changeset_revision, user_id=trans.security.encode_id( trans.user.id ) ): message = "You have already created a review for revision <b>%s</b> of repository <b>%s</b>." % ( changeset_revision, repository.name ) status = "error" else: # See if there are any reviews for previous changeset revisions that the user can copy. if not create_without_copying and not previous_review_id and suc.has_previous_repository_reviews( trans, repository, changeset_revision ): return trans.response.send_redirect( web.url_for( controller='repository_review', action='select_previous_review', **kwd ) ) # A review can be initially performed only on an installable revision of a repository, so make sure we have metadata associated # with the received changeset_revision. repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision ) if repository_metadata: metadata = repository_metadata.metadata if metadata: review = trans.app.model.RepositoryReview( repository_id=repository_metadata.repository_id, changeset_revision=changeset_revision, user_id=trans.user.id, rating=None, deleted=False ) trans.sa_session.add( review ) trans.sa_session.flush() if previous_review_id: review_to_copy = suc.get_review( trans, previous_review_id ) self.copy_review( trans, review_to_copy, review ) review_id = trans.security.encode_id( review.id ) message = "Begin your review of revision <b>%s</b> of repository <b>%s</b>." \ % ( changeset_revision, repository.name ) status = 'done' trans.response.send_redirect( web.url_for( controller='repository_review', action='edit_review', id=review_id, message=message, status=status ) ) else: message = "A new review cannot be created for revision <b>%s</b> of repository <b>%s</b>. Select a valid revision and try again." \ % ( changeset_revision, repository.name ) kwd[ 'message' ] = message kwd[ 'status' ] = 'error' else: return trans.response.send_redirect( web.url_for( controller='repository_review', action='manage_repository_reviews', **kwd ) ) return trans.response.send_redirect( web.url_for( controller='repository_review', action='view_or_manage_repository', **kwd ) )
def edit_review(self, trans, **kwd): # The value of the received id is the encoded review id. params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') review_id = kwd.get('id', None) review = suc.get_review(trans, review_id) components_dict = odict() for component in suc.get_components(trans): components_dict[component.name] = dict(component=component, component_review=None) repository = review.repository repo = hg.repository(suc.get_configured_ui(), repository.repo_path(trans.app)) for component_review in review.component_reviews: if component_review and component_review.component: component_name = component_review.component.name if component_name in components_dict: component_review_dict = components_dict[component_name] component_review_dict[ 'component_review'] = component_review components_dict[component_name] = component_review_dict # Handle a Save button click. save_button_clicked = False save_buttons = [ '%s%sreview_button' % (component_name, STRSEP) for component_name in components_dict.keys() ] save_buttons.append('revision_approved_button') for save_button in save_buttons: if save_button in kwd: save_button_clicked = True break if save_button_clicked: # Handle the revision_approved_select_field value. revision_approved = kwd.get('revision_approved', None) revision_approved_setting_changed = False if revision_approved: revision_approved = str(revision_approved) if review.approved != revision_approved: revision_approved_setting_changed = True review.approved = revision_approved trans.sa_session.add(review) trans.sa_session.flush() saved_component_names = [] for component_name in components_dict.keys(): flushed = False # Retrieve the review information from the form. # The star rating form field is a radio button list, so it will not be received if it was not clicked in the form. # Due to this behavior, default the value to 0. rating = 0 for k, v in kwd.items(): if k.startswith('%s%s' % (component_name, STRSEP)): component_review_attr = k.replace( '%s%s' % (component_name, STRSEP), '') if component_review_attr == 'component_id': component_id = str(v) elif component_review_attr == 'comment': comment = str(v) elif component_review_attr == 'private': private = CheckboxField.is_checked(v) elif component_review_attr == 'approved': approved = str(v) elif component_review_attr == 'rating': rating = int(str(v)) component = suc.get_component(trans, component_id) component_review = suc.get_component_review_by_repository_review_id_component_id( trans, review_id, component_id) if component_review: # See if the existing component review should be updated. if component_review.comment != comment or \ component_review.private != private or \ component_review.approved != approved or \ component_review.rating != rating: component_review.comment = comment component_review.private = private component_review.approved = approved component_review.rating = rating trans.sa_session.add(component_review) trans.sa_session.flush() flushed = True saved_component_names.append(component_name) else: # See if a new component_review should be created. if comment or private or approved != trans.model.ComponentReview.approved_states.NO or rating: component_review = trans.model.ComponentReview( repository_review_id=review.id, component_id=component.id, comment=comment, approved=approved, rating=rating) trans.sa_session.add(component_review) trans.sa_session.flush() flushed = True saved_component_names.append(component_name) if flushed: # Update the repository rating value to be the average of all component review ratings. average_rating = trans.sa_session.query( func.avg( trans.model.ComponentReview.table.c.rating ) ) \ .filter( trans.model.ComponentReview.table.c.repository_review_id == review.id ) \ .scalar() review.rating = int(average_rating) trans.sa_session.add(review) trans.sa_session.flush() # Update the information in components_dict. if component_name in components_dict: component_review_dict = components_dict[component_name] component_review_dict[ 'component_review'] = component_review components_dict[component_name] = component_review_dict if revision_approved_setting_changed: message += 'Approved value <b>%s</b> saved for this revision.<br/>' % review.approved if saved_component_names: message += 'Reviews were saved for components: %s' % ', '.join( saved_component_names) if not revision_approved_setting_changed and not saved_component_names: message += 'No changes were made to this review, so nothing was saved.' if review and review.approved: selected_value = review.approved else: selected_value = trans.model.ComponentReview.approved_states.NO revision_approved_select_field = build_approved_select_field( trans, name='revision_approved', selected_value=selected_value, for_component=False) rev, changeset_revision_label = suc.get_rev_label_from_changeset_revision( repo, review.changeset_revision) return trans.fill_template( '/webapps/community/repository_review/edit_review.mako', repository=repository, review=review, changeset_revision_label=changeset_revision_label, revision_approved_select_field=revision_approved_select_field, components_dict=components_dict, message=message, status=status)
def create_review(self, trans, **kwd): # The value of the received id is the encoded repository id. params = util.Params(kwd) message = util.restore_text(params.get('message', '')) status = params.get('status', 'done') repository_id = kwd.get('id', None) changeset_revision = kwd.get('changeset_revision', None) previous_review_id = kwd.get('previous_review_id', None) create_without_copying = 'create_without_copying' in kwd if repository_id: if changeset_revision: # Make sure there is not already a review of the revision by the user. repository = suc.get_repository_in_tool_shed( trans, repository_id) if suc.get_review_by_repository_id_changeset_revision_user_id( trans=trans, repository_id=repository_id, changeset_revision=changeset_revision, user_id=trans.security.encode_id(trans.user.id)): message = "You have already created a review for revision <b>%s</b> of repository <b>%s</b>." % ( changeset_revision, repository.name) status = "error" else: # See if there are any reviews for previous changeset revisions that the user can copy. if not create_without_copying and not previous_review_id and suc.has_previous_repository_reviews( trans, repository, changeset_revision): return trans.response.send_redirect( web.url_for(controller='repository_review', action='select_previous_review', **kwd)) # A review can be initially performed only on an installable revision of a repository, so make sure we have metadata associated # with the received changeset_revision. repository_metadata = suc.get_repository_metadata_by_changeset_revision( trans, repository_id, changeset_revision) if repository_metadata: metadata = repository_metadata.metadata if metadata: review = trans.app.model.RepositoryReview( repository_id=repository_metadata. repository_id, changeset_revision=changeset_revision, user_id=trans.user.id, rating=None, deleted=False) trans.sa_session.add(review) trans.sa_session.flush() if previous_review_id: review_to_copy = suc.get_review( trans, previous_review_id) self.copy_review(trans, review_to_copy, review) review_id = trans.security.encode_id(review.id) message = "Begin your review of revision <b>%s</b> of repository <b>%s</b>." \ % ( changeset_revision, repository.name ) status = 'done' trans.response.send_redirect( web.url_for(controller='repository_review', action='edit_review', id=review_id, message=message, status=status)) else: message = "A new review cannot be created for revision <b>%s</b> of repository <b>%s</b>. Select a valid revision and try again." \ % ( changeset_revision, repository.name ) kwd['message'] = message kwd['status'] = 'error' else: return trans.response.send_redirect( web.url_for(controller='repository_review', action='manage_repository_reviews', **kwd)) return trans.response.send_redirect( web.url_for(controller='repository_review', action='view_or_manage_repository', **kwd))