<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.forms import widgets, models
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
from cmsplugin_cascade.settings import CMSPLUGIN_CASCADE
from cmsplugin_cascade.fields import PartialFormField
from cmsplugin_cascade.models import CascadePage


class SectionForm(models.ModelForm):
    def clean_glossary(self):
        glossary = self.cleaned_data['glossary']
        if self.check_unique_element_id(self.instance, glossary['element_id']) is False:
            msg = _("The element ID `{element_id}` is not unique for this page.")
            raise ValidationError(msg.format(**glossary))
        return glossary

    @classmethod
    def check_unique_element_id(cls, instance, element_id):
        """
        Check for uniqueness of the given element_id for the current page.
        Return None if instance is not yet associated with a page.
        """
        try:
            element_ids = instance.page.cascadepage.glossary.get('element_ids', {})
        except ObjectDoesNotExist:
            pass
        else:
            element_ids[str(instance.pk)] = element_id
            return len(element_ids) == len(set(element_ids.values()))


class SectionModelMixin(object):
    def element_id(self):
        id_attr = self.glossary.get('element_id')
        if id_attr:
            return '{bookmark_prefix}{0}'.format(id_attr, **CMSPLUGIN_CASCADE)


class SectionMixin(object):
    def get_form(self, request, obj=None, **kwargs):
        glossary_fields = list(kwargs.pop('glossary_fields', self.glossary_fields))
        glossary_fields.append(PartialFormField('element_id',
            widgets.TextInput(),
            label=_("Element ID"),
            help_text=_("A unique identifier for this element.")<commit_msg>In djangoCMS 3.3 the page object is already avialbale<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.forms import widgets, models
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _
from cmsplugin_cascade.settings import CMSPLUGIN_CASCADE
from cmsplugin_cascade.fields import PartialFormField
from cmsplugin_cascade.models import CascadePage


class SectionForm(models.ModelForm):
    def clean_glossary(self):
        glossary = self.cleaned_data['glossary']
        if self.check_unique_element_id(self.instance, glossary['element_id']) is False:
            msg = _("The element ID `{element_id}` is not unique for this page.")
            raise ValidationError(msg.format(**glossary))
        return glossary

    @classmethod
    def check_unique_element_id(cls, instance, element_id):
        """
        Check for uniqueness of the given element_id for the current page.
        Return None if instance is not yet associated with a page.
        """
        try:
            element_ids = instance.page.cascadepage.glossary.get('element_ids', {})
        except (AttributeError, ObjectDoesNotExist):
            pass
        else:
            element_ids[str(instance.pk)] = element_id
            return len(element_ids) == len(set(element_ids.values()))


class SectionModelMixin(object):
    def element_id(self):
        id_attr = self.glossary.get('element_id')
        if id_attr:
            return '{bookmark_prefix}{0}'.format(id_attr, **CMSPLUGIN_CASCADE)


class SectionMixin(object):
    def get_form(self, request, obj=None, **kwargs):
        glossary_fields = list(kwargs.pop('glossary_fields', self.glossary_fields))
        glossary_fields.append(PartialFormField('element_id',
            widgets.TextInput(),
            label=_("Element ID"),
            help_text=_("A unique identifier for this element.")
{'fraction_non_alphanumeric': 0.0926208651399491, 'fraction_numerical': 0.0015267175572519084, 'mean_word_length': 3.382385730211817, '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': '7230395', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1130, 'n_words': 276}