<commit_before>import os

from django import forms
from django.template.defaultfilters import filesizeformat

from tgmfiles.models import TemporaryFileWrapper, get_max_file_size, human_readable_types, get_size_error


def allowed_type(file_type, allowed_types):
    if type(file_type) is not str:
        file_type = str(file_type, 'utf8')

    return file_type in allowed_types


class TemporaryFileForm(forms.ModelForm):
    class Meta:
        model = TemporaryFileWrapper

        fields = ('file', )

    def __init__(self, real_field, *args, **kwargs):
        self.allowed_types = real_field.allowed_types

        super().__init__(*args, **kwargs)

    def clean_file(self):
        uploaded_file = self.cleaned_data.get('file', False)
        if uploaded_file:
            if uploaded_file._size > get_max_file_size():
                # TODO: Correct size for error.
                raise forms.ValidationError(get_size_error())
        else:
            raise forms.ValidationError("Couldn't read uploaded file")

        if not allowed_type(uploaded_file.content_type, self.allowed_types):
            # TODO: i18n and handle plurar form.
            raise forms.ValidationError("File should be one of the following types [%s]" % (
                human_readable_types(self.allowed_types)))

        return uploaded_file<commit_msg>py2 compability<commit_after>import os

from django import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _

from tgmfiles.fields import TgmFormFileField
from tgmfiles.models import TemporaryFileWrapper, get_max_file_size, human_readable_types, get_size_error


def allowed_type(file_type, allowed_types):
    if type(file_type) is not str:
        file_type = str(file_type, 'utf8')

    return file_type in allowed_types


class TemporaryFileForm(forms.ModelForm):
    class Meta:
        model = TemporaryFileWrapper

        fields = ('file', )

    def __init__(self, real_field, *args, **kwargs):
        self.allowed_types = real_field.allowed_types

        super(TemporaryFileForm, self).__init__(*args, **kwargs)

    def clean_file(self):
        uploaded_file = self.cleaned_data.get('file', False)
        if uploaded_file:
            if uploaded_file._size > get_max_file_size():
                raise forms.ValidationError(get_size_error())
        else:
            raise forms.ValidationError(_("Couldn't read uploaded file"))

        if not allowed_type(uploaded_file.content_type, self.allowed_types):
            TgmFormFileField.file_type_error(uploaded_file.content_type, self.allowed_types)

        return uploaded_file
{'fraction_non_alphanumeric': 0.07972922151184655, 'fraction_numerical': 0.00188040616773223, 'mean_word_length': 3.353518821603928, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1602462', 'n_tokens_mistral': 794, 'n_tokens_neox': 749, 'n_words': 180}