<commit_before>#-*- coding: utf-8 -*-
from django.forms import Select, TextInput
from django.forms.widgets import MultiWidget
from django.template import Context
from django.template.loader import get_template
from .models import CallingCode

class CountryCodeSelect(Select):
    initial = None

    def __init__(self, phone_widget):
        self.phone_widget = phone_widget
        choices = [('', '---------')]
        calling_codes = CallingCode.objects.filter(country__active=True)
        for calling_code in calling_codes:
            choices.append((unicode(calling_code.id), unicode(calling_code)))
        return super(CountryCodeSelect, self).__init__(choices=sorted(choices, key=lambda item: item[1]))

    def render(self, name, value, *args, **kwargs):
        if isinstance(value, CallingCode):
            value = unicode(value.id)
        if value == self.phone_widget.empty_country_code:
            value = ""
        return super(CountryCodeSelect, self).render(name, value, *args, **kwargs)
    
    def value_from_datadict(self, *args, **kwargs):
        """
        Returns a country code model instance
        """
        pk = super(CountryCodeSelect, self).value_from_datadict(*args, **kwargs)
        try:
            code = CallingCode.objects.get(pk=pk)
        except CallingCode.DoesNotExist:
            code = None
        return code


class PhoneNumberWidget(MultiWidget):
    """
    A Widget that splits phone number input into:
    - an input for the country code prefix
    - an input for local phone number
    - an input for extension
    """
    template_name = "phonenumber_field/format_phone_number_widget_output.html"
    
    def __init__(self, attrs=None, initial=None):
        widgets = (CountryCodeSelect(self), TextInput(), TextInput())

        def f(i):
            def id_for_label(id_):
                if id_.endswith("_0"):
                    id_ = id_[:-2]
                return "{0}_{1}".format(id_, i) if id_ else id_
            return id_for_label
<commit_msg>handle case of malformed pk value (i.e. badly formed hex uuid). do not process an empty value<commit_after>#-*- coding: utf-8 -*-
from django.forms import Select, TextInput
from django.forms.widgets import MultiWidget
from django.template import Context
from django.template.loader import get_template
from .models import CallingCode

class CountryCodeSelect(Select):
    initial = None

    def __init__(self, phone_widget):
        self.phone_widget = phone_widget
        choices = [('', '---------')]
        calling_codes = CallingCode.objects.filter(country__active=True)
        for calling_code in calling_codes:
            choices.append((unicode(calling_code.id), unicode(calling_code)))
        return super(CountryCodeSelect, self).__init__(choices=sorted(choices, key=lambda item: item[1]))

    def render(self, name, value, *args, **kwargs):
        if isinstance(value, CallingCode):
            value = unicode(value.id)
        if value == self.phone_widget.empty_country_code:
            value = ""
        return super(CountryCodeSelect, self).render(name, value, *args, **kwargs)
    
    def value_from_datadict(self, *args, **kwargs):
        """
        Returns a country code model instance
        """
        code = None
        pk = super(CountryCodeSelect, self).value_from_datadict(*args, **kwargs)
        if pk:
            try:
                code = CallingCode.objects.get(pk=pk)
            except (CallingCode.DoesNotExist, ValueError):
                pass
        return code


class PhoneNumberWidget(MultiWidget):
    """
    A Widget that splits phone number input into:
    - an input for the country code prefix
    - an input for local phone number
    - an input for extension
    """
    template_name = "phonenumber_field/format_phone_number_widget_output.html"
    
    def __init__(self, attrs=None, initial=None):
        widgets = (CountryCodeSelect(self), TextInput(), TextInput())

        def f(i):
            def id_for_label(id_):
                if id_.endswith("_0"):
                    id_ = id_[:-2]
                return "{0}_{1}".format(id_, i) if id_ else id_
            return id_for_label

{'fraction_non_alphanumeric': 0.10587954110898662, 'fraction_numerical': 0.0028680688336520078, 'mean_word_length': 2.6265164644714036, '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': '2461599', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1173, 'n_words': 312}