'Rate a repository and return updated rating data.'

@web.expose
@web.require_login('rate    repositories')
def rate_repository(self, trans, **kwd):

    message = escape(kwd.get('message', ''))
    status = kwd.get('status', 'done')
    id = kwd.get('id', None)
    if (not id):
        return trans.response.send_redirect(web.url_for(controller='repository', action='browse_repositories', message='Select    a    repository    to    rate', status='error'))
    repository = repository_util.get_repository_in_tool_shed(trans.app, id)
    changeset_revision = repository.tip(trans.app)
    if (repository.user == trans.user):
        return trans.response.send_redirect(web.url_for(controller='repository', action='browse_repositories', message='You    are    not    allowed    to    rate    your    own    repository', status='error'))
    if kwd.get('rate_button', False):
        rating = int(kwd.get('rating', '0'))
        comment = kwd.get('comment', '')
        rating = self.rate_item(trans, trans.user, repository, rating, comment)
    (avg_rating, num_ratings) = self.get_ave_item_rating_data(trans.sa_session, repository, webapp_model=trans.model)
    display_reviews = util.string_as_bool(kwd.get('display_reviews', False))
    rra = self.get_user_item_rating(trans.sa_session, trans.user, repository, webapp_model=trans.model)
    metadata = metadata_util.get_repository_metadata_by_repository_id_changeset_revision(trans.app, id, changeset_revision, metadata_only=True)
    repository_type_select_field = rt_util.build_repository_type_select_field(trans, repository=repository)
    revision_label = hg_util.get_revision_label(trans.app, repository, changeset_revision, include_date=True)
    return trans.fill_template('/webapps/tool_shed/repository/rate_repository.mako', repository=repository, metadata=metadata, revision_label=revision_label, avg_rating=avg_rating, display_reviews=display_reviews, num_ratings=num_ratings, rra=rra, repository_type_select_field=repository_type_select_field, message=message, status=status)
