<commit_before>from django import forms
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django.utils.safestring import mark_safe

from models import MozillianProfile, Vote


VOTE_CHOICES = ((0, 'Skip'),
                (-1, 'No'),
                (1, 'Probably'),
                (2, 'Definetely'))


class HorizontalRadioRenderer(forms.RadioSelect.renderer):
    """Render RadioSelect Horizontally."""
    def render(self):
        return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))


class HorizontalRadioSelect(forms.widgets.RadioSelect):
    """Horizontal Radioselect."""
    renderer = HorizontalRadioRenderer


class VoteForm(forms.Form):
    """Voting Form."""
    def __init__(self, nominee, voter, *args, **kwargs):
        """Initialize form.
        Dynamically set fields.
        """
        super(VoteForm, self).__init__(*args, **kwargs)
        initial_data = 0
        if Vote.objects.filter(voter=voter, nominee=nominee).exists():
            initial_data = (Vote.objects
                            .filter(voter=voter, nominee=nominee)[0].vote)
        self.fields['mozillian_vote__%s__%s'
                    % (str(nominee.id), str(voter.id))] = (
                        forms.ChoiceField(
                            widget=HorizontalRadioSelect(),
                            initial=initial_data,
                            choices=VOTE_CHOICES,
                            label=''))

    def save(self, *args, **kwargs):
        cdata = self.cleaned_data
        for field, votes in cdata.items():
            nominee_id = field.split('__')[1]
            voter_id = field.split('__')[2]
            voter = get_object_or_404(User, pk=voter_id)
            nominee = get_object_or_404(MozillianProfile, pk=nominee_id)
            vote_obj = get_object_or_404(Vote, voter=voter, nominee=nominee)<commit_msg>Fix initial data on form population<commit_after>from django import forms
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django.utils.safestring import mark_safe

from models import MozillianProfile, Vote


VOTE_CHOICES = ((0, 'Skip'),
                (-1, 'No'),
                (1, 'Probably'),
                (2, 'Definetely'))


class HorizontalRadioRenderer(forms.RadioSelect.renderer):
    """Render RadioSelect Horizontally."""
    def render(self):
        return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))


class HorizontalRadioSelect(forms.widgets.RadioSelect):
    """Horizontal Radioselect."""
    renderer = HorizontalRadioRenderer


class VoteForm(forms.Form):
    """Voting Form."""
    def __init__(self, nominee, voter, *args, **kwargs):
        """Initialize form.
        Dynamically set fields.
        """
        super(VoteForm, self).__init__(*args, **kwargs)
        if Vote.objects.filter(voter=voter, nominee=nominee).exists():
            initial_data = (Vote.objects
                            .filter(voter=voter, nominee=nominee)[0].vote)
            self.fields['mozillian_vote__%s__%s' % (str(nominee.id), str(voter.id))] = (
                forms.ChoiceField(widget=HorizontalRadioSelect(),
                                  initial=initial_data,
                                  choices=VOTE_CHOICES,
                                  label=''))
        else:
            self.fields['mozillian_vote__%s__%s' % (str(nominee.id), str(voter.id))] = (
                forms.ChoiceField(widget=HorizontalRadioSelect(),
                                  choices=VOTE_CHOICES,
                                  label=''))

    def save(self, *args, **kwargs):
        cdata = self.cleaned_data
        for field, votes in cdata.items():
            nominee_id = field.split('__')[1]
            voter_id = field.split('__')[2]
            voter = get_object_or_404(User, pk=voter_id)
            nominee = get_object_or_404(MozillianProfile, pk=nominee_id)
            vote_obj = get_object_or_404(Vote, voter=voter, nominee=nominee)
{'fraction_non_alphanumeric': 0.12063808574277168, 'fraction_numerical': 0.009720837487537388, 'mean_word_length': 2.236290322580645, '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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4119658', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1161, 'n_words': 228}