import_utils

Utility classes to import files into Camelot

class camelot.view.import_utils.ColumnMapping(columns, rows, admin, default_fields)[source]

Object that maps the columns in the rows to import onto the fields of the data model. This object has attributes named column_0_field, column_1_field, ... Each of these attributes should be set to the field name in which the data of the column should be imported.

Parameters:
  • columns – the number of columns in the rows
  • rows – the list of RowData objects to import, the list should not be empty
  • admin – the admin object of the model in which to import
  • default_fields – a list of field names that will be used as the default fields to map
match_names()[source]

Use the data in the current row to determine field names

class camelot.view.import_utils.ColumnMappingAdmin(columns, admin, field_choices, entity=<class 'camelot.view.import_utils.ColumnMapping'>)[source]

Admin class that allows the user to manipulate the column mapping

Parameters:
  • columns – the number of columns for which to edit the mapping
  • admin – the admin object of the model in which to import
  • field_choices – the list of fields out of which the user can select
  • entity – the class that is used to define the column mapping
verbose_name_for_column(column)[source]

Create a column name starting from an index starting at 0 eg : column=0 -> name=’A’

class camelot.view.import_utils.ColumnSelectionAdmin(columns, admin, field_choices, entity=<class 'camelot.view.import_utils.ColumnMapping'>)[source]

Admin to edit a ColumnMapping class without data preview

class camelot.view.import_utils.RowData(row_number, row_data)[source]

Class representing the data in a single row of the imported file as an object with attributes column_0, column_1, ..., each representing the data in a single column of that row.

since the imported file might contain less columns than expected in some rows, the RowData object returns None for not existing attributes

Parameters:row_data – a list containing the data [column_0_data, column_1_data, ...] for a single row
class camelot.view.import_utils.RowDataAdmin(admin, column_mapping)[source]

Decorator that transforms the Admin of the class to be imported to an Admin of the RowData objects to be used when previewing and validating the data to be imported.

based on the field attributes of the original mode, it will turn the background color pink if the data is invalid for being imported.

Parameters:
  • admin – the camelot.admin.object_admin.ObjectAdmin admin object of the objects that will be imported
  • column_mapping – the ColumnMapping object that maps the columns in the row data to fields of the objects.
flush(obj)[source]

When flush is called, don’t do anything, since we’ll only save the object when importing them for real

get_validator(model)[source]

Creates a validator that validates the data to be imported, the validator will check if the background of the cell is pink, and if it is it will mark that object as invalid.

class camelot.view.import_utils.UTF8Recoder(f, encoding)[source]

Iterator that reads an encoded stream and reencodes the input to UTF-8.

class camelot.view.import_utils.UnicodeReader(f, dialect=<class csv.excel at 0xa7a2668>, encoding='utf-8', **kwds)[source]

A CSV reader which will iterate over lines in the CSV file “f”, which is encoded in the given encoding.

class camelot.view.import_utils.XlsReader(filename)[source]

Read an XLS/XLSX file and iterator over its lines.

The iterator returns each line of the excel as a list of strings.

The to_string field attribute is supposed to be able to interprete those strings and create a valid datatype.

get_format_string(xf_index)[source]
Returns:the string that specifies the format of a cell

Previous topic

forms

Next topic

main

This Page