Ejemplo n.º 1
0
 def __init__(self, instance, *args, **kwargs):
     self.instance = instance
     self.reader = create_csv_reader(instance.csv_file.file)
     self.klass = self.instance.content_type.model_class()
     # pylint: disable-msg=W0212
     choices = ([(None, '---- (None)')] +
                [(f.name, f.name) for f in self.klass._meta.fields])
     super(CSVAssociateForm, self).__init__(*args, **kwargs)
     for field_name in self.reader.fieldnames:
         self.fields[field_name] = forms.ChoiceField(choices=choices, required=False)
         mapped_field_name = self.klass.csvimporter['csv_associate'](field_name)
         if mapped_field_name in [f.name for f in self.klass._meta.fields]:
             self.fields[field_name].initial = mapped_field_name
         else:
             _choices = copy(choices)
             _choices.append((mapped_field_name, mapped_field_name))
             self.fields[field_name] = forms.ChoiceField(choices=_choices, required=False)
             self.fields[field_name].initial = mapped_field_name
Ejemplo n.º 2
0
 def clean_csv_file(self):
     if not 'csv_file' in self.cleaned_data:
         raise forms.ValidationError("No File selected.")
     # check file extension (.csv)
     if not self.cleaned_data['csv_file'].name.endswith(".csv"):
         raise forms.ValidationError(_("Wrong File extension."))
     # check if file can be parsed by pythons csv.DictReader()
     try:
         reader = create_csv_reader(self.cleaned_data['csv_file'].file)
     except:
         raise forms.ValidationError("Can't process file. Are you sure this is a csv file?")
     # check if format of csv is valid
     for field_name in reader.fieldnames:
         # check if we know all field_names
         # if not the csv is invalid => this raises an KeyError
         try:
             mapped_field_name = self.model.csvimporter['csv_associate'](field_name)
         except KeyError, e:
             raise forms.ValidationError('CSV is invalid. Fieldname "%s" is unknown.' % field_name)
Ejemplo n.º 3
0
    def __init__(self, instance, *args, **kwargs):
        self.instance = instance
        self.reader = create_csv_reader(instance.csv_file.file)
        self.klass = self.instance.content_type.model_class()
        # pylint: disable-msg=W0212
        choices = ([(None, '---- (None)')] +
                   [(f.name, f.name) for f in self.klass._meta.fields])
        super(CSVAssociateForm, self).__init__(*args, **kwargs)
        for field_name in self.reader.fieldnames:
            self.fields[field_name] = forms.ChoiceField(choices=choices,
                                                        required=False)
            #mapped_field_name = self.klass.csvimporter['csv_associate'](field_name)
            #if mapped_field_name in [f.name for f in self.klass._meta.fields]:

            if key_to_field_map(field_name) in [
                    f.name for f in self.klass._meta.fields
            ]:
                self.fields[field_name].initial = key_to_field_map(field_name)
            else:
                _choices = copy(choices)
                #_choices.append((key_to_field_map(field_name), key_to_field_map(field_name)))
                self.fields[field_name] = forms.ChoiceField(choices=_choices,
                                                            required=False)