<commit_before># -*- coding: utf-8 -*-
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.forms import ModelForm, TextInput, NumberInput, ModelChoiceField, DateTimeInput, Select, SelectMultiple \
    , ModelMultipleChoiceField, FileInput, HiddenInput
from django.forms.models import modelformset_factory
from sensordatainterface.models import *
from django.utils.translation import ugettext_lazy as _
from django import forms
from datetime import datetime
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.forms.util import flatatt
from django.forms.fields import BooleanField


class PrettyCheckboxWidget(forms.widgets.CheckboxInput):
    def render(self, name, value, attrs=None):
        final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
        if self.check_test(value):
            final_attrs['checked'] = 'checked'
        if not (value is True or value is False or value is None or value == ''):
            final_attrs['value'] = force_text(value)
        if 'prettycheckbox-label' in final_attrs:
            label = final_attrs.pop('prettycheckbox-label')
        else:
            label = ''
        return format_html('<label class="checkbox-label" for="{0}"><input{1} /> {2}</label>', attrs['id'], flatatt(final_attrs), label.capitalize())


class PrettyCheckboxField(BooleanField):
    widget = PrettyCheckboxWidget

    def __init__(self, *args, **kwargs):
        if kwargs['label']:
            kwargs['widget'].attrs['prettycheckbox-label'] = kwargs['label']
            kwargs['label'] = ''
        super(PrettyCheckboxField, self).__init__(*args, **kwargs)


class SamplingFeatureChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.samplingfeaturename


class OrganizationChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.organizationname


class EquipmentModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.modelname

    def validate(self, value):
        pass

    def to_python(self, value):
        try:
            value = super(EquipmentModelChoiceField, self).to_python(value)
        except self.queryset.model.DoesNotExist:
            key = self.to_field_name or 'pk'
            value = EquipmentModel.objects.filter(**{key: value})
            if not value.exists():
                raise ValidationError(self.error_messages['invalid_choice'], code='invalid_choice')
            else:
                value = value.first()
        return value


class PeopleChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.personfirstname + " " + obj.personlastname


class PeopleMultipleChoice(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return obj.organizationid.organizationname + ": " + obj.personid.personfirstname + " " + obj.personid.personlastname


class DeploymentActionChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        action = obj.actionid
        equipment = obj.equipmentid

        info = str(action.begindatetime) + " " + str(action.featureaction.get().samplingfeatureid.samplingfeaturecode) + ' ' + \
               str(equipment.equipmentserialnumber) + ' ' + str(equipment.equipmenttypecv.name) + ' ' + \
               str(equipment.equipmentmodelid.modelmanufacturerid.organizationname) + ' ' + \
               str(equipment.equipmentmodelid.modelpartnumber)

        return info


class MethodChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.methodname


class UnitChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.unitsname
<commit_msg>#116 Added some fallback text in case there is deployment data missing for the deployment select field.<commit_after># -*- coding: utf-8 -*-
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.forms import ModelForm, TextInput, NumberInput, ModelChoiceField, DateTimeInput, Select, SelectMultiple \
    , ModelMultipleChoiceField, FileInput, HiddenInput
from django.forms.models import modelformset_factory
from sensordatainterface.models import *
from django.utils.translation import ugettext_lazy as _
from django import forms
from datetime import datetime
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.forms.util import flatatt
from django.forms.fields import BooleanField


class PrettyCheckboxWidget(forms.widgets.CheckboxInput):
    def render(self, name, value, attrs=None):
        final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
        if self.check_test(value):
            final_attrs['checked'] = 'checked'
        if not (value is True or value is False or value is None or value == ''):
            final_attrs['value'] = force_text(value)
        if 'prettycheckbox-label' in final_attrs:
            label = final_attrs.pop('prettycheckbox-label')
        else:
            label = ''
        return format_html('<label class="checkbox-label" for="{0}"><input{1} /> {2}</label>', attrs['id'], flatatt(final_attrs), label.capitalize())


class PrettyCheckboxField(BooleanField):
    widget = PrettyCheckboxWidget

    def __init__(self, *args, **kwargs):
        if kwargs['label']:
            kwargs['widget'].attrs['prettycheckbox-label'] = kwargs['label']
            kwargs['label'] = ''
        super(PrettyCheckboxField, self).__init__(*args, **kwargs)


class SamplingFeatureChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.samplingfeaturename


class OrganizationChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.organizationname


class EquipmentModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.modelname

    def validate(self, value):
        pass

    def to_python(self, value):
        try:
            value = super(EquipmentModelChoiceField, self).to_python(value)
        except self.queryset.model.DoesNotExist:
            key = self.to_field_name or 'pk'
            value = EquipmentModel.objects.filter(**{key: value})
            if not value.exists():
                raise ValidationError(self.error_messages['invalid_choice'], code='invalid_choice')
            else:
                value = value.first()
        return value


class PeopleChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.personfirstname + " " + obj.personlastname


class PeopleMultipleChoice(ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return obj.organizationid.organizationname + ": " + obj.personid.personfirstname + " " + obj.personid.personlastname


class DeploymentActionChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        action = obj.actionid
        equipment = obj.equipmentid
        equipment_model = equipment.equipmentmodelid
        feature_action = action.featureaction.get()
        manufacturer = equipment_model.modelmanufacturerid if equipment_model is not None else None
        info = str(action.begindatetime) + ' '
        info += (str(feature_action.samplingfeatureid.samplingfeaturecode) + ' ') if feature_action is not None else ''
        info += (str(equipment.equipmentserialnumber) + ' ' + str(equipment.equipmenttypecv.name) + ' ') if equipment is not None else ''
        info += (str(manufacturer.organizationname) + ' ') if manufacturer is not None else ''
        info += (str(equipment_model.modelpartnumber) + ' ') if equipment_model is not None else ''
        return info


class MethodChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.methodname


class UnitChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.unitsname

{'fraction_non_alphanumeric': 0.09823330409723093, 'fraction_numerical': 0.001378273399323393, 'mean_word_length': 3.5637507146941108, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, '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': '4368653', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2130, 'n_words': 507}