<commit_before>from django import forms
from django.conf import settings
from dimagi.utils.modules import to_function


class GenericRepeaterForm(forms.Form):
    url = forms.URLField(
        required=True,
        label='URL to forward to',
        help_text='Please enter the full url, like http://www.example.com/forwarding/',
        widget=forms.TextInput(attrs={"class": "url"})
    )


class FormRepeaterForm(GenericRepeaterForm):
    exclude_device_reports = forms.BooleanField(
        required=False,
        label='Exclude device reports',
        initial=True
    )

    def __init__(self, *args, **kwargs):
        self.domain = kwargs.pop('domain')
        super(FormRepeaterForm, self).__init__(*args, **kwargs)

        self.formats = get_repeater_formats(self.domain)

        if self.formats and len(self.formats) > 1:
            self.fields['format'] = forms.ChoiceField(
                required=True,
                label='Payload Format',
                choices=self.formats,
            )

    def clean(self):
        cleaned_data = super(FormRepeaterForm, self).clean()
        if 'format' not in cleaned_data:
            cleaned_data['format'] = self.formats[0][0]

        return cleaned_data


def get_repeater_formats(domain):
    formats = []
    for format_name, gen_path in settings.REPEATER_PAYLOAD_GENERATORS.items():
        gen = to_function(gen_path)
        if gen().enabled_for_domain(domain):
            formats.append((format_name, format_name))
    return formats
<commit_msg>display Repeater specifc formats in the options<commit_after>from corehq.apps.receiverwrapper.models import FormRepeater, get_all_formats_in_repeater

from django import forms
from django.conf import settings
from dimagi.utils.modules import to_function


class GenericRepeaterForm(forms.Form):
    url = forms.URLField(
        required=True,
        label='URL to forward to',
        help_text='Please enter the full url, like http://www.example.com/forwarding/',
        widget=forms.TextInput(attrs={"class": "url"})
    )


class FormRepeaterForm(GenericRepeaterForm):
    exclude_device_reports = forms.BooleanField(
        required=False,
        label='Exclude device reports',
        initial=True
    )

    def __init__(self, *args, **kwargs):
        self.domain = kwargs.pop('domain')
        super(FormRepeaterForm, self).__init__(*args, **kwargs)

        self.formats = get_repeater_formats(FormRepeater, self.domain)

        if self.formats and len(self.formats) > 1:
            self.fields['format'] = forms.ChoiceField(
                required=True,
                label='Payload Format',
                choices=self.formats,
            )

    def clean(self):
        cleaned_data = super(FormRepeaterForm, self).clean()
        if 'format' not in cleaned_data:
            cleaned_data['format'] = self.formats[0][0]

        return cleaned_data


def get_repeater_formats(repeater, domain):
    formats = []
    for format_name, details in get_all_formats_in_repeater(repeater).iteritems():
        generator_cls = details['generator_cls']
        if generator_cls().enabled_for_domain(domain):
            formats.append((format_name, format_name))
    return formats
{'fraction_non_alphanumeric': 0.10335195530726257, 'fraction_numerical': 0.00186219739292365, 'mean_word_length': 2.901937046004843, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1992286', 'n_tokens_mistral': 962, 'n_tokens_neox': 944, 'n_words': 195}