def guess_association_dict(self, csv_datas_headers, user_defined=None): """ Try to build an association dict between the header of the current csv file we want to import and the model we want to generate :param dict user_defined: An already defined association_dict we want to use as reference :returns: a dict with {headername: associated_column} :rtype: dict that's pickable (can be stored in the session) """ result = OrderedDict() for header in csv_datas_headers: header = ascii.force_unicode(header) if not header: continue result[header] = None toguess = header.replace('*', '').lower() for column in self.columns.values(): name = column['name'].lower() label = column['label'].lower() if toguess in [name, label]: result[header] = column['name'] if user_defined is not None: for key, value in user_defined.items(): if key in result: result[key] = value return result
def get_value(arguments, key, default=None): """ Return the value for key in arguments or default """ val = arguments.get('--%s' % key) if not val: val = default return ascii.force_unicode(val)
def collect_args(self, csv_line, force_rel_creation=False): """ Collect the arguments to be used to build the new model * get the value from the csv_line * format it thanks to the informations provided in the column info attr * place it in a new dict with the model attribute names as keys :param dict line: a csv line as a dict :param bool force_rel_creation: Should we try to build related configuration option on the fly ? :returns: a tuple with the args to be used for instanciation and the resting values """ kwargs = {} unhandled = {} for csv_key, value in csv_line.items(): value = ascii.force_unicode(value) if csv_key == 'id': column_name = 'id' else: key = ascii.force_unicode(csv_key) column_name = self.association_dict.get(key) if column_name is None: column_name = self.association_dict.get(csv_key) if column_name is None: unhandled[csv_key] = value else: column = self.columns.get(column_name) if column is not None: new_value = format_input_value( value, column, force_rel_creation, ) if new_value is not None: kwargs[column_name] = new_value else: unhandled[csv_key] = value else: kwargs[column_name] = value return kwargs, unhandled