# Path: invenio_deposit/permissions.py
# def admin_permission_factory():
#     """Factory for creating a permission for an admin `deposit-admin-access`.
# 
#     If `invenio-access` module is installed, it returns a
#     :class:`invenio_access.permissions.Permission` object.
#     Otherwise, it returns a :class:`flask_principal.Permission` object.
# 
#     :returns: Permission instance.
#     """
#     try:
#         pkg_resources.get_distribution('invenio-access')
#         from invenio_access.permissions import Permission
#     except pkg_resources.DistributionNotFound:
#         from flask_principal import Permission
# 
#     return Permission(action_admin_access)


from elasticsearch_dsl import Q, TermsFacet
from flask import has_request_context
from flask_login import current_user
from invenio_search import RecordsSearch
from invenio_search.api import DefaultFilter
from .permissions import admin_permission_factory
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016-2019 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Configuration for deposit search."""




def deposits_filter():
    """Filter list of deposits.

    Permit to the user to see all if:

    * The user is an admin (see
        func:`invenio_deposit.permissions:admin_permission_factory`).

    * It's called outside of a request.

    Otherwise, it filters out any deposit where user is not the owner.
    """

# Path: invenio_deposit/permissions.py
# def admin_permission_factory():
#     """Factory for creating a permission for an admin `deposit-admin-access`.
# 
#     If `invenio-access` module is installed, it returns a
#     :class:`invenio_access.permissions.Permission` object.
#     Otherwise, it returns a :class:`flask_principal.Permission` object.
# 
#     :returns: Permission instance.
#     """
#     try:
#         pkg_resources.get_distribution('invenio-access')
#         from invenio_access.permissions import Permission
#     except pkg_resources.DistributionNotFound:
#         from flask_principal import Permission
# 
#     return Permission(action_admin_access)


# Path: invenio_deposit/search.py
from elasticsearch_dsl import Q, TermsFacet
from flask import has_request_context
from flask_login import current_user
from invenio_search import RecordsSearch
from invenio_search.api import DefaultFilter
from .permissions import admin_permission_factory

# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2016-2019 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Configuration for deposit search."""




def deposits_filter():
    """Filter list of deposits.

    Permit to the user to see all if:

    * The user is an admin (see
        func:`invenio_deposit.permissions:admin_permission_factory`).

    * It's called outside of a request.

    Otherwise, it filters out any deposit where user is not the owner.
    """

    if not has_request_context() or admin_permission_factory().can():