import os

from django import forms
from django.utils.translation import ugettext as _
from django.conf import settings

from .models import Genome


class UploadForm(forms.Form):
    upload_files = forms.FileField(
        max_length=100,
        error_messages={'required': _('Select data file.'),
                        'max_length': _('Too long file name')}
    )

    population = forms.ChoiceField(
        Genome.POPULATION_CHOICES,
        error_messages={'required': _('Select population.'),
                        'invalid_choice': _('Select population.')}
    )

    file_format = forms.ChoiceField(
        Genome.FILE_FORMAT_CHOICES,
        error_messages={'required': _('Select file format.'),
                        'invalid_choice': _('Select file format.')}
    )

    sex = forms.ChoiceField(
        Genome.SEX_CHOICES,
        error_messages={'required': _('Select file format.'),
                        'invalid_choice': _('Select file format.')}
    )

    def clean_upload_files(self):
        upload_files = self.files.getlist('upload_files')

        for upload_file in upload_files:
            if len(upload_file.name) > settings.MAX_UPLOAD_GENOME_FILE_NAME_LENGTH:
                raise forms.ValidationError(_('Too long file name.'))

            if upload_file.size > settings.MAX_UPLOAD_GENOME_FILE_SIZE:
                raise forms.ValidationError(_('Too large file size: {file_name}'.format(file_name=upload_file.name)))

            ext = os.path.splitext(upload_file.name)[1].lower()[1:]
            if ext not in ('csv', 'txt', 'vcf', 'tsv'):
                msg = _('Not allowed file extention {ext}: {file_name}'.format(file_name=upload_file.name, ext=ext))
                log.warn(msg)
                raise forms.ValidationError(msg)

            if upload_file.content_type == 'text/plain':
                pass
            elif upload_file.content_type in ('text/vcard', 'text/x-vcard', 'text/directory;profile=vCard', 'text/directory') and ext == 'vcf':
                pass
            else:
                msg = _('Not allowed file type {content_type}: {file_name}'.format(file_name=upload_file.name, content_type=upload_file.content_type))
                log.warn(msg)
                raise forms.ValidationError(msg)

        return upload_files

@@ -142,16 +142,84 @@
 Genome%0A%0A
+from utils import clogging%0Alog = clogging.getColorLogger(__name__)%0A%0A
 %0Aclass U
@@ -1700,14 +1700,8 @@
 tion
- %7Bext%7D
 : %7Bf
@@ -1748,48 +1748,9 @@
 name
-, ext=ext))%0A                log.warn(msg
+)
 )%0A  
@@ -2213,38 +2213,8 @@
 e))%0A
-                log.warn(msg)%0A
     

fix logging