<commit_before>import copy

from django import template
from django import forms
from django.utils.datastructures import SortedDict

register = template.Library()

def fieldset(parser, token):
    try:
        name, fields, as_, variable_name, from_, form = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError('bad arguments for %r'  % token.split_contents()[0])

    return FieldSetNode(fields.split(','), variable_name, form)

fieldset = register.tag(fieldset)


class FieldSetNode(template.Node):
    """
    Usage:
    {% fieldset field1,field2,field3 as fields from form %}
    <fieldset>
    {{ personal_fields.as_p }}
    </fieldset>
    """
    def __init__(self, fields, variable_name, form_variable):
        self.fields = fields
        self.variable_name = variable_name
        self.form_variable = form_variable

    def render(self, context):
        
        form = template.Variable(self.form_variable).resolve(context)
        new_form = copy.copy(form)        
        new_form.fields = SortedDict([(key, value) for key, value in form.fields.items() if key in self.fields])

        context[self.variable_name] = new_form

        return u''
<commit_msg>fieldset now supports translated fields if transmeta is installed<commit_after>import copy

from django import template
from django import forms
from django.utils.datastructures import SortedDict

register = template.Library()

def fieldset(parser, token):
    try:
        name, fields, as_, variable_name, from_, form = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError('bad arguments for %r'  % token.split_contents()[0])

    return FieldSetNode(fields.split(','), variable_name, form)

fieldset = register.tag(fieldset)



class FieldSetNode(template.Node):
    """
    Usage:
    {% fieldset field1,field2,field3 as fields from form %}
    <fieldset>
    {{ personal_fields.as_p }}
    </fieldset>
    """
    def __init__(self, fields, variable_name, form_variable):
        self.fields = fields
        self.variable_name = variable_name
        self.form_variable = form_variable

    def render(self, context):
        
        form = template.Variable(self.form_variable).resolve(context)
        fields = self.fields

        # if it is a modelform, get real fieldnames of translated fields from transmeta
        if hasattr(form, '_meta') and hasattr(form._meta, 'model'):
            try:
                from transmeta import get_all_translatable_fields, get_real_fieldname_in_each_language
                trans_fields = get_all_translatable_fields(form._meta.model)
                for field in trans_fields:
                    if field in fields:
                        fields.remove(field)
                        fields.extend(get_real_fieldname_in_each_language(field))
                print fields
            except ImportError:
                pass       

        new_form = copy.copy(form)
                    
        new_form.fields = SortedDict([(key, value) for key, value in form.fields.items() if key in fields])

        context[self.variable_name] = new_form

        return u''

{'fraction_non_alphanumeric': 0.09007585335018964, 'fraction_numerical': 0.0025284450063211127, 'mean_word_length': 2.7411347517730498, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1870583', 'n_tokens_mistral': 893, 'n_tokens_neox': 863, 'n_words': 226}