# Path: invenio_communities/views/decorators.py
# def pass_community(f):
#     """Decorate to retrieve the community record using the community service.
#     """
#     @wraps(f)
#     def view(**kwargs):
#         pid_value = kwargs['pid_value']
#         community = service().read(
#             id_=pid_value, identity=g.identity
#         )
#         kwargs['community'] = community
#         return f(**kwargs)
#     return view
# 
# def pass_community_logo(f):
#     """Decorate a view to pass a community logo using the files service."""
#     @wraps(f)
#     def view(**kwargs):
#         """."""
#         try:
#             pid_value = kwargs['pid_value']
#             files = service().read_logo(
#                 id_=pid_value, identity=g.identity
#             )
#             kwargs['logo'] = files
#         except FileNotFoundError:
#             kwargs['logo'] = None
# 
#         return f(**kwargs)
#     return view
# 
# def require_community_owner(f):
#     """Decorate a view to require community owner for accessing the view."""
#     @wraps(f)
#     def view(**kwargs):
#         """."""
#         pid_value = kwargs['pid_value']
#         community = service().read(
#             id_=pid_value, identity=g.identity
#         )
#         if community.has_permissions_to(["update"])["can_update"]:
#             return f(**kwargs)
#         else:
#             raise PermissionDeniedError()
# 
#     return view


from flask import current_app, render_template
from flask_login import login_required
from .decorators import pass_community, pass_community_logo, \
    require_community_owner
                        'helpText': 'Your community is restricted to users ' \
                                    'with access.'
                    }
                ]
            ),
            SITE_UI_URL=current_app.config["SITE_UI_URL"]
        ),
    )

@pass_community
@pass_community_logo
def communities_detail(community=None, logo=None, pid_value=None):
    """Community detail page."""
    return render_template(
        "invenio_communities/details/index.html",
        community=community.to_dict(), # TODO: use serializer
        logo=logo.to_dict() if logo else None,
        # TODO: inject this from a vocabulary in the community
        types={
            "organization": "Organization",
            "event": "Event",
            "topic": "Topic",
            "project": "Project"
        },
        # Pass permissions so we can disable partially UI components
        # e.g Settings tab
        permissions=community.has_permissions_to(['update']),
        active_menu_tab="search"
    )


# Path: invenio_communities/views/decorators.py
# def pass_community(f):
#     """Decorate to retrieve the community record using the community service.
#     """
#     @wraps(f)
#     def view(**kwargs):
#         pid_value = kwargs['pid_value']
#         community = service().read(
#             id_=pid_value, identity=g.identity
#         )
#         kwargs['community'] = community
#         return f(**kwargs)
#     return view
# 
# def pass_community_logo(f):
#     """Decorate a view to pass a community logo using the files service."""
#     @wraps(f)
#     def view(**kwargs):
#         """."""
#         try:
#             pid_value = kwargs['pid_value']
#             files = service().read_logo(
#                 id_=pid_value, identity=g.identity
#             )
#             kwargs['logo'] = files
#         except FileNotFoundError:
#             kwargs['logo'] = None
# 
#         return f(**kwargs)
#     return view
# 
# def require_community_owner(f):
#     """Decorate a view to require community owner for accessing the view."""
#     @wraps(f)
#     def view(**kwargs):
#         """."""
#         pid_value = kwargs['pid_value']
#         community = service().read(
#             id_=pid_value, identity=g.identity
#         )
#         if community.has_permissions_to(["update"])["can_update"]:
#             return f(**kwargs)
#         else:
#             raise PermissionDeniedError()
# 
#     return view


# Path: invenio_communities/views/communities.py
from flask import current_app, render_template
from flask_login import login_required
from .decorators import pass_community, pass_community_logo, \
    require_community_owner

                        'helpText': 'Your community is restricted to users ' \
                                    'with access.'
                    }
                ]
            ),
            SITE_UI_URL=current_app.config["SITE_UI_URL"]
        ),
    )

@pass_community
@pass_community_logo
def communities_detail(community=None, logo=None, pid_value=None):
    """Community detail page."""
    return render_template(
        "invenio_communities/details/index.html",
        community=community.to_dict(), # TODO: use serializer
        logo=logo.to_dict() if logo else None,
        # TODO: inject this from a vocabulary in the community
        types={
            "organization": "Organization",
            "event": "Event",
            "topic": "Topic",
            "project": "Project"
        },
        # Pass permissions so we can disable partially UI components
        # e.g Settings tab
        permissions=community.has_permissions_to(['update']),
        active_menu_tab="search"
    )


@pass_community