示例#1
0
    def filter_communities(cls, p, so, with_deleted=False):
        """Search for communities.

        Helper function which takes from database only those communities which
        match search criteria. Uses parameter 'so' to set communities in the
        correct order.

        Parameter 'page' is introduced to restrict results and return only
        slice of them for the current page. If page == 0 function will return
        all communities that match the pattern.
        """
        query = cls.query if with_deleted else \
            cls.query.filter(cls.deleted_at.is_(None))

        if p:
            p = p.replace(' ', '%')
            query = query.filter(db.or_(
                cls.id.ilike('%' + p + '%'),
                cls.title.ilike('%' + p + '%'),
                cls.description.ilike('%' + p + '%'),
            ))

        if so in current_app.config['COMMUNITIES_SORTING_OPTIONS']:
            order = so == 'title' and db.asc or db.desc
            query = query.order_by(order(getattr(cls, so)))
        else:
            query = query.order_by(db.desc(cls.ranking))
        return query
示例#2
0
文件: api.py 项目: weko3-dev35/weko
 def get_logs(self):
     """Get logs."""
     try:
         with db.session.begin_nested():
             resync_logs = db.session.query(ResyncLogs).filter_by(
                 resync_indexes_id=int(self.id)).order_by(
                     db.desc(ResyncLogs.id)).all()
             result = [
                 dict(
                     **{
                         "id":
                         logs.id,
                         "start_time":
                         logs.start_time.strftime('%Y-%m-%dT%H:%M:%S%z')
                         if logs.start_time else '',
                         "end_time":
                         logs.end_time.strftime('%Y-%m-%dT%H:%M:%S%z'
                                                ) if logs.end_time else '',
                         "status":
                         logs.status,
                         "errmsg":
                         logs.errmsg,
                         "counter":
                         logs.counter,
                         "log_type":
                         logs.log_type,
                     }) for logs in resync_logs
             ]
             return result
     except Exception as ex:
         current_app.logger.debug(ex)
         return False
示例#3
0
    def filter_communities(cls, p, so, with_deleted=False):
        """Search for communities.

        Helper function which takes from database only those communities which
        match search criteria. Uses parameter 'so' to set communities in the
        correct order.

        Parameter 'page' is introduced to restrict results and return only
        slice of them for the current page. If page == 0 function will return
        all communities that match the pattern.
        """
        query = cls.query if with_deleted else \
            cls.query.filter(cls.deleted_at.is_(None))

        if p:
            query = query.filter(db.or_(
                cls.id.like("%" + p + "%"),
                cls.title.like("%" + p + "%"),
                cls.description.like("%" + p + "%"),
            ))

        if so in current_app.config['COMMUNITIES_SORTING_OPTIONS']:
            order = so == 'title' and db.asc or db.desc
            query = query.order_by(order(getattr(cls, so)))
        else:
            query = query.order_by(db.desc(cls.ranking))
        return query
示例#4
0
 def latest_release(self, status=None):
     """Chronologically latest published release of the repository."""
     # Bail out fast if object not in DB session.
     if self not in db.session:
         return None
     q = self.releases if status is None else self.releases.filter_by(
         status=status)
     return q.order_by(db.desc(Release.created)).first()
示例#5
0
 def latest_release(self, status=None):
     """Chronologically latest published release of the repository."""
     # Bail out fast if object not in DB session.
     if self not in db.session:
         return None
     q = self.releases if status is None else self.releases.filter_by(
         status=status)
     return q.order_by(db.desc(Release.created)).first()
示例#6
0
def repository(name):
    """Display selected repository."""
    user_id = current_user.id
    github = GitHubAPI(user_id=user_id)
    token = github.session_token

    if token:
        repos = github.account.extra_data.get('repos', [])
        repo = next((repo for repo_id, repo in repos.items()
                     if repo.get('full_name') == name), {})
        if not repo:
            abort(403)

        try:
            # NOTE: Here we do not check for repository ownership, since it
            # might have changed even though the user might have made releases
            # in the past.
            repo_instance = Repository.get(user_id=user_id,
                                           github_id=repo['id'],
                                           check_owner=False)
        except RepositoryAccessError:
            abort(403)
        except NoResultFound:
            repo_instance = Repository(name=repo['full_name'],
                                       github_id=repo['id'])

        releases = [
            current_github.release_api_class(r) for r in (
                repo_instance.releases.order_by(db.desc(Release.created)).all()
                if repo_instance.id else []
            )
        ]
        return render_template(
            current_app.config['GITHUB_TEMPLATE_VIEW'],
            repo=repo_instance,
            releases=releases,
            serializer=current_github.record_serializer,
        )

    abort(403)
示例#7
0
def repository(name):
    """Display selected repository."""
    user_id = current_user.id
    github = GitHubAPI(user_id=user_id)
    token = github.session_token

    if token:
        repos = github.account.extra_data.get('repos', [])
        repo = next((repo for repo_id, repo in repos.items()
                     if repo.get('full_name') == name), {})
        if not repo:
            abort(403)

        try:
            # NOTE: Here we do not check for repository ownership, since it
            # might have changed even though the user might have made releases
            # in the past.
            repo_instance = Repository.get(user_id=user_id,
                                           github_id=repo['id'],
                                           check_owner=False)
        except RepositoryAccessError:
            abort(403)
        except NoResultFound:
            repo_instance = Repository(name=repo['full_name'],
                                       github_id=repo['id'])

        releases = [
            current_github.release_api_class(r)
            for r in (repo_instance.releases.order_by(db.desc(Release.created))
                      .all() if repo_instance.id else [])
        ]
        return render_template(
            current_app.config['GITHUB_TEMPLATE_VIEW'],
            repo=repo_instance,
            releases=releases,
            serializer=current_github.record_serializer,
        )

    abort(403)