# 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

@login_required
def communities_new():
    """Communities creation page."""
    return render_template(
        "invenio_communities/new.html",
        form_config=dict(
            access=dict(
                visibilty=[
                    {
                        'text': 'Public',
                        'value': 'public',
                        'icon': 'group',
                        'helpText': 'Your community is publicly accessible ' \
                                    'and shows up in search results.'
                    },
                    {
                        'text': 'Restricted',
                        'value': 'restricted',
                        'icon': 'lock',
                        'helpText': 'Your community is restricted to users ' \
                                    'with access.'
                    }
                ]
            ),
            SITE_UI_URL=current_app.config["SITE_UI_URL"]
        ),
    )

@pass_community

# 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


@login_required
def communities_new():
    """Communities creation page."""
    return render_template(
        "invenio_communities/new.html",
        form_config=dict(
            access=dict(
                visibilty=[
                    {
                        'text': 'Public',
                        'value': 'public',
                        'icon': 'group',
                        'helpText': 'Your community is publicly accessible ' \
                                    'and shows up in search results.'
                    },
                    {
                        'text': 'Restricted',
                        'value': 'restricted',
                        'icon': 'lock',
                        'helpText': 'Your community is restricted to users ' \
                                    'with access.'
                    }
                ]
            ),
            SITE_UI_URL=current_app.config["SITE_UI_URL"]
        ),
    )

@pass_community

@pass_community_logo