<commit_before># -*- coding: utf-8 -*-
from django.contrib.sites.models import Site
from django.db.models import get_model
from django.forms import fields
from django.forms.widgets import TextInput
from django.forms.models import ModelForm
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist
from cms.models import Page
try:
    from .fields import PageSearchField as PageSelectFormField
except ImportError:
    from cms.forms.fields import PageSelectFormField
from .models import LinkElement


class LinkForm(ModelForm):
    """
    Form class to add fake fields for rendering the ModelAdmin's form, which later are used to
    populate the glossary of the model.
    """
    LINK_TYPE_CHOICES = (('cmspage', _("CMS Page")), ('exturl', _("External URL")), ('email', _("Mail To")),)
    link_type = fields.ChoiceField()
    cms_page = PageSelectFormField(required=False, label='',
        help_text=_("An internal link onto CMS pages of this site"))
    ext_url = fields.URLField(required=False, label='', help_text=_("Link onto external page"))
    mail_to = fields.EmailField(required=False, label='', help_text=_("Open Email program with this address"))

    class Meta:
        model = LinkElement
        fields = ('glossary',)

    class Media:
        js = ['admin/js/cascade-linkplugin.js']

    def __init__(self, *args, **kwargs):
        instance = kwargs.get('instance')
        initial = instance and dict(instance.glossary) or {'link': {'type': 'cmspage'}}
        initial.update(kwargs.pop('initial', {}))
        link_type = initial['link']['type']
        if instance:
            cms_page_queryset = Page.objects.drafts().on_site(instance.get_site())
        else:
            cms_page_queryset = Page.objects.drafts().on_site(Site.objects.get_current())
        self.base_fields['link_type'].choices = self.LINK_TYPE_CHOICES
        self.base_fields['link_type'].initial = link_type
        self.base_fields['cms_page'].queryset = cms_page_queryset
        getattr(self, 'set_initial_{0}'.format(link_type))(initial)
        kwargs.update(initial=initial)
        super(LinkForm, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(LinkForm, self).clean()
        if 'link_data' in cleaned_data:
            cleaned_data['glossary'].update(link=cleaned_data['link_data'])
            del self.cleaned_data['link_data']
        else:
            cleaned_data['glossary'].update(link={'type': 'none'})
        return cleaned_data
<commit_msg>simplified the code<commit_after># -*- coding: utf-8 -*-
from django.contrib.sites.models import Site
from django.db.models import get_model
from django.forms import fields
from django.forms.widgets import TextInput
from django.forms.models import ModelForm
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist
from cms.models import Page
try:
    from .fields import PageSearchField as PageSelectFormField
except ImportError:
    from cms.forms.fields import PageSelectFormField
from .models import LinkElement


class LinkForm(ModelForm):
    """
    Form class to add fake fields for rendering the ModelAdmin's form, which later are used to
    populate the glossary of the model.
    """
    LINK_TYPE_CHOICES = (('cmspage', _("CMS Page")), ('exturl', _("External URL")), ('email', _("Mail To")),)
    link_type = fields.ChoiceField()
    cms_page = PageSelectFormField(required=False, label='',
        help_text=_("An internal link onto CMS pages of this site"))
    ext_url = fields.URLField(required=False, label='', help_text=_("Link onto external page"))
    mail_to = fields.EmailField(required=False, label='', help_text=_("Open Email program with this address"))

    class Meta:
        model = LinkElement
        fields = ('glossary',)

    class Media:
        js = ['admin/js/cascade-linkplugin.js']

    def __init__(self, *args, **kwargs):
        instance = kwargs.get('instance')
        initial = instance and dict(instance.glossary) or {'link': {'type': 'cmspage'}}
        initial.update(kwargs.pop('initial', {}))
        link_type = initial['link']['type']
        site = instance and instance.get_site() or Site.objects.get_current()
        cms_page_queryset = Page.objects.drafts().on_site(site)
        self.base_fields['link_type'].choices = self.LINK_TYPE_CHOICES
        self.base_fields['link_type'].initial = link_type
        self.base_fields['cms_page'].queryset = cms_page_queryset
        getattr(self, 'set_initial_{0}'.format(link_type))(initial)
        kwargs.update(initial=initial)
        super(LinkForm, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(LinkForm, self).clean()
        if 'link_data' in cleaned_data:
            cleaned_data['glossary'].update(link=cleaned_data['link_data'])
            del self.cleaned_data['link_data']
        else:
            cleaned_data['glossary'].update(link={'type': 'none'})
        return cleaned_data

{'fraction_non_alphanumeric': 0.12251787132644956, 'fraction_numerical': 0.0007942811755361397, 'mean_word_length': 3.694315004659832, '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': '7367554', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1471, 'n_words': 363}