Sujet précédent

5.1.10. controllers.user — Controllers for users

Sujet suivant

5.3. decomposers — Utilities to decompose a part

Cette page

Autres langues

5.2. csvimport — Tools to import data from a CSV file

Tools to import data from a CSV file.

exception plmapp.csvimport.CSVImportError(errors)[source]

Bases: exceptions.StandardError

Exception raised when an import of a CSV file fails.

class plmapp.csvimport.Preview(csv_file, encoding, known_headers)[source]

Bases: object

Preview of a CSV file.

Paramètres:
  • csv_file (a file like object) – the csv file being parsed
  • encoding – encoding of the file (utf-8, ascii, etc.)
  • known_headers – collection of headers that may be valid
headers

headers of the CSV file

guessed_headers

headers translated according to known_headers, an header that can not be translated is replaced by None

rows

first non-headers rows of the file (at most two rows)

class plmapp.csvimport.CSVImporter(csv_file, user, encoding='utf-8')[source]

Bases: object

Abstract class to import data from a CSV file.

Paramètres:
  • csv_file (a file like object) – file being imported
  • user (User) – user who imports the file
  • encoding – encoding of the file (utf-8, ascii, etc.)

For “end users”, this class has two useful methods:

An implementation must overwrite the methods get_headers_set() and parse_row() and redefine the attribute REQUIRED_HEADERS.

REQUIRED_HEADERS = ()

Headers that must be present in the csv file

classmethod get_headers_set()[source]

Returns a set of all possible headers.

Note

This method is abstract and must be implemented.

classmethod get_headers()[source]

Returns a sorted list of all possible headers.

classmethod get_missing_headers_msg()[source]

Returns a message explaining which headers are required.

get_preview()[source]

Returns a Preview of the csv file.

import_csv(headers)[source]

Imports the csv file. headers is the list of headers as given by the user. Columns whose header is None are ignored. headers must contains all values of REQUIRED_HEADERS.

If one or several errors occur (missing headers, row which can not be parsed), a CSVImportError is raised with all detected errors.

Retourne:A list of PLMObjectController of all created objects.
tear_down()[source]

Method called once all rows have been successfully parsed.

By default, this method sends all blocked mails.

store_errors(line, *errors)[source]

Appends errors to the list of errors which occured at the line line.

get_value(row, header)[source]
get_values(row, *headers)[source]
parse_row(line, row)[source]

Method called by import_csv() for each row.

Paramètres:
  • line (int) – line number of current row, useful to store a list of errors
  • row (list of unicode strings.) – row being parsed.

This method must be overwritten. Implementation can use the methods get_value(), get_values(), and store_errors() to retrieve values and store detected errors.

Warning

All Controller created should not send emails since an error may occur and thus, all modifications would be cancelled. To block mails, call Controller.block_mails(). You can released all blocked mails by appending the controller to objects. import_csv() will send mails if no errors occurred.

Example:

ctrl = get_obj(type, reference, revision, user)
ctrl.block_mails()
...
if ok:
    self.objects.append(ctrl)
class plmapp.csvimport.PLMObjectsImporter(csv_file, user, encoding='utf-8')[source]

Bases: plmapp.csvimport.CSVImporter

An CSVImporter that creates PLMObject from a csv file.

The CSV must contain the following columns:

  • type
  • reference
  • revision
  • name
  • group (name of the group, not its id)
  • lifecycle (name of the lifecycle, not its id)

Moreover, it must have a column for each required field of defined types.

REQUIRED_HEADERS = ('type', 'reference', 'revision', 'name', 'group', 'lifecycle')

Headers that must be present in the csv file

classmethod get_headers_set()[source]

Returns a set of all possible headers.

tear_down()[source]
parse_row(line, row)[source]

Method called by import_csv() for each row.

class plmapp.csvimport.BOMImporter(csv_file, user, encoding='utf-8')[source]

Bases: plmapp.csvimport.CSVImporter

A CSVImporter that builds a bom from a CSV file.

The CSV must contain the following columns:

  • parent-type
  • parent-reference
  • parent-revision
  • child-type
  • child-reference
  • child-revision
  • quantity
  • order
REQUIRED_HEADERS = ('parent-type', 'parent-reference', 'parent-revision', 'child-type', 'child-reference', 'child-revision', 'quantity', 'order')
HEADERS_SET = set(['parent-reference', 'parent-revision', 'child-revision', 'child-reference', 'child-type', 'order', 'parent-type', 'quantity'])
classmethod get_headers_set()[source]
parse_row(line, row)[source]
class plmapp.csvimport.UsersImporter(csv_file, user, encoding='utf-8')[source]

Bases: plmapp.csvimport.CSVImporter

A CSVImporter that sponsors users from a CSV file.

The CSV must contain the following columns:

  • username
  • first_name
  • last_name
  • email
  • groups (multiple groups can be separeted by a “/”)
  • language
REQUIRED_HEADERS = ('username', 'first_name', 'last_name', 'email', 'groups', 'language')
HEADERS_SET = set(['username', 'first_name', 'last_name', 'language', 'groups', 'email'])
classmethod get_headers_set()[source]
tear_down()[source]
parse_row(line, row)[source]
plmapp.csvimport.IMPORTERS = {'csv': <class 'plmapp.csvimport.PLMObjectsImporter'>, 'bom': <class 'plmapp.csvimport.BOMImporter'>, 'users': <class 'plmapp.csvimport.UsersImporter'>}

Dictionary (name -> CSVImporter’s subclass) of known CSVImporter