<commit_before># coding=utf-8
"""
"""
from __future__ import absolute_import

import logging

from werkzeug.exceptions import Forbidden
from werkzeug.utils import import_string
from flask import Blueprint, g
from flask.helpers import _endpoint_from_view_func
from flask.ext.login import current_user

from abilian.i18n import _l
from abilian.services.security import security, Admin as AdminRole
from abilian.web.action import actions
from abilian.web.nav import NavGroup, NavItem, BreadcrumbItem, Endpoint

from .panel import AdminPanel

logger = logging.getLogger(__name__)

_BP_PREFIX = 'admin'


class Admin(object):
  """
  Flask extension for an admin interface with pluggable admin panels.

  Note: this is quite different that a Django-style admin interface.
  """

  def __init__(self, *panels, **kwargs):
    self.app = None
    self.panels = []
    self._panels_endpoints = {}
    self.nav_paths = {}
    self.breadcrumb_items = {}
    self.setup_blueprint()

    self.nav_root = NavGroup(
      'admin', 'root', title=_l(u'Admin'),
      endpoint=None,
      condition=lambda context: (not current_user.is_anonymous()
                                 and security.has_role(current_user, AdminRole))
    )

    for panel in panels:
      self.register_panel(panel)

    app = kwargs.pop('app', None)
    if app is not None:
      self.init_app(app)

  def init_app(self, app):
    panels = app.config.get('ADMIN_PANELS', ())

    # resolve fully qualified name into an AdminPanel object
    for fqn in panels:
      panel_class = import_string(fqn, silent=True)
      if panel_class is None:
        logger.warning('Could not import panel: "%s"', fqn)
        continue
      if not issubclass(panel_class, AdminPanel):
        logger.error('"%s" is not a %s.AdminPanel, skipping',
                     fqn, AdminPanel.__module__)
        continue

      self.register_panel(panel_class())
      logger.debug('Registered panel "%s"', fqn)

    if not self.panels:
      @self.blueprint.route('', endpoint='no_panel')
      def no_panels_view():
        return "No panels registered"

      self.nav_root.endpoint = 'admin.no_panel'
    else:
      self.nav_root.endpoint = self.nav_root.items[0].endpoint

    self.root_breadcrumb_item = BreadcrumbItem(
      label=self.nav_root.title,
      url=self.nav_root.endpoint,
    )

    app.register_blueprint(self.blueprint)

    with app.app_context():
      actions.register(self.nav_root, *self.nav_root.items)

    self.app = app
    app.extensions['admin'] = self

  def register_panel(self, panel):
    if self.app:
      raise ValueError('Extension already initialized for app, cannot add more'
                       ' panel')

    self.panels.append(panel)
    panel.admin = self
    rule = "/" + panel.id
    endpoint = nav_id = panel.id
    abs_endpoint = 'admin.{}'.format(endpoint)

    if hasattr(panel, 'get'):
      self.blueprint.add_url_rule(rule, endpoint, panel.get)
      self._panels_endpoints[abs_endpoint] = panel
    if hasattr(panel, 'post'):
      endpoint += "_post"
      self.blueprint.add_url_rule(rule, endpoint, panel.post, methods=['POST'])
      self._panels_endpoints['admin.' + endpoint] = panel

    panel.install_additional_rules(
        self.get_panel_url_rule_adder(panel, rule, endpoint))

    nav = NavItem('admin:panel', nav_id,
                  title=panel.label, icon=panel.icon, divider=False,
                  endpoint=abs_endpoint)
    self.nav_root.append(nav)
    self.nav_paths[abs_endpoint] = nav.path
    self.breadcrumb_items[panel] = BreadcrumbItem(
      label=panel.label,<commit_msg>admin extension: fix additionals endpoint when panel has post method<commit_after># coding=utf-8
"""
"""
from __future__ import absolute_import

import logging

from werkzeug.exceptions import Forbidden
from werkzeug.utils import import_string
from flask import Blueprint, g
from flask.helpers import _endpoint_from_view_func
from flask.ext.login import current_user

from abilian.i18n import _l
from abilian.services.security import security, Admin as AdminRole
from abilian.web.action import actions
from abilian.web.nav import NavGroup, NavItem, BreadcrumbItem, Endpoint

from .panel import AdminPanel

logger = logging.getLogger(__name__)

_BP_PREFIX = 'admin'


class Admin(object):
  """
  Flask extension for an admin interface with pluggable admin panels.

  Note: this is quite different that a Django-style admin interface.
  """

  def __init__(self, *panels, **kwargs):
    self.app = None
    self.panels = []
    self._panels_endpoints = {}
    self.nav_paths = {}
    self.breadcrumb_items = {}
    self.setup_blueprint()

    self.nav_root = NavGroup(
      'admin', 'root', title=_l(u'Admin'),
      endpoint=None,
      condition=lambda context: (not current_user.is_anonymous()
                                 and security.has_role(current_user, AdminRole))
    )

    for panel in panels:
      self.register_panel(panel)

    app = kwargs.pop('app', None)
    if app is not None:
      self.init_app(app)

  def init_app(self, app):
    panels = app.config.get('ADMIN_PANELS', ())

    # resolve fully qualified name into an AdminPanel object
    for fqn in panels:
      panel_class = import_string(fqn, silent=True)
      if panel_class is None:
        logger.warning('Could not import panel: "%s"', fqn)
        continue
      if not issubclass(panel_class, AdminPanel):
        logger.error('"%s" is not a %s.AdminPanel, skipping',
                     fqn, AdminPanel.__module__)
        continue

      self.register_panel(panel_class())
      logger.debug('Registered panel "%s"', fqn)

    if not self.panels:
      @self.blueprint.route('', endpoint='no_panel')
      def no_panels_view():
        return "No panels registered"

      self.nav_root.endpoint = 'admin.no_panel'
    else:
      self.nav_root.endpoint = self.nav_root.items[0].endpoint

    self.root_breadcrumb_item = BreadcrumbItem(
      label=self.nav_root.title,
      url=self.nav_root.endpoint,
    )

    app.register_blueprint(self.blueprint)

    with app.app_context():
      actions.register(self.nav_root, *self.nav_root.items)

    self.app = app
    app.extensions['admin'] = self

  def register_panel(self, panel):
    if self.app:
      raise ValueError('Extension already initialized for app, cannot add more'
                       ' panel')

    self.panels.append(panel)
    panel.admin = self
    rule = "/" + panel.id
    endpoint = nav_id = panel.id
    abs_endpoint = 'admin.{}'.format(endpoint)

    if hasattr(panel, 'get'):
      self.blueprint.add_url_rule(rule, endpoint, panel.get)
      self._panels_endpoints[abs_endpoint] = panel
    if hasattr(panel, 'post'):
      post_endpoint= endpoint + "_post"
      self.blueprint.add_url_rule(rule, post_endpoint, panel.post,
                                  methods=['POST'])
      self._panels_endpoints['admin.' + post_endpoint] = panel

    panel.install_additional_rules(
        self.get_panel_url_rule_adder(panel, rule, endpoint))

    nav = NavItem('admin:panel', nav_id,
                  title=panel.label, icon=panel.icon, divider=False,
                  endpoint=abs_endpoint)
    self.nav_root.append(nav)
    self.nav_paths[abs_endpoint] = nav.path
    self.breadcrumb_items[panel] = BreadcrumbItem(
      label=panel.label,
{'fraction_non_alphanumeric': 0.10485304169514696, 'fraction_numerical': 0.0010936431989063567, 'mean_word_length': 3.062187673514714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1529940', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2238, 'n_words': 529}