<commit_before>from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound

from . import SettingsView
from deform import Form, ValidationFailure

import logging
logger = logging.getLogger(__name__)

class Ldap(SettingsView):
    def __init__(self, request):
        super(Ldap, self).__init__(request, name='settings_ldap', title='LDAP')

    def breadcrumbs(self):
        breadcrumbs = super(Ldap, self).breadcrumbs()
        breadcrumbs.append(dict(route_path=self.request.route_path(self.name), title=self.title))
        return breadcrumbs

    @view_config(route_name='settings_ldap', renderer='../templates/settings/ldap.pt')
    def view(self):
        # Get LDAP settings
        ldap_settings = self.db.ldap.find_one()
        if ldap_settings is None:
            ldap_settings = dict()

        # Generate form
        from phoenix.schema.settings import LdapSchema
        ldap_form = Form(schema = LdapSchema(), buttons = ('submit',), formid = 'deform')

        if 'submit' in self.request.params:
            try:
                # Validate form
                appstruct = ldap_form.validate(self.request.params.items())
            except ValidationFailure, e:
                logger.exception('Validation failed!')
                return dict(title = 'LDAP Settings', form = e.render())
            else:
                # Update LDAP settings
                ldap_settings['server']      = appstruct['server']
                ldap_settings['bind']        = appstruct['bind']
                ldap_settings['passwd']      = appstruct['passwd']
                ldap_settings['base_dn']     = appstruct['base_dn']
                ldap_settings['filter_tmpl'] = appstruct['filter_tmpl']
                ldap_settings['scope']       = appstruct['scope']
                ldap_settings['email']       = appstruct['email']
                self.db.ldap.save(ldap_settings)

                import ldap
                if ldap_settings['scope'] == 'ONELEVEL':
                    ldap_scope = ldap.SCOPE_ONELEVEL
                else:
                    ldap_scope = ldap.SCOPE_SUBTREE

                from pyramid.config import Configurator
                config = Configurator(registry = self.request.registry)
                config.ldap_setup(ldap_settings['server'],
                        bind = ldap_settings['bind'],
                        passwd = ldap_settings['passwd'])
                config.ldap_set_login_query(
                        base_dn = ldap_settings['base_dn'],
                        filter_tmpl = ldap_settings['filter_tmpl'],
                        scope = ldap_scope)
                config.commit()

                self.session.flash('Successfully updated LDAP settings!', queue = 'success')

        # Display form
        return dict(title = 'LDAP Settings', form = ldap_form.render(ldap_settings))<commit_msg>Just added another comment.<commit_after>from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound

from . import SettingsView
from deform import Form, ValidationFailure

import logging
logger = logging.getLogger(__name__)

class Ldap(SettingsView):
    def __init__(self, request):
        super(Ldap, self).__init__(request, name='settings_ldap', title='LDAP')

    def breadcrumbs(self):
        breadcrumbs = super(Ldap, self).breadcrumbs()
        breadcrumbs.append(dict(route_path=self.request.route_path(self.name), title=self.title))
        return breadcrumbs

    @view_config(route_name='settings_ldap', renderer='../templates/settings/ldap.pt')
    def view(self):
        # Get LDAP settings
        ldap_settings = self.db.ldap.find_one()
        if ldap_settings is None:
            ldap_settings = dict()

        # Generate form
        from phoenix.schema.settings import LdapSchema
        ldap_form = Form(schema = LdapSchema(), buttons = ('submit',), formid = 'deform')

        if 'submit' in self.request.params:
            try:
                # Validate form
                appstruct = ldap_form.validate(self.request.params.items())
            except ValidationFailure, e:
                logger.exception('Validation failed!')
                return dict(title = 'LDAP Settings', form = e.render())
            else:
                # Update LDAP settings
                ldap_settings['server']      = appstruct['server']
                ldap_settings['bind']        = appstruct['bind']
                ldap_settings['passwd']      = appstruct['passwd']
                ldap_settings['base_dn']     = appstruct['base_dn']
                ldap_settings['filter_tmpl'] = appstruct['filter_tmpl']
                ldap_settings['scope']       = appstruct['scope']
                ldap_settings['email']       = appstruct['email']
                self.db.ldap.save(ldap_settings)

                import ldap
                if ldap_settings['scope'] == 'ONELEVEL':
                    ldap_scope = ldap.SCOPE_ONELEVEL
                else:
                    ldap_scope = ldap.SCOPE_SUBTREE

                from pyramid.config import Configurator
                config = Configurator(registry = self.request.registry)
                config.ldap_setup(ldap_settings['server'],
                        bind = ldap_settings['bind'],
                        passwd = ldap_settings['passwd'])
                config.ldap_set_login_query(
                        base_dn = ldap_settings['base_dn'],
                        filter_tmpl = ldap_settings['filter_tmpl'],
                        scope = ldap_scope)
                config.commit()
                # FK: It happens that the first login after updating will fail. I can't tell why.

                self.session.flash('Successfully updated LDAP settings!', queue = 'success')

        # Display form
        return dict(title = 'LDAP Settings', form = ldap_form.render(ldap_settings))
{'fraction_non_alphanumeric': 0.10006807351940095, 'fraction_numerical': 0.0, 'mean_word_length': 2.02159383033419, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4995715', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1551, 'n_words': 315}