Exemplo n.º 1
0
class AdminRepositoryGrid(RepositoryGrid):
    class DeletedColumn(grids.BooleanColumn):
        def get_value(self, trans, grid, repository):
            if repository.deleted:
                return 'yes'
            return ''

    columns = [
        RepositoryGrid.NameColumn(
            "Name",
            key="name",
            link=(lambda item: dict(operation="view_or_manage_repository",
                                    id=item.id)),
            attach_popup=True),
        RepositoryGrid.HeadsColumn("Heads"),
        RepositoryGrid.UserColumn(
            "Owner",
            model_class=model.User,
            link=(lambda item: dict(operation="repositories_by_user",
                                    id=item.id)),
            attach_popup=False,
            key="User.username"),
        RepositoryGrid.DeprecatedColumn("Deprecated",
                                        key="deprecated",
                                        attach_popup=False),
        # Columns that are valid for filtering but are not visible.
        DeletedColumn("Deleted", key="deleted", attach_popup=False)
    ]
    columns.append(
        grids.MulticolFilterColumn("Search repository name",
                                   cols_to_filter=[columns[0]],
                                   key="free-text-search",
                                   visible=False,
                                   filterable="standard"))
    operations = [operation for operation in RepositoryGrid.operations]
    operations.append(
        grids.GridOperation("Delete",
                            allow_multiple=False,
                            condition=(lambda item: not item.deleted),
                            async_compatible=False))
    operations.append(
        grids.GridOperation("Undelete",
                            allow_multiple=False,
                            condition=(lambda item: item.deleted),
                            async_compatible=False))
    standard_filters = []
    default_filter = {}

    def build_initial_query(self, trans, **kwd):
        return trans.sa_session.query(model.Repository) \
                               .join(model.User.table)
class RepositoriesWithReviewsGrid(RepositoryGrid):
    # This grid filters out repositories that have been marked as either deprecated or deleted.

    class WithReviewsRevisionColumn(grids.GridColumn):
        def get_value(self, trans, grid, repository):
            # Restrict to revisions that have been reviewed.
            if repository.reviews:
                rval = ''
                repo = hg_util.get_repo_for_repository(trans.app,
                                                       repository=repository,
                                                       repo_path=None,
                                                       create=False)
                for review in repository.reviews:
                    changeset_revision = review.changeset_revision
                    rev, label = hg_util.get_rev_label_from_changeset_revision(
                        repo, changeset_revision)
                    rval += '<a href="manage_repository_reviews_of_revision?id=%s&changeset_revision=%s">%s</a><br/>' % \
                        (trans.security.encode_id(repository.id), changeset_revision, label)
                return rval
            return ''

    class WithoutReviewsRevisionColumn(grids.GridColumn):
        def get_value(self, trans, grid, repository):
            # Restrict the options to revisions that have not yet been reviewed.
            repository_metadata_revisions = metadata_util.get_repository_metadata_revisions_for_review(
                repository, reviewed=False)
            if repository_metadata_revisions:
                rval = ''
                for repository_metadata in repository_metadata_revisions:
                    rev, label, changeset_revision = \
                        hg_util.get_rev_label_changeset_revision_from_repository_metadata(trans.app,
                                                                                          repository_metadata,
                                                                                          repository=repository,
                                                                                          include_date=True,
                                                                                          include_hash=False)
                    rval += '<a href="manage_repository_reviews_of_revision?id=%s&changeset_revision=%s">%s</a><br/>' % \
                        (trans.security.encode_id(repository.id), changeset_revision, label)
                return rval
            return ''

    class ReviewersColumn(grids.TextColumn):
        def get_value(self, trans, grid, repository):
            rval = ''
            if repository.reviewers:
                for user in repository.reviewers:
                    rval += '<a class="view-info" href="repository_reviews_by_user?id=%s">' % trans.security.encode_id(
                        user.id)
                    rval += '%s</a> | ' % user.username
                rval = rval.rstrip(' | ')
            return rval

    class RatingColumn(grids.TextColumn):
        def get_value(self, trans, grid, repository):
            rval = ''
            for review in repository.reviews:
                if review.rating:
                    for index in range(1, 6):
                        rval += '<input '
                        rval += 'name="star1-%s" ' % trans.security.encode_id(
                            review.id)
                        rval += 'type="radio" '
                        rval += 'class="community_rating_star star" '
                        rval += 'disabled="disabled" '
                        rval += 'value="%s" ' % str(review.rating)
                        if review.rating > (index - 0.5) and review.rating < (
                                index + 0.5):
                            rval += 'checked="checked" '
                        rval += '/>'
                rval += '<br/>'
            return rval

    class ApprovedColumn(grids.TextColumn):
        def get_value(self, trans, grid, repository):
            rval = ''
            for review in repository.reviews:
                if review.approved:
                    rval += '%s<br/>' % review.approved
            return rval

    title = "All reviewed repositories"
    model_class = model.Repository
    template = '/webapps/tool_shed/repository_review/grid.mako'
    default_sort_key = "Repository.name"
    columns = [
        RepositoryGrid.NameColumn(
            "Repository name",
            key="name",
            link=(lambda item: dict(operation="view_or_manage_repository",
                                    id=item.id)),
            attach_popup=True),
        RepositoryGrid.UserColumn("Owner",
                                  model_class=model.User,
                                  attach_popup=False,
                                  key="User.username"),
        WithReviewsRevisionColumn("Reviewed revisions"),
        ReviewersColumn("Reviewers", attach_popup=False),
        RatingColumn("Rating", attach_popup=False),
        ApprovedColumn("Approved", attach_popup=False)
    ]
    columns.append(
        grids.MulticolFilterColumn("Search repository name",
                                   cols_to_filter=[columns[0]],
                                   key="free-text-search",
                                   visible=False,
                                   filterable="standard"))
    operations = [
        grids.GridOperation("Inspect repository revisions",
                            allow_multiple=False,
                            condition=(lambda item: not item.deleted),
                            async_compatible=False)
    ]

    def build_initial_query(self, trans, **kwd):
        return trans.sa_session.query(model.Repository) \
                               .filter(and_(model.Repository.table.c.deleted == false(),
                                            model.Repository.table.c.deprecated == false())) \
                               .join((model.RepositoryReview.table, model.RepositoryReview.table.c.repository_id == model.Repository.table.c.id)) \
                               .join((model.User.table, model.User.table.c.id == model.Repository.table.c.user_id)) \
                               .outerjoin((model.ComponentReview.table, model.ComponentReview.table.c.repository_review_id == model.RepositoryReview.table.c.id)) \
                               .outerjoin((model.Component.table, model.Component.table.c.id == model.ComponentReview.table.c.component_id))