'Return a query containing repositories for resetting metadata.  The order parameter
is used for displaying the list of repositories ordered alphabetically for display on
a page.  When called from the Tool Shed API, order is False.'

def get_query_for_setting_metadata_on_repositories(self, my_writable=False, order=True):

    if my_writable:
        username = self.user.username
        clause_list = []
        for repository in self.sa_session.query(self.app.model.Repository).filter((self.app.model.Repository.table.c.deleted == false())):
            if (repository.type in [rt_util.REPOSITORY_SUITE_DEFINITION, rt_util.TOOL_DEPENDENCY_DEFINITION]):
                clause_list.append((self.app.model.Repository.table.c.id == repository.id))
            else:
                allow_push = repository.allow_push(self.app)
                if allow_push:
                    allow_push_usernames = allow_push.split(',')
                    if (username in allow_push_usernames):
                        clause_list.append((self.app.model.Repository.table.c.id == repository.id))
        if clause_list:
            if order:
                return self.sa_session.query(self.app.model.Repository).filter(or_(*clause_list)).order_by(self.app.model.Repository.table.c.name, self.app.model.Repository.table.c.user_id)
            else:
                return self.sa_session.query(self.app.model.Repository).filter(or_(*clause_list))
        else:
            return self.sa_session.query(self.app.model.Repository).filter((self.app.model.Repository.table.c.id == (-1)))
    elif order:
        return self.sa_session.query(self.app.model.Repository).filter((self.app.model.Repository.table.c.deleted == false())).order_by(self.app.model.Repository.table.c.name, self.app.model.Repository.table.c.user_id)
    else:
        return self.sa_session.query(self.app.model.Repository).filter((self.app.model.Repository.table.c.deleted == false()))
