Пример #1
0
 def edit_component( self, trans, **kwd ):
     message = escape( kwd.get( 'message', '' ) )
     status = kwd.get( 'status', 'done' )
     id = kwd.get( 'id', None )
     if not id:
         message = "No component ids received for editing"
         trans.response.send_redirect( web.url_for( controller='admin',
                                                    action='manage_categories',
                                                    message=message,
                                                    status='error' ) )
     component = review_util.get_component( trans.app, id )
     if kwd.get( 'edit_component_button', False ):
         new_description = kwd.get( 'description', '' ).strip()
         if component.description != new_description:
             component.description = new_description
             trans.sa_session.add( component )
             trans.sa_session.flush()
             message = "The information has been saved for the component named <b>%s</b>" % escape( component.name )
             status = 'done'
             return trans.response.send_redirect( web.url_for( controller='repository_review',
                                                               action='manage_components',
                                                               message=message,
                                                               status=status ) )
     return trans.fill_template( '/webapps/tool_shed/repository_review/edit_component.mako',
                                 component=component,
                                 message=message,
                                 status=status )
Пример #2
0
 def edit_review( self, trans, **kwd ):
     # The value of the received id is the encoded review id.
     message = escape( kwd.get( 'message', '' ) )
     status = kwd.get( 'status', 'done' )
     review_id = kwd.get( 'id', None )
     review = review_util.get_review( trans.app, review_id )
     components_dict = odict()
     for component in review_util.get_components( trans.app ):
         components_dict[ component.name ] = dict( component=component, component_review=None )
     repository = review.repository
     repo = hg_util.get_repo_for_repository( trans.app, repository=repository, repo_path=None, create=False )
     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' % ( comp_name, STRSEP ) for comp_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 = review_util.get_component( trans.app, component_id )
             component_review = \
                 review_util.get_component_review_by_repository_review_id_component_id( trans.app,
                                                                                        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( and_( trans.model.ComponentReview.table.c.repository_review_id == review.id,
                                                                 trans.model.ComponentReview.table.c.deleted == false(),
                                                                 trans.model.ComponentReview.table.c.approved != trans.model.ComponentReview.approved_states.NA ) ) \
                                                  .scalar()
                 if average_rating is not None:
                     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 = grids_util.build_approved_select_field( trans,
                                                                              name='revision_approved',
                                                                              selected_value=selected_value,
                                                                              for_component=False )
     rev, changeset_revision_label = hg_util.get_rev_label_from_changeset_revision( repo, review.changeset_revision )
     return trans.fill_template( '/webapps/tool_shed/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 )