Esempio n. 1
0
    def clean_upload_file(self):
        key = self.cleaned_data['key']
        upload_file = self.cleaned_data['upload_file']
        if not key:
            raise forms.ValidationError(_('Please specify the key to identify duplicates'))

        file_content = upload_file.read()
        upload_file.seek(0)
        header_line_index = file_content.find('\n')
        header_list = ((file_content[:header_line_index]
                            ).strip('\r')).split(',')
        header_list = normalize_field_names(header_list)
        key_list = []
        for key in key.split(','):
            key_list.append(key)
        missing_columns = []
        for item in key_list:
            if not item in header_list:
                missing_columns.append(item)
        if missing_columns:
            raise forms.ValidationError(_(
                        """
                        'Field(s) %(fields)s used to identify the duplicates
                        should be included in the .csv file.'
                        """ % {'fields' : ', '.join(missing_columns)}))

        return upload_file
Esempio n. 2
0
    def clean_upload_file(self):
        key = self.cleaned_data['key']
        upload_file = self.cleaned_data['upload_file']
        if not key:
            raise forms.ValidationError(
                _('Please specify the key to identify duplicates'))

        file_content = upload_file.read().decode(
            'utf-8')  # decode from bytes to string
        upload_file.seek(0)
        header_line_index = file_content.find('\n')
        header_list = ((
            file_content[:header_line_index]).strip('\r')).split(',')
        header_list = normalize_field_names(header_list)
        key_list = []
        for key in key.split(','):
            key_list.append(key)
        missing_columns = []
        for item in key_list:
            if item not in header_list:
                missing_columns.append(item)
        if missing_columns:
            raise forms.ValidationError(
                _("""
                        'Field(s) %(fields)s used to identify the duplicates
                        should be included in the .csv file.'
                        """ % {'fields': ', '.join(missing_columns)}))

        return upload_file