# This file is part of Indico.
# Copyright (C) 2002 - 2020 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.

from __future__ import absolute_import, unicode_literals

from flask import render_template
from markupsafe import Markup

from indico.core.db import db
from indico.core.db.sqlalchemy.protection import ProtectionMode
from indico.util.i18n import _
from indico.web.forms.fields import IndicoEnumRadioField
from indico.web.forms.widgets import JinjaWidget


class IndicoProtectionField(IndicoEnumRadioField):
    widget = JinjaWidget('forms/protection_widget.html', single_kwargs=True)
    radio_widget = JinjaWidget('forms/radio_buttons_widget.html', orientation='horizontal', single_kwargs=True)

    def __init__(self, *args, **kwargs):
        self.protected_object = kwargs.pop('protected_object')(kwargs['_form'])
        get_acl_message_url = kwargs.pop('acl_message_url', None)
        self.acl_message_url = get_acl_message_url(kwargs['_form']) if get_acl_message_url else None
        self.can_inherit_protection = self.protected_object.protection_parent is not None
        if not self.can_inherit_protection:
            kwargs['skip'] = {ProtectionMode.inheriting}
        super(IndicoProtectionField, self).__init__(*args, enum=ProtectionMode, **kwargs)

    def render_protection_message(self):
        protected_object = self.get_form().protected_object
        if hasattr(protected_object, 'get_non_inheriting_objects'):
            non_inheriting_objects = protected_object.get_non_inheriting_objects()
        else:
            non_inheriting_objects = []
        if isinstance(protected_object.protection_parent, db.m.Event):
            parent_type = _('Event')
        elif isinstance(protected_object.protection_parent, db.m.Category):
            parent_type = _('Category')
        else:
            parent_type = _('Session')
        rv = render_template('_protection_info.html', field=self, protected_object=protected_object,
                             parent_type=parent_type, non_inheriting_objects=non_inheriting_objects)
        return Markup(rv)

[((21, 13, 21, 76), 'indico.web.forms.widgets.JinjaWidget', 'JinjaWidget', (), '', False, 'from indico.web.forms.widgets import JinjaWidget\n'), ((22, 19, 22, 111), 'indico.web.forms.widgets.JinjaWidget', 'JinjaWidget', (), '', False, 'from indico.web.forms.widgets import JinjaWidget\n'), ((45, 13, 46, 100), 'flask.render_template', 'render_template', (), '', False, 'from flask import render_template\n'), ((47, 15, 47, 25), 'markupsafe.Markup', 'Markup', ({(47, 22, 47, 24): 'rv'}, {}), '(rv)', False, 'from markupsafe import Markup\n'), ((40, 26, 40, 36), 'indico.util.i18n._', '_', ({(40, 28, 40, 35): '"""Event"""'}, {}), "('Event')", False, 'from indico.util.i18n import _\n'), ((42, 26, 42, 39), 'indico.util.i18n._', '_', ({(42, 28, 42, 38): '"""Category"""'}, {}), "('Category')", False, 'from indico.util.i18n import _\n'), ((44, 26, 44, 38), 'indico.util.i18n._', '_', ({(44, 28, 44, 37): '"""Session"""'}, {}), "('Session')", False, 'from indico.util.i18n import _\n')]